#include<iostream>
#include<string>
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;
};
//菜单显示
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(Addressbooks *abs) //用指针接收地址
{
if (abs->m_Size == MAX)
{
cout << "通讯录已满,不能添加" << endl;
return;
}
else
{
//姓名
string name;
cout << "请输入姓名" << endl;
cin >> name;
//要将姓名信息添加到数组,而数组嵌套了另一个结构体;personArray[i]中的i要随着人数的每次添加而增大;
abs->personArray[abs->m_Size].m_Name = name;
//性别
int sex=0;
cout << "请输入性别: (1--男,2--女)" << endl;
//输入错误可以循环输入,直到正确
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "输入有误,重新输入" << endl;
}
//年龄
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;
//更新当前通讯录人员数量
abs->m_Size++;
cout << "添加成功" << endl;
//暂停+清屏
system("pause");
system("cls");
}
}
// 2.全部显示联系人
void showPerson(Addressbooks *abs)
{
//如果有,则全部显示
//没有,则提示为空
//注意两点:1、一行显示一个人信息 2、性别的显示
if (abs->m_Size == 0)
{
cout << "通讯录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名: " << abs->personArray[i].m_Name << '\t'; //用制表符取代nedl,不换行
cout << "性别: " <<(abs->personArray[i].m_Sex == 1 ? "男" : "女" ) << '\t'; //此处修改,三目
cout << "年龄: " << abs->personArray[i].m_Age << '\t';
cout << "电话: " << abs->personArray[i].m_Phone << '\t';
cout << "地址: " << abs->personArray[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
//判断是否存在
int isExist(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 delePerson(Addressbooks *abs)
{
string name;
cout << "输入要删除的联系人" << endl;
cin >> name;
int res = isExist(abs, name);
if (res != -1)
{
for (int i = res; i < abs->m_Size - 1; i++) //从当前下标开始前移
{
abs->personArray[i] = abs->personArray[i + 1]; //本质是将后者前移,覆盖掉前者
}
cout << "删除成功" << endl;
abs->m_Size--; //更新联系人数量,易漏
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//4.查找联系人
void findPerson(Addressbooks *abs)
{
string name;
cout << "请输入要查找的联系人" << endl;
cin >> name;
int res = isExist(abs, name);
if (res != -1)
{
cout << "姓名: " << abs->personArray[res].m_Name << '\t';
cout << "性别: " << (abs->personArray[res].m_Sex == 1 ? "男" : "女") << '\t';
cout << "年龄: " << abs->personArray[res].m_Age << '\t';
cout << "电话: " << abs->personArray[res].m_Phone << '\t';
cout << "地址: " << abs->personArray[res].m_Addr << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//5.修改联系人
void modifyPerson(Addressbooks *abs)
{
string name;
cout << "请输入要修改的联系人" << endl;
cin >> name;
int res = isExist(abs, name);
if (res != -1)
{
//修改姓名
cout << "是否需要修改姓名? (y--修改 n--不修改)" << endl;
char ch;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
cout << "请重新输入姓名:" << endl;
cin >> name;
abs->personArray[res].m_Name = name;
}
//修改性别
int sex;
cout << "是否需要修改性别? (y--修改 n--不修改)" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
while (1)
{
cout << "请输入性别: (1--男 2--女)" << endl;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[res].m_Sex = sex;
break;
}
else
{
cout << "输入有误,重新输入" << endl;
}
}
}
//修改年龄
cout << "是否需要修改年龄? (y--修改 n--不修改)" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
int age;
cout << "请重新输入年龄:" << endl;
cin >> age;
abs->personArray[res].m_Age = age;
}
//修改电话
cout << "是否需要修改电话? (y--修改 n--不修改)" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
string phone;
cout << "请重新输入电话:" << endl;
cin >> phone;
abs->personArray[res].m_Phone = phone;
}
//修改地址
cout << "是否需要修改地址? (y--修改 n--不修改)" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
string addr;
cout << "请重新输入地址:" << endl;
cin >> addr;
abs->personArray[res].m_Addr = addr;
}
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//6.清空联系人
void cleanPerson(Addressbooks *abs)
{
char ch;
cout << "是否确认清空? 是--y 否--n" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
abs->m_Size = 0;
cout << "通讯录已清空" << endl;
}
system("pause");
system("cls");
}
//main函数
int main()
{
//初始化整个通讯录结构体
struct Addressbooks abs;
//初始化结构体当前人员数量
abs.m_Size = 0;
//用户默认输入
int select = 0;
do
{
//菜单显示
showMenu();
cin >> select;
switch (select)
{
case 1: //添加
addPerson(&abs); //调用添加函数,值传递不能改变变量,通过地址传递
break;
case 2: //显示
showPerson(&abs);
break;
case 3: //删除
delePerson(&abs);
break;
case 4: //查找
findPerson(&abs);
break;
case 5: //修改
modifyPerson(&abs);
break;
case 6: //清空
cleanPerson(&abs);
break;
case 0: //退出
cout << "欢迎下次使用" << endl;
system("pause");
return 0; ///退出整个程序
default:
break;
}
} while (1);
system("pause");
}
代码流程如下
个人信息结构体:存储个人信息
通讯录结构体:以数组形式封装个人信息+人员数量
就像手机通讯录,打开先显示每个人头像以及总体数量,点击个人头像,才能看到具体的信息
用到的知识点:结构体+数组+函数调用+switch语句(暂没有用链表,链表应该会比数组方便)