본문 바로가기
01.Bit 단기/C++

29_C++ 실습

by chojju 2018. 5. 3.
반응형


정수의 주소를 n개 저장관리
배열은 차곡차곡 쌓여있다

------------------------------------------------------------------------------------
class BitArrayList
자료구조 관점에서 3개의 변수 필요

1 저장소
// 값을 저장하는 공간
int arr[10] : 정수 10개 저장 
int *arr : 정수 n개 저장   // 동적 메모리 저장
 arr = (int*)malloc(sizeof(int)*n); // 힙 메모리에 n개를 저장할 수 있는 공간 생성

// 주소값을 저장하는 공간
int *arr[10]: 정수 주소 10개 저장 
int **arr : 정수 주소 n개 저장 // 동적 메모리 저장
 arr = (int**)malloc(sizeof(int*)*n); 

2 저장 최대값
 오버플로우 방지위해

3 저장개수 및 저장할 위치
 배열은 밑에서 쌓인다


메인함수에서 개체생성
헤더파일에서 생성자 생성
cpp파일에서 구현
cpp파일에서 멤버변수 초기화
메인함수


---------------------------------------------------------------------------------------


//BitArrayList.h

#pragma once
/*
int arr[10] : 정수 10개 저장
int *arr    : 정수 N개 저장
  arr = (int*)malloc(sizeof(int)*N);

int *arr[10]: 정수 주소 10개 저장
int * *arr   : 정수 주소 N개 저장
  arr = (int**)malloc(sizeof(int*)*N);
*/
class BitArrayList
{
 //자료구조 관점에서 배열은 3개의 맴버변수
 int** arr;  //저장소
 int   max; //저장 최대값
 int   size; //저장개수 및 저장할위치

public:
 BitArrayList(int max = 10);
 ~BitArrayList();

public:
 int getMax()  { return max; }
 int getSize()  { return size;  }
 int* getData(int idx) { return arr[idx]; }

public:
 bool pushBack(int *value);
 bool Erase(int idx);
private:
 bool isOverflow();
};


}

#include <iostream>
using namespace std;
#include "BitArrayList.h"

BitArrayList::BitArrayList(int max /*=10*/) 
{
 size = 0;
 //this : 자신의 개체 주소를 갖는 변수
 this->max = max;

// arr = (int**)malloc(sizeof(int*)*max);
 arr = new int*[max];
}


BitArrayList::~BitArrayList()
{
// free(arr);
 delete[] arr;
}

bool BitArrayList::pushBack(int *value)
{
 if (isOverflow() == true)
  return false;

 arr[size] = value;
 size++;
 return true;
}

bool BitArrayList::isOverflow()
{
 if (max <= size) return true;
 else    return false;
}

bool BitArrayList::Erase(int idx)
{
 if (idx >= 0 && idx < size)
 {
  delete arr[idx]; //<= 동적메모리삭제

  for (int i = idx; i < size - 1; i++)
  {
   arr[i] = arr[i + 1];
  }
  size--;
  return true;
 }
 else
  return false;
}


//소스.cpp

#include <iostream>
using namespace std;
#include "BitArrayList.h"

BitArrayList *alist;
void Insert();
void SelectAll();
void Select();
void Update();
void Delete();

int main()

 alist = new BitArrayList(10);
 int idx;
 while (true)
 {
  system("cls");
  SelectAll();
  cout << "========================================" << endl;
  cout << "[1]입력 [2]검색 [3]수정 [4]삭제 [5]종료" << endl;
  cout << "========================================" << endl;
  cin >> idx;
  switch (idx)
  {
  case 1: Insert();  break;
  case 2: Select();  break;
  case 3: Update();  break;
  case 4: Delete();  break;
  case 5: return 0;
  }
  system("pause");
 }
 delete alist;
 return 0;
}

void Delete()
{
 int key;
 cout << "삭제할 번호 입력 : ";
 cin >> key;

 for (int i = 0; i < alist->getSize(); i++)
 {
  int *p = alist->getData(i);
  if (key == *p)
  {
   alist->Erase(i);
   cout << "삭제되었습니다." << endl;
   return;
  }
 }
 cout << "데이터가 존재하지 않습니다." << endl;
}

void Update()
{
 int key;
 cout << "수정할 번호 입력 : ";
 cin >> key;

 for (int i = 0; i < alist->getSize(); i++)
 {
  int *p = alist->getData(i);
  if (key == *p)
  {
   int num;
   cout << "수정할 숫자를 입력 : ";
   cin >> num;
   *p = num; //<== 수정 코드...
   return;
  }
 }
 cout << "데이터가 존재하지 않습니다." << endl;
}

void Select()
{
 int key;
 cout << "검색할 번호 입력 : ";
 cin >> key;

 for (int i = 0; i < alist->getSize(); i++)
 {
  int *p = alist->getData(i);
  if (key == *p)
  {
   printf("%d 위치에 %d 가 저장되어 있습니다.\n",
    i, *p);
   return;
  }
 }
 cout << "데이터가 존재하지 않습니다." << endl;
}

void SelectAll()
{
 for (int i = 0; i < alist->getSize(); i++)
 {
  int *p = alist->getData(i);
  printf("%4d", *p);
 }
 cout << endl;
}

void Insert()
{
 //1. 저장할 정보 생성
 int num;
 cout << "저장할 정수 입력  : ";
 cin >> num;

 //2. 저장할 객체 생성
 int *p = new int(num);

 //3. 저장 요청
 if (alist->pushBack(p) == true)
  cout << "저장되었습니다." << endl;
 else
  cout << "저장공간이 없습니다." << endl;
}

 

반응형

'01.Bit 단기 > C++' 카테고리의 다른 글

31_상속  (0) 2018.05.03
30_C++ 삽입 삭제 수정 실습  (0) 2018.05.03
29_new delete연산자  (0) 2018.05.03
27_has a 최종 문법 코드  (0) 2018.05.03
26_has a 객체초기화  (0) 2018.05.03

댓글