#include<iostream>
using namespace std;
#define Max 1000
struct Person
{
string m_Name;
string m_Sex;
int m_Age;
string m_Phone;
string m_Address;
};
struct Addressbooks
{
Person pesron[Max];
int size;
};
void ShowMenu()
{
cout << "**********************"<<endl;
cout << "*****1.添加联系人*****"<<endl;
cout << "*****2.显示联系人*****"<<endl;
cout << "*****3.删除联系人*****"<<endl;
cout << "*****4.查找联系人*****"<<endl;
cout << "*****5.修改联系人*****"<<endl;
cout << "*****6.清空联系人*****"<<endl;
cout << "*****0.退出通讯录*****"<<endl;
cout << "**********************"<<endl;
}
void addPerson(struct Addressbooks*book)
{
if(book->size==Max)
{
cout << "通讯录已经满,无法添加<<endl";
return;
}
else
{
cout << "请输入姓名:"<<endl;
cin>>book->pesron[book->size].m_Name;
int sex;
while (1)
{
string m_Sex[3] = { "女","男","未知" };
cout << "请输入性别: 0--女 1--男 2--未知 " << endl;
cin >> sex;
if (sex >= 0 && sex <= 2)
{
book->pesron[book->size].m_Sex = m_Sex[sex];
break;
}
else
cout << "输入错误 ,请重新输入" << endl;
}
cout << "请输入年龄:" << endl;
cin >> book->pesron[book->size].m_Age;
cout << "请输入电话:" << endl;
cin >> book->pesron[book->size].m_Phone;
cout << "请输入家庭住址:" << endl;
cin >> book->pesron[book->size].m_Address;
book->size++;
cout << " 添加成功! " << endl;
}
}
void showPerson(struct Addressbooks* book)
{
if (book->size == 0)
cout << "没有任何记录!" << endl;
else
for (int i = 0; i < book->size; i++)
{
cout << "姓名:" << book->pesron[i].m_Name<<"\t";
cout << " 性别:" << book->pesron[i].m_Sex <<"\t";
cout << " 年龄:" << book->pesron[i].m_Age <<"\t";
cout << " 电话号码:" << book->pesron[i].m_Phone<<"\t" ;
cout << " 住址:" << book->pesron[i].m_Address << endl;
}
}
void deletPerson(struct Addressbooks* book)
{
if (book->size == 0) cout << "通讯录已空" << endl;
else
{
cout << "请输入要删除的联系人名:" << endl;
string name;
cin >> name;
int i;
for ( i = 0; i < book->size; i++)
{
if (name == book->pesron[i].m_Name)
{
for (int j = i; j < book->size - 1; j++)
{
book->pesron[j] = book->pesron[j + 1];
book->size--;
}
cout << "删除成功" << endl;
break;
}
}
if (i == book->size)
cout << "通讯录中无此人!" << endl;
}
}
void findPerson(struct Addressbooks* book)
{
if (book->size == 0) cout << "通讯录为空" << endl;
else
{
cout << "请输入要查找的联系人名:" << endl;
string name;
cin >> name;
int i;
for (i = 0; i < book->size; i++)
{
if (name == book->pesron[i].m_Name)
{
cout << "姓名:" << book->pesron[i].m_Name << "\t";
cout << " 性别:" << book->pesron[i].m_Sex << "\t";
cout << " 年龄:" << book->pesron[i].m_Age << "\t";
cout << " 电话号码:" << book->pesron[i].m_Phone << "\t";
cout << " 住址:" << book->pesron[i].m_Address << endl;
}
}
if (i == book->size)
cout << "通讯录中无此人!" << endl;
}
}
void modifyPerson(struct Addressbooks* book)
{
if (book->size == 0) cout << "通讯录为空" << endl;
else
{
cout << "请输入要修改的联系人名:" << endl;
string name;
cin >> name;
int i;
int flag = 0;
for (i = 0; i < book->size; i++)
{
if (name == book->pesron[i].m_Name)
{
cout << "姓名: " ;
cin >> book->pesron[i].m_Name;
cout << endl;
int sex;
while (1)
{
string m_Sex[3] = { "女","男","未知" };
cout << " 0--女 1--男 2--未知 " << endl;
cout << "请输入性别";
cin >> sex;
cout << endl;
if (sex >= 0 && sex <= 2)
{
book->pesron[book->size].m_Sex = m_Sex[sex];
break;
}
else
cout << "输入错误 ,请重新输入" << endl;
}
cout << "年龄:" ;
cin >> book->pesron[i].m_Age;
cout<< endl;
cout << "电话号码:" ;
cin >> book->pesron[i].m_Phone;
cout<<endl ;
cout << "住址:";
cin>>book->pesron[i].m_Address;
cout << endl;
flag = 1;
}
}
if(flag==0)
cout << "通讯录中无此人!" << endl;
}
}
void clearPerson(struct Addressbooks* book)
{
int flag;
cout << "是否真的要清空通讯录" << endl;
cout << " 按其他键退出 按2清空" << endl;
cin >> flag;
if (flag == 2)
{
for (int i = 0; i < book->size; i++)
{
book->pesron[i] = {};
}
book->size = 0;
}
else
{
return;
}
}
int main()
{
Addressbooks book;
book.size = 0;
int input=0;
while (1)
{
ShowMenu();
cin >> input;
switch (input)
{
case 1:
addPerson(&book);
break;
case 2:
showPerson(&book);
break;
case 3:
deletPerson(&book);
break;
case 4:
findPerson(&book);
break;
case 5:
modifyPerson(&book);
break;
case 6:
clearPerson(&book);
break;
case 0:
cout << "退出通讯录!欢迎下次使用!";
break;
default: cout << "error input";
}
system("pause");
system("cls");
if (input == 0)
{
break;
}
}
}
|