需求: 1.添加联系人:向通讯录中添加新人,包括姓名、性别、年龄、联系电话、家庭住址,最多纪录1000人 2.显示联系人:显示已保存的全部联系人 3.删除联系人:按照姓名可以删除指定联系人 4.查找联系人:按照姓名查看指定联系人的全部信息 5.修改联系人:按照姓名修改联系人的信息 6.清空联系人:清空通讯录中所有的联系人信息 7.退出通讯录:推出当前使用的通讯录
练习内容:
函数的定义和调用、数组、地址传递和值传递、基本判断和循环
代码:
#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
//显示菜单界面
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;
}
//设计联系人结构体
struct Person
{
string m_Name; //姓名
int m_Sex; //性别
int m_Age; //年龄
string m_Phone; //电话号码
string m_Addr; //家庭住址
};
//设计通讯录结构体
struct Addressbooks
{
//通讯录中保存的联系人的数组
struct Person personArry[ MAX ];
//通讯录中当前联系人个数
int m_Size;
};
//添加联系人
void addPerson(Addressbooks* abs)
{
//判断通讯录是否已满
if (abs->m_Size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
}
else
{
//添加具体联系人
//姓名
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->personArry[abs->m_Size].m_Name = name;
//性别
int sex;
cout << "请输入性别: " << endl;
cout << "1--男" << endl << "2--女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArry[abs->m_Size].m_Sex = sex;
break;
}
else
{
cout << "输入错误,请重新输入!" << endl;
}
}
//年龄
int age;
cout << "请输入年龄:" << endl;
while (true)
{
cin >> age;
if (age > 0)
{
abs->personArry[abs->m_Size].m_Age = age;
break;
}
else
{
cout << "输入错误,请重新输入!" << endl;
continue;
}
}
//电话号码
string phone;
cout << "请输入联系电话:" << endl;
cin >> phone;
abs->personArry[abs->m_Size].m_Phone = phone;
//家庭住址
string address;
cout << "请输入家庭住址:" << endl;
cin >> address;
abs->personArry[abs->m_Size].m_Addr = address;
//更新通讯录人数
abs->m_Size++;
system("cls");
//cout << "****************" << endl;
cout << "添加成功" << endl;
//cout << "****************" << endl;
system("pause"); //请按任意键继续
system("cls"); //清屏
}
}
//显示联系人
void showPreson(Addressbooks * abs)
{
system("cls");
if (abs->m_Size == 0)
{
cout << "当前无联系人" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名:" << abs->personArry[i].m_Name << "\t";
cout << "性别:" << (abs->personArry[i].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArry[i].m_Age << "\t";
cout << "电话:" << abs->personArry[i].m_Phone << "\t";
cout << "住址:" << abs->personArry[i].m_Addr << "\t";
cout << endl;
}
}
system("pause");
system("cls");
}
//检测联系人是否存在
int findPerson(Addressbooks* abs, string name)
{
//存在返回联系人下标,不存在返回-1
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArry[i].m_Name == name)
{
return i;
}
}
return -1;
}
//删除联系人
void delPerson(Addressbooks * abs)
{
cout << "请输入要删除的联系人:" << endl;
string name;
cin >> name;
int ret = findPerson(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "找到此人" << endl;
//showPreson(abs);
cout << "姓名:" << abs->personArry[ret].m_Name << "\t";
cout << "性别:" << (abs->personArry[ret].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArry[ret].m_Age << "\t";
cout << "电话:" << abs->personArry[ret].m_Phone << "\t";
cout << "住址:" << abs->personArry[ret].m_Addr << "\t";
cout << endl;
cout << "确认要删除吗?" << endl;
int check = 0;
cout << "1--确认" << endl;
cout << "2--取消" << endl;
cin >> check;
if (check != 1)
{
system("cls");
}
else if (check == 1)
{
for (int j = ret; j < abs->m_Size; j++)
{
//数据前移
abs->personArry[j] = abs->personArry[j + 1];
}
abs->m_Size--; //更新通讯录中的人数
system("cls");
cout << "删除成功" << endl;
system("pause");
system("cls");
}
}
}
//查找联系人
void seekPerson(Addressbooks* abs)
{
cout << "请输入要查找的联系人: " << endl;
string name;
cin >> name;
system("cls");
//判断输入的联系人是否存在
int ret =findPerson(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "找到此人" << endl;
cout << "姓名:" << abs->personArry[ret].m_Name << "\t";
cout << "性别:" << (abs->personArry[ret].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArry[ret].m_Age << "\t";
cout << "电话:" << abs->personArry[ret].m_Phone << "\t";
cout << "住址:" << abs->personArry[ret].m_Addr << "\t";
cout << endl;
}
system("pause");
system("cls");
}
//修改联系人
void modifyPerson(Addressbooks* abs)
{
cout << "请输入要修改的联系人:" << endl;
string name;
cin >> name;
system("cls");
//判断输入的联系人是否存在
int ret = findPerson(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "找到此人" << endl;
cout << "姓名:" << abs->personArry[ret].m_Name << "\t";
cout << "性别:" << (abs->personArry[ret].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArry[ret].m_Age << "\t";
cout << "电话:" << abs->personArry[ret].m_Phone << "\t";
cout << "住址:" << abs->personArry[ret].m_Addr << "\t";
cout << endl;
cout << "请选择要修改的项目:" << endl;
cout << "1--姓名" << endl << "2--性别" << endl << "3--年龄" << endl
<< "4--电话" << endl << "5--住址" << endl;
int select = 0;
cin >> select;
switch (select)
{
case 1: //修改姓名
{
string name;
//string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->personArry[ret].m_Name = name;
system("cls");
cout << "修改成功" << endl;
}
break;
case 2:
{
int sex;
cout << "请输入性别: " << endl;
cout << "1--男" << endl << "2--女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArry[ret].m_Sex = sex;
break;
}
else
{
cout << "输入错误,请重新输入!" << endl;
}
}
system("cls");
cout << "修改成功" << endl;
}
break;
case 3:
{
int age;
cout << "请输入年龄:" << endl;
while (true)
{
cin >> age;
if (age > 0)
{
abs->personArry[ret].m_Age = age;
break;
}
else
{
cout << "输入错误,请重新输入!" << endl;
continue;
}
}
system("cls");
cout << "修改成功" << endl;
}
break;
case 4:
{
string phone;
cout << "请输入联系电话:" << endl;
cin >> phone;
abs->personArry[ret].m_Phone = phone;
system("cls");
cout << "修改成功" << endl;
}
break;
case 5:
{
string address;
cout << "请输入家庭住址:" << endl;
cin >> address;
abs->personArry[ret].m_Addr = address;
system("cls");
cout << "修改成功" << endl;
}
break;
}
system("pause");
system("cls");
}
}
//清空联系人
void cleanPerson(Addressbooks* abs)
{
cout << "确认要删除所有联系人吗?" << endl;
int select = 0;
cout << "1--确认" << endl << "2--取消" << endl;
cin >> select;
if (select != 1)
{
system("cls");
}
else
{
system("cls");
abs->m_Size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
}
int main()
{
//创建一个通讯录结构体变量
Addressbooks abs;
abs.m_Size = 0;
int select = 0; //创建用户输入的变量
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //添加联系人
addPerson(&abs); //使用地址传递,可以修饰实参
break;
case 2: //显示联系人
showPreson(&abs);
break;
case 3: //删除联系人
delPerson(&abs);
break;
case 4: //查找联系人
seekPerson(&abs);
break;
case 5: //修改联系人
modifyPerson(&abs);
break;
case 6: //清空联系人
cleanPerson(&abs);
break;
case 0: //退出通讯录
system("cls");
cout << "欢迎下次使用 :-)" << endl;
system("pause");
return 0;
break;
}
}
//菜单调用
system("pause");
return 0;
}
|