#include <iostream>
#include <string>
#include <ctime>
using namespace std;
#define MAX 1000
struct Person
{
string m_name;
int m_age;
int m_sex;
string m_iphone;
string m_address;
};
struct ConcatcBooks
{
struct Person personArray[MAX];
int m_size;
};
void addPerson(struct ConcatcBooks *pConcatcs)
{
if (pConcatcs->m_size >= MAX)
{
cout << "通讯录已满,无法添加" << endl;
return;
}
string name;
int age = 0;
int sex = 0;
string iphone;
string address;
cout << "请输入姓名:" << endl;
cin >> name;
pConcatcs->personArray[pConcatcs->m_size].m_name = name;
cout << "请输入年龄:" << endl;
cin >> age;
pConcatcs->personArray[pConcatcs->m_size].m_age = age;
while (age < 0)
{
cout << "输入错误,请重新输入" << endl;
cin >> age;
}
cout << "请输入性别(1:男 2:女):" << endl;
cin >> sex;
while (sex != 1 && sex != 2)
{
cout << "输入错误,请重新输入" << endl;
cin >> sex;
}
pConcatcs->personArray[pConcatcs->m_size].m_sex = sex;
cout << "请输入电话号码:" << endl;
cin >> iphone;
pConcatcs->personArray[pConcatcs->m_size].m_iphone = iphone;
// while (iphone.length() != 11)
// {
// cout << "输入错误,请重新输入" << endl;
// cin >> iphone;
// }
cout << "请输入地址:" << endl;
cin >> address;
pConcatcs->personArray[pConcatcs->m_size].m_address = address;
cout << "添加成功:" << endl;
pConcatcs->m_size++;
system("pause");
system("cls");
}
void showConcats(struct ConcatcBooks *pConcatcs)
{
if (pConcatcs->m_size == 0)
{
cout << "通讯录为空" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < pConcatcs->m_size; i++)
{
cout << "姓名:" << pConcatcs->personArray[i].m_name << "\t";
cout << "年龄:" << pConcatcs->personArray[i].m_age << "\t";
cout << "性别:" << (pConcatcs->personArray[i].m_sex == 1 ? "男" : "女") << "\t";
cout << "电话号码:" << pConcatcs->personArray[i].m_iphone << "\t";
cout << "地址:" << pConcatcs->personArray[i].m_address << endl;
}
system("pause");
system("cls");
}
int findPerson(string name, struct ConcatcBooks *pConcatcs)
{
for (int i = 0; i < pConcatcs->m_size; i++)
{
if (pConcatcs->personArray[i].m_name == name)
{
return i;
}
}
cout << "没有找到该联系人" << endl;
return -1;
}
void deletePerson(struct ConcatcBooks *pConcatcs)
{
if (pConcatcs->m_size == 0)
{
cout << "通讯录为空" << endl;
system("pause");
system("cls");
return;
}
int index;
string name;
cout << "请输入要删除的联系人姓名:" << endl;
cin >> name;
index = findPerson(name, pConcatcs);
if (index == -1)
{
system("pause");
system("cls");
return;
}
for (int i = index; i < pConcatcs->m_size; i++)
{
pConcatcs->personArray[i] = pConcatcs->personArray[i + 1];
}
cout << "删除成功" << endl;
pConcatcs->m_size--;
system("pause");
system("cls");
}
void findPerson(struct ConcatcBooks *pConcatcs)
{
if (pConcatcs->m_size == 0)
{
cout << "通讯录为空" << endl;
system("pause");
system("cls");
return;
}
string name;
cout << "输入要查找的人名" << endl;
cin >> name;
int index = findPerson(name, pConcatcs);
if (index == -1)
{
system("pause");
system("cls");
return;
}
cout << "姓名:" << pConcatcs->personArray[index].m_name << "\t";
cout << "年龄:" << pConcatcs->personArray[index].m_age << "\t";
cout << "性别" << (pConcatcs->personArray[index].m_sex == 1 ? "男" : "女") << "\t";
cout << "电话号码:" << pConcatcs->personArray[index].m_iphone << "\t";
cout << "地址:" << pConcatcs->personArray[index].m_address << endl;
system("pause");
system("cls");
}
void modifyPerason(struct ConcatcBooks *pConcatcs)
{
if (pConcatcs->m_size == 0)
{
cout << "通讯录为空" << endl;
system("pause");
system("cls");
return;
}
string name;
cout << "请输入要修改的联系人姓名:" << endl;
cin >> name;
int index = findPerson(name, pConcatcs);
if (index == -1)
{
system("pause");
system("cls");
return;
}
cout << "请输入新的信息:" << endl;
cout << "姓名:" << endl;
cin >> pConcatcs->personArray[index].m_name;
cout << "年龄:" << endl;
cin >> pConcatcs->personArray[index].m_age;
while (pConcatcs->personArray[index].m_age < 0)
{
cout << "输入错误,请重新输入" << endl;
cin >> pConcatcs->personArray[index].m_age;
}
cout << "性别(1:男 2:女):" << endl;
cin >> pConcatcs->personArray[index].m_sex;
while (pConcatcs->personArray[index].m_sex != 1 && pConcatcs->personArray[index].m_sex != 2)
{
cout << "输入错误,请重新输入" << endl;
cin >> pConcatcs->personArray[index].m_sex;
}
cout << "电话号码:" << endl;
cin >> pConcatcs->personArray[index].m_iphone;
while (pConcatcs->personArray[index].m_iphone.length() != 11)
{
cout << "输入错误,请重新输入" << endl;
cin >> pConcatcs->personArray[index].m_iphone;
}
cout << "地址:" << endl;
cin >> pConcatcs->personArray[index].m_address;
cout << "修改成功" << endl;
system("pause");
system("cls");
return;
}
void clearConcats(struct ConcatcBooks *pConcatcs)
{
pConcatcs->m_size = 0;
cout << "清空成功" << endl;
system("pause");
system("cls");
}
void showMenu()
{
cout << "***************************" << endl;
cout << "\t1、添加联系人" << endl;
cout << "\t2、显示联系人" << endl;
cout << "\t3、删除联系人" << endl;
cout << "\t4、查找联系人" << endl;
cout << "\t5、修改联系人" << endl;
cout << "\t6、清空联系人" << endl;
cout << "\t0、退出" << endl;
cout << "***************************" << endl;
}
int main()
{
int select = 0;
struct ConcatcBooks concatcBooks;
struct ConcatcBooks *pConcatcs = &concatcBooks;
concatcBooks.m_size = 0;
while (1)
{
showMenu();
cin >> select;
switch (select)
{
case 1:
addPerson(pConcatcs);
break;
case 2:
showConcats(pConcatcs);
break;
case 3:
deletePerson(pConcatcs);
break;
case 4:
findPerson(pConcatcs);
break;
case 5:
modifyPerason(pConcatcs);
break;
case 6:
clearConcats(pConcatcs);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
}