#include <iostream>
#include <string>
using namespace std;
const int MAX_Phone= 10;
class Phone
{
public:
Phone() : m_count(0){}
void Input()
{
if(m_count == MAX_Phone)
{
cout << "더 이상 추가 할 수 없습니다." << endl;
return;
}
while(1)
{
string phone, name;
cout << "이름입력 : ";
cin >> name;
cout << "전화번호입력 : ";
cin >> phone;
m_name[m_count] = name;
m_phone[m_count] = phone;
m_count++;
if(m_count == MAX_Phone)
{
cout << "더 이상 추가 할 수 없습니다." << endl;
break;
}
else
{
int sel;
cout << "1. 계속 2. 그만:";
cin >> sel;
if(sel == 2)
break;
}
}
}
void Delete()
{
if(m_count == 0)
{
cout << "등록된 전화번호가 없습니다." << endl;
return ;
}
string name;
cout << "삭제할 사람이름:";
cin >> name;
int sel;
cout << "1.삭제 2.취소:";
cin >> sel;
if(sel == 1)
{
for(int i = 0; i < m_count; i++)
{
if(m_name[i] == name)
{
for(int j = i; j < m_count-1; j++)
{
m_name[j] = m_name[j+1];
m_phone[j] = m_phone[j+1];
}
m_count--;
cout << "삭제되었습니다." << endl;
return;
}
}
cout << "해당하는 사람이 없습니다." << endl;
}
}
void Search()
{
if(m_count == 0)
{
cout << "등록된 전화번호가 없습니다." << endl;
return ;
}
while(1)
{
string name;
cout << "찾을 사람이름:";
cin >> name;
int count = 0;
for(int i = 0; i < m_count; i++)
{
if(m_name[i] == name)
{
cout << name << "의 전화번호=" << m_phone[i] << endl;
count++;
}
}
if(count)
cout << name << "은 " << count << "명 발견됨" << endl;
else
cout << "해당하는 사람이 없습니다" << endl;
int sel;
cout << "1. 계속 2. 그만:";
cin >> sel;
if(sel == 2)
break;
}
}
void Modify()
{
if(m_count == 0)
{
cout << "등록된 전화번호가 없습니다." << endl;
return ;
}
string name;
cout << "변경할 사람이름:";
cin >> name;
int count = 0;
for(int i = 0; i < m_count; i++)
{
if(m_name[i] == name)
{
cout << "새로운전화번호:";
cin >> m_phone[i];
cout << "변경되었습니다." << endl;
return;
}
}
cout << "해당하는 사람이 없습니다." << endl;
}
void Sort()
{
int sel;
cout << "1.이름순 2.전화번호순:";
cin >> sel;
string name, phone;
for(int i = 0; i < m_count-1; i++)
{
for(int j= i+1; j < m_count; j++)
{
if(Compare(sel, i, j))
{
name = m_name[i];
m_name[i] = m_name[j];
m_name[j] = name;
phone = m_phone[i];
m_phone[i] = m_phone[j];
m_phone[j] = phone;
}
}
}
cout << "정렬완료" << endl;
}
void Print()
{
cout.width(10); cout << "순번";
cout.width(15); cout << "이름";
cout.width(20); cout << "전화번호" << endl;
for(int i= 0; i < m_count; i++)
{
cout.width(10); cout << i+1;
cout.width(15); cout << m_name[i];
cout.width(20); cout << m_phone[i] << endl;
}
}
private:
int Compare(const int &SortKind, const int &i, const int &j)
{
if(SortKind == 1)
{
return (m_name[i].compare(m_name[j]) > 0);
}
else
{
return (m_phone[i].compare(m_phone[j]) > 0);
}
private:
string m_phone[MAX_Phone];
string m_name[MAX_Phone];
int m_count;
};
void PrintMenu()
{
cout<<"전화번호부 관리프로그램"<<endl;
cout << "1. 입력" << endl;
cout << "2. 출력" << endl;
cout << "3. 검색" << endl;
cout << "4. 삭제" << endl;
cout << "5. 변경" << endl;
cout << "6. 정렬" << endl;
cout << "7. 종료" << endl;
cout << "번호를 선택하세요 : ";
}
int Menu()
{
PrintMenu();
int menu;
cin >> menu;
return menu;
}
void main()
{
Phone j;
while(1)
{
switch(Menu())
{
case 1:
j.Input();
break;
case 2:
j.Print();
break;
case 3:
j.Search();
break;
case 4:
j.Delete();
break;
case 5:
j.Modify();
break;
case 6:
j.Sort();
break;
case 7:
return;
}
}
}
음..불량인듯..