编辑器vs2019
代码如下:
#include <iostream>
using namespace std;
#define MAX 1000 // 最大人数
// 联系人信息结构体
struct Person
{
string m_name;
// 性别 1 男 2 女
int m_sex;
int m_age;
string m_phone;
string m_addr;
};
// 通讯录结构体
struct Addressbooks
{
struct Person personArray[MAX]; // 通讯录中保存的联系人数组
int m_size; //通讯录中人员个数
};
// 1、添加联系人
void addPerson(struct Addressbooks * abs)
{
// 判断通讯录是否满人,满人就不用加了
if (abs->m_size == MAX)
{
cout << "满2000人,不能在加了" << endl;
return ;
}
else
{
string name;
cout << "名字:" << endl;
cin >> name;
abs->personArray[abs->m_size].m_name = name;
int sex;
cout << "性别:1 男 ; 2 女" << endl;
cin >> sex;
abs->personArray[abs->m_size].m_sex = sex;
int age;
cout << "年龄:" << endl;
cin >> age;
abs->personArray[abs->m_size].m_age = age;
string phone;
cout << "手机号:" << endl;
cin >> phone;
abs->personArray[abs->m_size].m_phone = phone;
string addr;
cout << "地址:" << endl;
cin >> addr;
abs->personArray[abs->m_size].m_addr = addr;
cout << "添加"<< name << "成功!" << endl;
// 更新通讯录人数
abs->m_size++;
system("pause"); // 请按任意键继续
system("cls"); // 清屏操作
}
}
// 2、显示联系人
void showPerson(struct Addressbooks* abs)
{
if (abs->m_size == 0)
{
cout << "没有人,当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名:" << abs->personArray[i].m_name
<< "\t性别:" << ( abs->personArray[i].m_sex == 1 ? "男" : "女" )
<< "\t年龄:" << abs->personArray[i].m_age
<< "\t电话:" << abs->personArray[i].m_phone
<< "\t地址:" << abs->personArray[i].m_addr << endl;
}
}
system("pause");
system("cls");
}
// 检测联系人是否存在,如存在,返回索引位置,不存在 返回-1
int isExit(struct Addressbooks *abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
// 找到用户输入的名字
if (name == abs->personArray[i].m_name)
{
return i;
}
}
return -1;
}
// 3、删除指定联系人
void deletePerson(struct Addressbooks* abs)
{
cout << "请输入要删除的联系人: " << endl;
string name;
cin >> name;
// 判断用户输入名字是否存在
int res = isExit(abs, name);
if (res == -1)
{
cout << "查无此人" << endl;
}
else
{
// 名字存在,数据前移
for (int i = res; i < abs->m_size; i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_size--; // 更新通讯录中的人员数
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
// 4、查找联系人
void findPerson(struct Addressbooks * abs)
{
cout << "请输入要查找的联系人信息: " << endl;
string name;
cin >> name;
// 判断用户输入名字是否存在
int res = isExit(abs, name);
if (res == -1)
{
cout << "查无此人" << endl;
}
else
{
// 名字存在,展现信息
cout << "姓名:" << abs->personArray[res].m_name <<
"\t性别:" << abs->personArray[res].m_sex <<
"\t年龄:"<< abs->personArray[res].m_age <<
"\t号码:" << abs->personArray[res].m_phone <<
"\t地址:" << abs->personArray[res].m_addr << endl;
}
system("pause");
system("cls");
}
//5、 修改联系人
void updatePerson(struct Addressbooks* abs)
{
cout << "请输入要删除的联系人: " << endl;
string name;
cin >> name;
// 判断用户输入名字是否存在
int res = isExit(abs, name);
if (res == -1)
{
cout << "查无此人" << endl;
}
else
{
// 名字存在,修改联系人
string name;
int sex;
int age;
string phone, addr;
cout << "输入名字:" << endl;
cin >> name;
cout << "输入性别:1 男 2 女" << endl;
cin >> sex;
cout << "输入年龄:" << endl;
cin >> age;
cout << "输入电话:" << endl;
cin >> phone;
cout << "输入地址:" << endl;
cin >> addr;
abs->personArray[res].m_name = name;
abs->personArray[res].m_sex = sex;
abs->personArray[res].m_age = age;
abs->personArray[res].m_phone = phone;
abs->personArray[res].m_addr = addr;
cout << "修改成功" << endl;
}
system("pause");
system("cls");
}
// 6、清空联系人
void cleanPerson(struct Addressbooks* abs)
{
abs->m_size = 0;
cout << "已清空" << endl;
system("pause");
system("cls");
}
// 菜单界面
void showMenu()
{
cout << "**********************" << endl;
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;
}
int main()
{
// 创建通讯录结构体变量
struct Addressbooks abs;
// 初始化通讯录中当前人员个数
abs.m_size = 0;
while (true)
{
showMenu();
int num; // 用户选择
cout << "请输入选择的操作:" << endl;
cin >> num;
switch (num)
{
case 0: // 0、退出通讯录
printf("正在退出!\n");
return 0;
case 1: // 1、添加联系人
addPerson(&abs); // 利用地址传递,可以修饰实参
break;
case 2: // 2、显示联系人
//int len = abs.m_size;
showPerson(&abs);
break;
case 3: // 3、删除联系人
deletePerson(&abs);
break;
case 4: // 4、查找联系人
findPerson(&abs);
break;
case 5: // 5、修改联系人
updatePerson(&abs);
break;
case 6: // 6、清空联系人
cleanPerson(&abs);
break;
default:
break;
}
}
// system("pause");
return 0;
}
标签:cout,管理系统,res,C++,abs,通讯录,personArray,size,name
From: https://www.cnblogs.com/code3/p/17304072.html