评论

收藏

[C++] c++通讯录实战(2)

编程语言 编程语言 发布于:2021-08-09 15:07 | 阅读数:522 | 评论:0

代码如下:

#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
//设计联系人结构体
struct Person {
//姓名
string name;
//性别
int sex;
//年龄
int age;
//电话
string phone;
//住址
string addr;
};
//设计通讯录结构体
struct Addressbooks
{
//通讯录中保存联系人数组
struct Person personArray[MAX];
//通讯录中当前记录联系人个数
int 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;

}
//添加联系人
void addPerson(struct Addressbooks *abs)
{
//判断通讯录是不是已经满了
if (abs->size == MAX)
{
cout << "通讯录已满,无法添加" << endl;
return;
}
else
{

cout << "请输入该添加人的姓名:" << endl;
string name;
cin >> name;
abs->personArray[abs->size].name = name;
cout << "请输入该添加人的性别(0表示女生/1表示男生)" << endl;
int sex=0;
while (true)
{
cin >> sex;
if (sex == 0 || sex == 1)
{
abs->personArray[abs->size].sex = sex;
break;
}
else
{
cout << "输出错误,请重新输入" << endl;
}
}

cout << "请输入该添加人的年龄" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age >= 0 && age <= 200)
{
abs->personArray[abs->size].age = age;
break;
}
else
{
cout << "年龄不合理" << endl;
}
}
cout << "请输入该添加人的电话号码" << endl;
string phone;
cin >> phone;
abs->personArray[abs->size].phone = phone;
cout << "请输入该添加人的地址" << endl;
string addr;
cin >> addr;
abs->personArray[abs->size].addr = addr;
//更新通讯录中的人数
abs->size++;
//
cout << "添加完成" << endl;
system("pause");//按任意键继续
system("cls");

}

}
//显示联系人功能
void showPerson(struct Addressbooks *abs)
{
if (abs->size == 0)
{
cout << "通讯录为空" << endl;
}
else
{
cout << "您一共有" << abs->size << "个联系人" << endl;
for (int i = 0; i < abs->size; i++)
{
cout << "名字:" << abs->personArray[i].name << "  \t" << "性别:" << abs->personArray[i].sex << "  \t" << 
"年龄:" << abs->personArray[i].age << "  \t" <<"电话号码:" << abs->personArray[i].phone << "  \t" << "地址:" << abs->personArray[i].addr << "  \t" << endl;
}
}
system("pause");
system("cls");
}
//定义函数,检测联系人是否存在,如果存在返回下标,不存在返回-1
int isExist(Addressbooks *abs,string name)
{
for (int i = 0; i <abs->size; i++)
{
if (abs->personArray[i].name == name)
{
return i;
}

}
return -1;
system("pause");
system("cls");
}
//删除联系人
void delPerson(struct Addressbooks *abs)
{

cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
int ret =isExist(abs, name);
if (ret != -1)
{
for (int i = ret; ret<abs->size ; i++)
{
//查到此人,要进行删除
for (int i = ret; ret< abs->size; i++)
{
//数据向前移动
abs->personArray[i] = abs->personArray[i + 1];
}
}
//更新通讯录中的人员数
abs->size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人";
}
system("pause");
system("cls");
}
//查找联系人
void findPerson(struct Addressbooks* abs)
{
cout << "请输入要查找人的姓名" << endl;
string name;
cin >> name;
int res = isExist(abs, name);
if (res == -1)
{
cout << "查无此人" << endl;
}
else {
cout << "查到了此人" << endl;
cout << "它的下标为:" << res << endl;
cout << abs->personArray[res].name <<" " << abs->personArray[res].addr<<" " << abs->personArray[res].age <<
" " << abs->personArray->phone <<" " << abs->personArray->sex << endl;
}
}
//修改联系人
void updatePerson(struct Addressbooks* abs)
{
string name;
cout << "请输入要修改人的姓名" << endl;
cin >> name;
int res = isExist(abs, name);
if (res != -1)
{
cout << "现在开始修改了" << endl;
cout << "请输入修改后的姓名" << endl;
string name;
cin >> name;
abs->personArray[res].name = name;
cout << "请输入修改后的性别" << endl;
int sex=0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 0)
{
abs->personArray[res].sex = sex;
break;
}
else
{
cout << "性别设置不合理,请重新输入" << endl;
}
}
cout << "请修改后的输入年龄" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age >= 0 && age <= 200)
{
abs->personArray[res].age = age;
cout << "修改年龄成功" << endl;
break;
}
else
{
cout << "年龄不合理" << endl;
}
}
cout << "请输入修改后的电话" << endl;
string phone;
cin >> phone;
abs->personArray[res].phone = phone;
cout << "请输入修改后的地址" << endl;
string addr;
cin >> addr;
abs->personArray[res].addr = addr;



}
else
{
cout << "通讯录中无此人" << endl;
}
cout << "修改成功" << endl;
}
//清空联系人
void cleanPerson(struct Addressbooks *abs)
{
//做逻辑上的删除
abs->size = 0;
cout << "清除成功" << endl;
}
int main()
{
//创建通讯录实例
struct Addressbooks abs;
//初始化通讯录中的人员
abs.size = 0;
//创建用户选择的变量
int select = 0;
bool loop = true;

do
{
showMenu();
cout << "请输入您的选择数字(0-6)" << endl;
cin >> select;
switch (select)
{
//1.添加联系人
case 1:
addPerson(&abs);
break;
//2.显示联系人
case 2:
 showPerson(&abs);
break;
//3.删除联系人
case 3:
  delPerson(&abs);
break;
case 4:
//4.查找联系人
findPerson(&abs);
break;
case 5:
//5.修改联系人
updatePerson(&abs);
break;
case 6:
//6.清空联系人
cleanPerson(&abs);
break;
//0.退出通讯录
case 0:
cout << "您已退出通讯录管理系统!" << endl;
loop = false;
break;
default:
break;
}
} while (loop);
system("pause");
return 0;
}

关注下面的标签,发现更多相似文章