언어

c++

파아랑새 2016. 1. 20. 19:36

#include<iostream>
#include<stdlib.h>
#define SIZE 10
#define ERROR 0
typedef int element;
using namespace std;
class sequence
{
public:
 int seq[SIZE];
 int dataCnt;
public:
 sequence()
 {
  seq[SIZE] = { 0, };
  dataCnt = 0;
 }
 void data_input();//[0]입력
 element data_remove();//[1]삭제
 void data_modification();//[2]수정
 void data_printf();//[3]출력
 void data_Cnt();//[4]데이터 갯수 출력
};
void sequence::data_input()
{
 int dataInsert;
 cout << "삽입할 데이터를 입력하세요 ";
 cin >> dataInsert;
 this->seq[this->dataCnt] = dataInsert;
 this->dataCnt++;
}
element sequence::data_remove()
{
 int element_data = 0;
 if (this->dataCnt == 0)
 {
  cout << "삭제할 데이터가 없습니다." << endl;
  return ERROR;
 }
 else
 //(this->dataCnt != 0)
 {
  this->dataCnt--;
  element_data = this->seq[this->dataCnt];
  return element_data;
 }
}
void sequence::data_modification()
{
 int position_modi = 0;
 if (this->dataCnt == 0)
 {
  cout << "수정할 데이터가 없습니다." << endl;
  return;
 }
 else//(this->dataCnt != 0)
 {
  cout << "<<<현재 데이터>>>" << endl;
  for (int index = 0; index < this->dataCnt; index++)
  {
   cout <<"["<< index + 1 <<"]"<<this->seq[index]<<endl;
  }
  cout << "수정할 데이터의 위치 입력:  ";
  while (true)
  {
   cin >> position_modi;
   if ((position_modi <= 0) || (position_modi > this->dataCnt))
   {
    cout << "수정할 수 없는 위치 입니다." << endl;
    cout << "다시입력해주세요" << endl;
   }
   else
   {
    cout << "수정하고자 하는 값 입력:  ";
    cin >> this->seq[position_modi-1];
    break;
   }
  }
  cout << "<<<수정 후 데이터>>>" << endl;
  for (int index = 0; index < this->dataCnt; index++)
  {
   cout << "[" << index + 1 << "]" << this->seq[index] << endl;
  }
 }
}
void sequence::data_printf()
{
 int index;
 if (this->dataCnt == 0)
 {
  cout << "출력할 데이터가 없습니다." << endl;
 }
 else
 //(this->dataCnt != 0)
 {
  for (index = 0; index < this->dataCnt; index++)
  {
   cout << this->seq[index] << endl;
  }
 }
}
void sequence::data_Cnt()
{
 cout << "현재 데이터 갯수는 " << this->dataCnt << " 입니다." << endl;
}
int main(void)
{
 cout << "[0] 데이터 삽입" << endl;
 cout << "[1] 데이터 삭제" << endl;
 cout << "[2] 데이터 수정" << endl;
 cout << "[3] 데이터 출력" << endl;
 cout << "[4] 데이터 갯수 출력" << endl;
 cout << "[5] 종료" << endl;
 sequence seq;
 char choice;
 int remove = 0;
 while (true)
 {
  cout << "선택: ";
  cin >> choice;
  switch (choice-'0')
  {
  case 0: seq.data_input();
    break;
  case 1: remove = seq.data_remove();
   if (seq.dataCnt != 0) cout << remove << "가 삭제 되었습니다." << endl;
    break;
  case 2: seq.data_modification();
    break;
  case 3: seq.data_printf();
    break;
  case 4: seq.data_Cnt();
    break;
  case 5: cout << "프로그램 종료" << endl;
    return 0;
  default: cout << "메뉴에 없는 값을 입력하셨습니다 다시 입력해주세요" << endl;
    break;
  }
 }
 return 0;
}