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

30_C++ 삽입 삭제 수정 실습

by chojju 2018. 5. 3.
반응형

//Member.h

#pragma once
#include <iostream>
using namespace std;

class Member
{
private:
 string name;
 string phone;
 int    age;

public:
 Member();
 Member(string _name, string _phone, int _age);
 ~Member();

public:
 string getName() const { return name; }
 string getPhone()const { return phone; }
 int    getAge() const { return age; }
 void setName(string value) { name = value; }
 void setPhone(string value) { phone = value; }
 void setAge(int value) { age = value; }

public:
 void Print() const;
};


//Log.h

#pragma once
#include <iostream>
using namespace std;
class Log
{
 int flag; //ex) insert > 1
 string msg; //    "입력"
 tm t;  //    로그저장시간
public:
 Log();
 Log(int _flag, string _msg, tm _t);
 ~Log();

public:
 static tm* CurrentTime();
 void Print();
};


Control.h

#pragma once
#include "BitArrayList.h"

class Control
{
 BitArrayList *alist;
 BitArrayList *alog;
public:
 Control();
 ~Control();

public:
 void Insert();
 void SelectAll();
 void Select();
 void Update();
 void Delete();
 void LogPrint();
};


//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
{
 void* * arr;  //저장소
 int   max; //저장 최대값
 int   size; //저장개수 및 저장할위치

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

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

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


//Member.cpp


#include "Member.h"

Member::Member()
{
}

Member::Member(string _name, string _phone, int _age)
 :name(_name), phone(_phone), age(_age)
{

}

Member::~Member()
{
}

void Member::Print() const
{
 printf("[이름] %-10s[전번] %-20s[나이] %-5d\n",
  name.c_str(), phone.c_str(), age);
}


//Log.cpp

///////Log소스
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <time.h>
#include "Log.h"

 

Log::Log()
{
}
Log::Log(int _flag, string _msg, tm _t)
 :flag(_flag), msg(_msg), t(_t)
{

}

Log::~Log()
{
}

tm* Log::CurrentTime()

 time_t timer;
 timer = time(NULL); //초단위
 tm *t;
 t = localtime(&timer);
 return t;
}

//time_t type을 tm으로 변경
void Log::Print()
{
 char tmp[20];
 sprintf(tmp, "%d:%d:%d",
  t.tm_hour, t.tm_min, t.tm_sec);
 printf("[flag]%-2d [msg]%-10s [time]%-20s\n",
  flag, msg.c_str(), tmp);
}


//Control.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include "Control.h"
#include "BitArrayList.h"
#include "Member.h"
#include "Log.h"

Control::Control()
{
 int num;
 cout << "최대 저장개수 : ";
 cin >> num;
 alist = new BitArrayList(num);
 alog = new BitArrayList(100);
}

Control::~Control()
{
 delete alist;
}


void Control::Insert()
{
 //1. 저장할 정보 생성
 char name[20], phone[20];
 int  age;
 getchar();
 cout << "이름 입력 : ";
 cin.getline(name, sizeof(name));
 cout << "전번 입력 : ";
 cin.getline(phone, sizeof(phone));
 cout << "나이 입력 : ";
 cin >> age;


 //2. 저장할 객체 생성
 Member *p = new Member(name, phone, age);

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

 //log 저장=============================
 tm *t = Log::CurrentTime();
 Log *log = new Log(1, "Insert", *t);

 alog->pushBack(log);
 //=====================================
}


void Control::SelectAll()
{
 for (int i = 0; i < alist->getSize(); i++)
 {
  Member *p = (Member*)alist->getData(i);
  p->Print();
 }
 cout << endl;

}


void Control::Select()
{
 char key[20];
 cout << "검색할 문자 입력 : ";
 getchar();
 cin.getline(key, sizeof(key));

 for (int i = 0; i < alist->getSize(); i++)
 {
  Member *p = (Member*)alist->getData(i);
  if (strcmp(key, p->getName().c_str()) == 0)
   //if (key == p->getName())
  {
   cout << "[인덱스] " << i << endl;
   p->Print();
   return;
  }
 }
 cout << "데이터가 존재하지 않습니다." << endl;

 //log 저장=============================
 char *msg = new char[20];
 strcpy(msg, "[2]Select");
 alog->pushBack(msg);
 //=====================================
}


void Control::Update()
{
 char key[20];
 cout << "수정할 이름 검색 : ";
 getchar();
 cin.getline(key, sizeof(key));

 for (int i = 0; i < alist->getSize(); i++)
 {
  Member *p = (Member*)alist->getData(i);
  if (strcmp(key, p->getName().c_str()) == 0)
  {
   char phone[20];
   int  age;
   cout << "수정할 전화번호 : ";
   cin.getline(phone, sizeof(phone));
   cout << "수정할 나이 : ";
   cin >> age;
   p->setPhone(phone);
   p->setAge(age);
   cout << "수정되었습니다." << endl;
   return;
  }
 }
 cout << "데이터가 존재하지 않습니다." << endl;

 //log 저장=============================
 char *msg = new char[20];
 strcpy(msg, "[3]Update");
 alog->pushBack(msg);
 //=====================================
}


void Control::Delete()
{
 char key[20];
 cout << "삭제할 이름 입력 : ";
 getchar();
 cin.getline(key, sizeof(key));

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

 //log 저장=============================
 char *msg = new char[20];
 strcpy(msg, "[4]Delete");
 alog->pushBack(msg);
 //=====================================
}

void Control::LogPrint()
{
 for (int i = 0; i < alog->getSize(); i++)
 {
  Log *p = (Log*)alog->getData(i);
  p->Print();
 }
 cout << endl;
}


//BitArrayList

#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 void*[max];
}


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

bool BitArrayList::pushBack(void *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 "Control.h"

int main()
{
 Control *con = new Control;
 int idx;
 while (true)
 {
  system("cls");
  con->SelectAll();
  cout << "====================================================" << endl;
  cout << "[1]입력 [2]검색 [3]수정 [4]삭제 [5]로그보기 [6]종료" << endl;
  cout << "====================================================" << endl;
  cin >> idx;
  switch (idx)
  {
  case 1: con->Insert();  break;
  case 2: con->Select();  break;
  case 3: con->Update();  break;
  case 4: con->Delete();  break;
  case 5: con->LogPrint(); break;
  case 6: return 0;
  }
  system("pause");
 }
 delete con;
 return 0;
}

반응형

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

32_상속(부모멤버들의 접근지정변경)  (0) 2018.05.03
31_상속  (0) 2018.05.03
29_C++ 실습  (0) 2018.05.03
29_new delete연산자  (0) 2018.05.03
27_has a 최종 문법 코드  (0) 2018.05.03

댓글