【学校实验】通讯录管理系统C++
作者:互联网
通讯录管理系统
一、代码
//封装函数显示该界面,在main函数中调用这个封装函数
#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
void showMenu();
//设计联系人的结构体 名字 性别 年龄 电话 住址
struct People {
//创建名字
string Pname;
//创建性别 输入1为男 输入2为女
int Psex;
//创建年龄
int Page;
//创建手机号码
string Ptele;
//创建地址
string Pdress;
};
//设计通讯录的结构体 通讯录的容量为1000
struct Abooks {
//通讯录中保存的联系人的数组
struct People Parry[MAX];
//记录通讯录中的总人数
int Asize;
};
//1.功能1,添加联系人
void Addpeople(Abooks * abs)
{
//判断通讯录名额是否已满
if (abs->Asize == MAX)
{
cout << "通讯录已满,录入失败" << endl;
}
else
{
//提示输入姓名
string name;
cout << "请输入名字:" << endl;
cin >> name;
abs->Parry[abs->Asize].Pname = name;
cout << "成功..." << endl;
//提示输入性别 1男2女
cout << "请输入性别:" << endl;
cout << "输入1 ---- 为男生 输入2 ---- 为女生" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->Parry[abs->Asize].Psex = sex;
cout << "成功..." << endl;
break;
}
cout << "输入错误,请重新输入..." << endl;
}
//提示输入年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->Parry[abs->Asize].Page = age;
cout << "成功..." << endl;
//提示输入电话
cout << "请输入电话:" << endl;
string tele ;
cin >> tele;
abs->Parry[abs->Asize].Ptele = tele;
cout << "成功..." << endl;
/*while (true)
{
cin >> tele;
if (tele % 100000000000 > 10000000000)
{
abs->Parry[abs->Asize].Ptele = tele;
cout << "成功..." << endl;
break;
}
cout << "电话号码错误,请重新输入..." << endl;
}*/
//提示输入地址
cout << "请输入地址:" << endl;
string dress;
cin >> dress;
abs->Parry[abs->Asize].Pdress = dress;
cout << "成功..." << endl;
abs->Asize++;
cout << "添加成功..." << endl;
system("pause");//按任意键继续
system("cls");//清屏操作
}
}
//功能2.显示联系人
void Showpeople(Abooks* abs)
{
//判断通讯录人数是否为0,为0显示未添加联系人
if (abs->Asize == 0)
{
cout << "当前通讯录无联系人.." << endl
<< "请添加联系人.." << endl;
}
else
{
//通讯录不为0时,显示联系人
for (int i = 0; i < abs->Asize; i++)
{
cout << "姓名为:" << abs->Parry[i].Pname << "\t"
<< "性别为:" << (abs->Parry[i].Psex == 1 ? "男" : "女") << "\t"
<< "年龄为:" << abs->Parry[i].Page << "\t"
<< "电话号码为:"<< abs->Parry[i].Ptele <<"\t"
<< " 地址为:" << abs->Parry[i].Pdress <<endl<<endl;
}
}
system("pause");//按任意键继续
system("cls");//清屏操作
}
//设计判断联系人是否存在的函数
int Judge(Abooks* abs, string name)
{
for (int i = 0; i < abs->Asize; i++)
{
if (abs->Parry[i].Pname == name)
{
return i;
}
else return -1;
}
}
//功能3. 删除连续人
void DeletePeople(Abooks *abs )
{
cout << "请输入要删除的联系人.." << endl;
string name;
cin >> name;
int ret = Judge(abs, name);
if (ret != -1) {
//找到此人,进行删除
for (int i = ret; i < abs->Asize; i++)
{
//每个数据都前移,覆盖住要删除的哪个数据
abs->Parry[i] = abs->Parry[i + 1];
}
abs->Asize--;
cout << "删除成功.." << endl;
}
else {
cout << "查无此人,请重新输入.." << endl;
}
system("pause");//按任意键继续
system("cls");//清屏操作
}
//功能四的输出函数
void show(Abooks* abs, int ret) {
cout << "姓名为:" << abs->Parry[ret].Pname << "\t"
<< "性别为:" << (abs->Parry[ret].Psex == 1 ? "男" : "女") << "\t"
<< "年龄为:" << abs->Parry[ret].Page << "\t"
<< "电话号码为:" << abs->Parry[ret].Ptele << "\t"
<< "地址为:" << abs->Parry[ret].Pdress << endl << endl;
}
//功能四 查找联系人
void FindPeople(Abooks* abs) {
//输入名字,与通讯录数组里的名字相配对 成功就输出 否则 显示查无此人
cout << "请输入您要查找的名字.." << endl;
string name;
cin >> name;
int ret = Judge(abs, name);
if (ret != -1)
{
show(abs, ret);
}
else
{
cout << "查无此人,请查正后再输.." << endl;
}
system("pause");
system("cls");
}
//功能五的 修改函数
void Change(Abooks* abs, int ret)
{
cout << "请输入新的信息" << endl;
string name;
cout << "请输入名字:" << endl;
cin >> name;
abs->Parry[ret].Pname = name;
cout << "成功..." << endl;
cout << "请输入性别:" << endl;
cout << "输入1 ---- 为男生 输入2 ---- 为女生" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->Parry[ret].Psex = sex;
cout << "成功..." << endl;
break;
}
cout << "输入错误,请重新输入..." << endl;
}
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->Parry[ret].Page = age;
cout << "成功..." << endl;
cout << "请输入电话:" << endl;
string tele;
cin >> tele;
abs->Parry[ret].Ptele = tele;
cout << "成功..." << endl;
cout << "请输入地址:" << endl;
string dress;
cin >> dress;
abs->Parry[ret].Pdress = dress;
cout << "成功..." << endl << endl;
}
//功能五 修改联系人
void ChangePeople(Abooks* abs)
{
cout << "请输入您要修改的联系人" << endl;
string name;
cin >> name;
int ret = Judge(abs, name);
if (ret != -1)
{
Change(abs, ret);
cout << "修改后的信息为:" << endl;
show(abs, ret);
}
else
{
cout<< "查无此人,请查正后再输.." << endl;
}
system("pause");
system("cls");
}
//功能六 清空通讯录
void DeleteAll(Abooks* abs)
{
cout << "确定要清空通讯录吗?" << endl;
cout << "确定 " << "\t" << "取消"<<endl;
string chance;
cin >> chance;
if (chance == "确定")
{
abs->Asize = 0;
cout << "删除成功";
system("pause");
system("cls");
}
else if (chance == "取消")
{
system("pause");
system("cls");
}
else cout << "输入错误,请重新输入。" << endl;
}
//菜单界面
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;
}
int main() {
//创建一个通讯录结构体变量
Abooks abs;
//初始化通讯录的人数
abs.Asize = 0;
//用户输入选项
int select = 0;
while (true) {//建立一个类似死循环
showMenu();
cin >> select;
switch (select) {
case 1:// 添加联系人
Addpeople(&abs);
break;
case 2:// 显示联系人
Showpeople(&abs);
break;
case 3:// 删除联系人
DeletePeople(&abs);
break;
case 4:// 查找联系人
FindPeople(&abs);
break;
case 5:// 修改联系人
ChangePeople(&abs);
break;
case 6:// 清空联系人
DeleteAll(&abs);
break;
case 0:// 退出通讯录
cout << "欢迎下次使用通讯录" << endl;
system("pause");//请按任意键继续
return 0;
default:
break;
}
}
system("pause");
return 0;
}
二、总结
system(“cls”); 清屏操作会让程序运行起来更加美观
弄清指针关系
标签:cout,管理系统,C++,Parry,abs,通讯录,Asize,ret 来源: https://blog.csdn.net/maikotom/article/details/120138698