代码如下
#include <cstddef>
#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
struct Person
{
string name;
int gender;
int age;
string phone;
string addr;
};
struct AddressBook
{
struct Person personArray[MAX];
int size = 0;
};
// 鑿滃崟
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;
}
// 1.添加用户
void addPerson(AddressBook * abs)
{
if (abs ->size >= MAX)
{
cout << "通讯录已满" << endl;
return;
}
else
{
string name;
cout << "输入用户名:" << endl;
cin >> name;
abs -> personArray[abs->size].name = name;
int age;
cout << "输入年龄:" << endl;
cin >> age;
abs -> personArray[abs -> size].age = age;
int gender;
while (true)
{
cout << "输入性别 1-男 0-女:" << endl;
cin >> gender;
if (gender != 1 && gender != 0)
{
cout << "输入有误请重新输入" << endl;
}
else
{
abs->personArray[abs->size].gender = gender;
break;
}
}
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
abs -> personArray[abs->size].phone = phone;
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
abs -> personArray[abs -> size].addr = addr;
//数组长度加一
abs -> size++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
}
// 2.查看通讯录
void showPerson(AddressBook * addrbook)
{
if (addrbook -> size == 0)
{
cout << "通讯录为空" << endl;
}
else
{
for (int i = 0; i < addrbook->size; i++)
{
cout << "用户名 " << addrbook->personArray[i].name << "\t";
cout << "性别 " << (addrbook->personArray[i].gender == 1?"男":"女") << "\t";
cout << "年龄 " << addrbook->personArray[i].age << "\t";
cout << "电话 " << addrbook->personArray[i].phone << "\t";
cout << "地址 " << addrbook->personArray[i].addr << endl;
}
}
system("pause");
system("cls");
}
// 检查用户是否存在
int checkPerson(AddressBook * addr,string name)
{
for(int i =0;i<addr->size;i++){
if (name == addr->personArray[i].name) {
return i;
}
}
return -1;
}
// 3.删除用户
void deletePerson(AddressBook * abs)
{
string name;
cout << "请输入需要删除的用户名" << endl;
cin >> name;
int res = checkPerson(abs,name);
if (res != -1)
{
for (int i = res; i < abs->size; i++)
{
abs->personArray[i] = abs->personArray[i+1];
}
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
// 查找用户
void selectPerson(AddressBook * addr)
{
string personName;
cout << "请输入用户名:"<< endl;
cin >> personName;
int res = checkPerson(addr, personName);
if (res != -1)
{
cout << "用户名 :" << addr->personArray[res].name << "\t";
cout << "性别 :" << (addr->personArray[res].gender == 1?"男":"女") << "\t";
cout << "年龄 :" << addr->personArray[res].age << "\t";
cout << "电话 :" << addr->personArray[res].phone << "\t";
cout << "地址 :" << addr->personArray[res].addr << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
// 修改用户
void updatePerson(AddressBook * abs)
{
string name;
cout << "请输入需要修改的用户名:" << endl;
cin >> name;
int res = checkPerson(abs,name);
if (res == -1)
{
cout << "查无此人" << endl;
}
else
{
string personName;
cout << "请输入用户名:" << endl;
cin >> personName;
abs -> personArray[res].name = personName;
int age;
cout << "输入年龄:" << endl;
cin >> age;
abs -> personArray[res].age = age;
int gender;
while (true)
{
cout << "输入性别 1-男 0-女:" << endl;
cin >> gender;
if (gender != 1 && gender != 0)
{
cout << "输入有误请重新输入" << endl;
}
else
{
abs->personArray[res].gender = gender;
break;
}
}
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
abs -> personArray[res].phone = phone;
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
abs -> personArray[res].addr = addr;
cout << addr;
cout << "修改成功!" << endl;
system("pause");
system("cls");
}
}
// 清空通讯录
void clear(AddressBook * abs)
{
abs->size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
int main()
{
AddressBook addrBook;
Person *per;
int select = 0;
int flag = 1;
while (flag)
{
showMenu();
cin >> select;
switch (select)
{
case 1: addPerson(&addrBook);break;
case 2: showPerson(&addrBook);break;
case 3: deletePerson(&addrBook);break;
case 4: selectPerson(&addrBook);break;
case 5: updatePerson(&addrBook);break;
case 6: clear(&addrBook);break;
case 0:cout << "退出成功!" << endl;flag=0;break;
default:
break;
}
}
return 0;
}