题目描述:
- 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除指定联系人
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
#include <iostream>
#include <string>
#define MAX 1000
using namespace std;
typedef struct TONGXUNLU
{
struct PERSON
{
string name, number, address;
bool sex;
int age;
} person[MAX];
int length = 0;
} TONGXUNLU;
int findNameF(const TONGXUNLU * const tongXunLu, const string findName)
{
for (int i = 0; i < tongXunLu->length; i++)
if (findName == tongXunLu->person[i].name)
return i;
return -1;
}
void changePersonF(TONGXUNLU * const tongXunLu, const int index)
{
string name;
while (true)
{
cout << "请输入名字:";
cin >> name;
if (findNameF(tongXunLu, name) == -1)
{
tongXunLu->person[index].name = name;
break;
}
cout << "输入错误!" << endl;
}
cout << "请输入号码:";
cin >> tongXunLu->person[index].number;
cout << "请输入地址:";
cin >> tongXunLu->person[index].address;
int sex;
while (true)
{
cout << "请输入性别:";
cin >> sex;
if (sex == 1 || sex == 0)
{
tongXunLu->person[index].sex = sex;
break;
}
cout << "输入错误" << endl;
}
cout << "请输入年龄:";
cin >> tongXunLu->person[index].age;
}
void showPersonF(const TONGXUNLU * const tongXunLu, const int index)
{
cout << "======" << index + 1 << "======" << endl;
cout << "姓名:\t" << tongXunLu->person[index].name << endl;
cout << "地址:\t" << tongXunLu->person[index].address << endl;
cout << "性别:\t" << (tongXunLu->person[index].sex == true ? "男" : "女") << endl;
cout << "年龄:\t" << tongXunLu->person[index].age << endl;
cout << "号码:\t" << tongXunLu->person[index].number << endl;
}
void addPerson(TONGXUNLU * const tongXunLu)
{
string name;
int sex;
if (tongXunLu->length > MAX)
cout << "已经被填满了!" << endl;
else if (tongXunLu->length = 0)
cout << "这里什么也没有" << endl;
else
{
changePersonF(tongXunLu, tongXunLu->length);
tongXunLu->length++;
}
}
void showPersons(const TONGXUNLU * const tongXunLu)
{
for (int i = 0; i < tongXunLu->length; i++)
showPersonF(tongXunLu, i);
}
void deletePerson(TONGXUNLU * const tongXunLu)
{
string deleteName;
cout << "请输入要删除人的名字:";
cin >> deleteName;
int deleteIndex = findNameF(tongXunLu, deleteName);
if (deleteIndex != -1)
{
for (int i = deleteIndex; i < tongXunLu->length; i++)
tongXunLu->person[i] = tongXunLu->person[i + 1];
tongXunLu->length--;
cout << "删除成功!" << endl;
}
else
cout << "查无此人!" << endl;
}
void findPerson(const TONGXUNLU * const tongXunLu)
{
string findName;
cout << "请输入要查找联系人名字:";
cin >> findName;
int findIndex = findNameF(tongXunLu, findName);
if (findIndex != -1)
showPersonF(tongXunLu, findIndex);
else
cout << "查无此人!" << endl;
}
void changePerson(TONGXUNLU * const tongXunLu)
{
string changeName;
cout << "请输入要修改联系人名字:";
cin >> changeName;
int changeIndex = findNameF(tongXunLu, changeName);
if (changeIndex != -1)
changePersonF(tongXunLu, changeIndex);
else
cout << "查无此人!" << endl;
}
void showMenu()
{
cout << "***************************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 7、[清除屏幕] *****" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "***************************" << endl;
}
int main(int argc, char const *argv[])
{
TONGXUNLU tongxunlu;
int choose;
while (true)
{
showMenu();
cout << "请选择:";
cin >> choose;
switch (choose)
{
case 1:
addPerson(&tongxunlu);
break;
case 2:
showPersons(&tongxunlu);
break;
case 3:
deletePerson(&tongxunlu);
break;
case 4:
findPerson(&tongxunlu);
break;
case 5:
changePerson(&tongxunlu);
break;
case 6:
tongxunlu.length = 0;
break;
case 7:
system("cls");
break;
case 0:
return 0;
default:
continue;
}
}
return 0;
}
|