1)
220529
#include<iostream>
using namespace std;
#include<string>
#define MAX 100
void showmeau() {//显示目录函数
cout << "\t欢迎使用通讯录" << endl;
cout << "\t\t请选择0-4" << endl;
cout << "\t1.添加人员" << endl;
cout << "\t2.显示人员" << endl;
cout << "\t3.查找人员" << endl;
cout << "\t4.删除人员" << endl;
cout << "\t0.退出" << endl;
}
void exitTuichu() {//退出通讯录函数
cout << "谢谢您的使用,成功退出" << endl;
return;
}
//定义人的结构
struct Person {
string name;
string eleDianhua;
};
struct Person personarr[MAX];//定义通讯录结构
int sizerenshu = 0;//记录存入的人数
void tianJiarenyuan() {//添加人员函数
if (sizerenshu <= MAX) {
string name1;
string eleDIanhua1;
Person per1;
cout << "请输入人的名字" << endl;
cin >> name1;
cout << "请输入人的电话" << endl;
cin >> eleDIanhua1;
per1.name = name1;
per1.eleDianhua = eleDIanhua1;
//int sizerenshu = sizeof(personarr) / sizeof(personarr[0]);
personarr[sizerenshu] = per1;
sizerenshu++;
cout << "添加成功" << endl;
}
else
{
cout << "人数已满,请做删减操作再添加" << endl;
return;
}
cout << endl;
}
void showPer() {//显示通讯录所有人函数
if (sizerenshu>0)
{
for (int i = 0;i < sizerenshu;i++) {
cout << "姓名\t" << personarr[i].name << "\t电话\t" << personarr[i].eleDianhua << endl;
}
}
else {
cout << "当前通讯录为空" << endl;
}
}
void Cazaoren() {//查找人函数,根据姓名查找
if (sizerenshu > 0) {
string name2;
cout << "请输入您要查找的人的名字" << endl;
cin >> name2;
for (int i = 0;i < sizerenshu;i++) {
if (personarr[i].name == name2) {
cout << "查找成功" << endl;
cout << "姓名\t" << personarr[i].name << "\t电话\t" << personarr[i].eleDianhua << endl;
}
else {
cout << "查找失败" << endl;
}
}
}
else {
cout << "当前通讯录为空" << endl;
}
}
void Sancu() {//人员删除函数
if (sizerenshu > 0) {
for (int i = 0;i < sizerenshu;i++) {
personarr[i] = {};
cout << "删除成功" << endl;
sizerenshu--;
}
}
else {
cout << "当前通讯录为空" << endl;
}
}
//测试函数
void text() {
while (true) {
showmeau();
int xuanZhe = 0;
int xuanze = 0;
cin >> xuanze;
xuanZhe = (int)xuanze;
cout << xuanze << endl;
cout << xuanZhe << endl;
switch (xuanZhe) {
case 1://1.添加人员
tianJiarenyuan();
system("pause");
system("cls");
break;
case 2://2.显示人员
showPer();
system("pause");
system("cls");
break;
case 3://3.查找人员
Cazaoren();
system("pause");
system("cls");
break;
case 4://4.删除人员
Sancu();
system("pause");
system("cls");
break;
case 0://0.退出
exitTuichu();
system("pause");
system("cls");
return;
}
}
}
//主函数
int main() {
text();
//int sizerenshu = sizeof(personarr) / sizeof(personarr[0]);
//cout << sizerenshu << endl;
system("pause");
}
//.通讯录
//1.添加人员
//2.显示人员
//3.查找人员
//4.删除人员
//0.退出
目的:
- 总结运用,
- 通讯录的实现比较简单
- switch语句对功能选择的选择,
- while语句对功能重复操作做的循环,
- for语句对通讯录数组成员遍历、添加、显示等功能的循环
- 功能函数分开写
- if语句对通讯录是否为空的判断
过程:
- 功能选项显示函数---实现退出函数---定义人员结构、结构数组---定义全局变量sizerenshu---其他功能函数
分析
- ①具有添加,显示,查找,删除等功能。未做修改的功能。将功能分开写在不同的函数当中,可以实现功能的具体化,应该是面向过程了。
- ②运用循环加清屏的操作能对界面实现的更加简洁,
- ③运用return直接退出函数,可以退出循环一直为真的结果
- ④运用sizerenshu的全局变量对数组中存放的成员进行合理统计,方便和丰富了其他功能
目前已知问题:
- ①未对输入的姓名,电话是否合法判断
- ②未实现拨号,查看拨号等功能