通讯录管理系统
#include<iostream>
#include<cstring>
using namespace std;
#define MAX 1000
struct person
{
string name;
int sex;
int age;
string tel;
string address;
};
struct addressbooks
{
struct person persons[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(addressbooks* abs)
{
if (abs->Size == MAX)
{
cout << "通讯录已满" << endl;
return;
}
else
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->persons[abs->Size].name = name;
int sex = 0;
cout << "请输入性别: [1.男性][2.女性]" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->persons[abs->Size].sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
int age = 0;
cout << "请输入年龄:" << endl;
while (true)
{
cin >> age;
if (age > 0)
{
abs->persons[abs->Size].age = age;
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
string tel;
cout << "请输入联系电话:" << endl;
cin >> tel;
abs->persons[abs->Size].tel = tel;
string address;
cout << "请输入家庭住址:" << endl;
cin >> address;
abs->persons[abs->Size].address = address;
abs->Size++;
cout << "添加成功!!!" << endl;
system("pause");
system("cls");
}
}
void showperson(addressbooks* abs)
{
if (abs->Size == 0)
cout << "当前通讯录为空" << endl;
for (int i = 0; i < abs->Size; i++)
{
cout << "姓名:" << abs->persons[i].name << '\t';
if (abs->persons[i].sex == 1)
cout << "性别:男" << '\t';
else cout << "性别:女" << '\t';
cout << "年龄:" << abs->persons[i].age << '\t';
cout << "电话:" << abs->persons[i].tel << '\t';
cout << "家庭住址:" << abs->persons[i].address << '\t' << endl;
}
system("pause");
system("cls");
}
int is_exist(addressbooks* abs)
{
string name;
cout << "请输入联系人姓名" << endl;
cin >> name;
for (int i = 0; i < abs->Size; i++)
if (name == abs->persons[i].name)
return i;
cout << "联系人不存在" << endl;
return -1;
}
void deleteperson(addressbooks* abs,int pos)
{
if (pos == -1) {
system("pause");
system("cls");
return;
}
for (int i = pos; i < abs->Size; i++)
{
abs->persons[i].name = abs->persons[i + 1].name;
abs->persons[i].sex = abs->persons[i + 1].sex;
abs->persons[i].age = abs->persons[i + 1].age;
abs->persons[i].tel = abs->persons[i + 1].tel;
abs->persons[i].address = abs->persons[i + 1].address;
}
abs->Size--;
cout << "已删除该联系人" << endl;
system("pause");
system("cls");
}
void locateperson(addressbooks* abs, int pos)
{
if (pos == -1) {
system("pause");
system("cls");
return;
}
cout << "查找成功" << endl;
cout << abs->persons[pos].name << '\t';
if (abs->persons[pos].sex == 1)
cout << "性别:男" << '\t';
else cout << "性别:女" << '\t';
cout << abs->persons[pos].age << '\t';
cout << abs->persons[pos].tel << '\t';
cout << abs->persons[pos].address << '\t' << endl;
system("pause");
system("cls");
}
void alterperson(addressbooks* abs, int pos)
{
if (pos == -1) {
system("pause");
system("cls");
return;
}
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->persons[pos].name = name;
int sex = 0;
cout << "请输入性别: [1.男性][2.女性]" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->persons[pos].sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
string tel;
cout << "请输入联系电话:" << endl;
cin >> tel;
abs->persons[pos].tel = tel;
string address;
cout << "请输入家庭住址:" << endl;
cin >> address;
abs->persons[pos].address = address;
cout << "修改成功!!!" << endl;
system("pause");
system("cls");
}
void clearperson(addressbooks* abs)
{
int len = abs->Size;
for (int i = 0; i < len; i++)
{
abs->persons[i].name = abs->persons[i + 1].name;
abs->persons[i].sex = abs->persons[i + 1].sex;
abs->persons[i].age = abs->persons[i + 1].age;
abs->persons[i].tel = abs->persons[i + 1].tel;
abs->persons[i].address = abs->persons[i + 1].address;
abs->Size--;
}
cout << "已清空所有联系人!!!" << endl;
system("pause");
system("cls");
}
int main()
{
addressbooks abs;
abs.Size = 0;
int select = 0;
while (true)
{
showmenu();
cin >> select;
switch (select)
{
case 1: addperson(&abs); break;
case 2: showperson(&abs); break;
case 3: deleteperson(&abs,is_exist(&abs)); break;
case 4: locateperson(&abs, is_exist(&abs)); break;
case 5: alterperson(&abs, is_exist(&abs)); break;
case 6: clearperson(&abs); break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
}
}
system("pause");
return 0;
}