目录
主菜单界面
注释
while是为了让循环的界面在除了选0以外的选项均出现主菜单界面
代码展示
//主菜单
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;
}
退出通讯录
注释
通过system("pause");结束,退出系统
添加联系人
注释
上限为1000,添加姓名,联系电话,年龄,性别,家庭住址
设计联系人结构体:姓名,年龄,性别,联系电话(要用字符串表示),家庭住址
设计通讯录的结构体:上限1000,存放联系人数组,记录当前已有的联系人的个数
在main函数中创建通讯录; 添加函数用的是地址传递,否则不能改变实参;添加完后要更新联系人人数
代码展示
//添加联系人
//联系人结构体
struct person {
string name;
int age;
string sex;
string phonenum; //连续数字要用字符串表示
string address;
};
struct books {
struct person Personarr[MAX]; //通讯录保存的联系人数组
int size; //记录通讯录人数
};
//添加函数
void add(books* abs) {
if (abs->size > 1000) {
cout << "添加人数已达上限" << endl;
return;
}
else {
cout << "请输入姓名:";
cin >> abs->Personarr[abs->size].name;
cout << "请输入正确性别:" << endl;
int sex = 0;
//如果输入的是1或者2的话,才可跳出循环,否则一直循环
while (true) {
cout << "1---男" << endl;
cout << "2---女" << endl;
cin >> sex;
if (sex == 1){
abs->Personarr[abs->size].sex = "男";
break;
}
else if (sex == 2) {
abs->Personarr[abs->size].sex = "女";
break;
}
else {
cout << "输入有误,请重新输入" << endl;
}
}
cout << "请输入年龄:";
cin >> abs->Personarr[abs->size].age;
cout << "请输入联系电话:";
cin >> abs->Personarr[abs->size].phonenum;
cout << "请输入家庭住址:";
cin >> abs->Personarr[abs->size].address;
}
//更新联系人的人数:当前记录数要自加1
abs->size++;
cout << "添加成功" << endl;
//清屏操作
system("pause");
system("cls");
}
显示联系人
注释
判断当前人数是否为空,为空就现实通讯录为空,否则再输入。
代码展示
//显示已添加联系人
void Showperson(books* abs) {
//判断当前人数是否为空,为空就现实通讯录为空,否则再输入
if (abs->size == 0) {
cout << "通讯录为空" << endl;
}
else
{
int i;
for (i = 0; i < abs->size; i++) {
cout << "姓名:" << abs->Personarr[i].name << "\t";
cout << "性别:" << abs->Personarr[i].sex << "\t";
cout << "年龄:" << abs->Personarr[i].age << "\t";
cout << "联系电话:" << abs->Personarr[i].phonenum << "\t";
cout << "家庭住址:" << abs->Personarr[i].address << endl;
}
cout << endl;
}
system("pause");
system("cls");
}
修改联系人
注释
1、查找函数:判断是否存在,存在,返回通讯录的位置,删除;不存在,显示查无此人,返回-1(同删除的检测函数)
2、修改函数:检测函数传参数i。 将当前数组下的联系人的所有信息修改
代码展示
//修改联系人
void modifyperson(books* abs) {
string name;
cout << "查找联系人的信息:";
cin >> name;
int ret = IsExit(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else
{
cout << "请输入要修改的信息:" << endl;
cout << "请输入姓名:";
cin >> abs->Personarr[abs->size].name;
cout << "请输入正确性别:" << endl;
int sex = 0;
//如果输入的是1或者2的话,才可跳出循环,否则一直循环
while (true) {
cout << "1---男" << endl;
cout << "2---女" << endl;
cin >> sex;
if (sex == 1) {
abs->Personarr[ret].sex = "男";
break;
}
else if (sex == 2) {
abs->Personarr[ret].sex = "女";
break;
}
else {
cout << "输入有误,请重新输入" << endl;
}
}
cout << "请输入年龄:";
cin >> abs->Personarr[ret].age;
cout << "请输入联系电话:";
cin >> abs->Personarr[ret].phonenum;
cout << "请输入家庭住址:";
cin >> abs->Personarr[ret].address;
}
system("pause");
system("cls");
}
删除联系人
注释
1、检测函数:判断是否存在,存在,返回通讯录的位置,删除;不存在,显示查无此人,返回-1
2、删除函数:检测函数传参数i。 删除的同时,通过将数组下标i后的联系人前移即可,且人数减1
代码展示
//检测联系人是否存在:判断是否存在,存在,返回通讯录的位置,删除;不存在,显示查无此人,并且返回-1
//参数一:通讯录;参数二:对比姓名
int IsExit(books* abs,string name) {
int i;
for (i = 0; i < abs->size; i++) {
if (abs->Personarr[i].name == name) {
cout << "找到此人!" << endl;
return i;
}
}
return -1;
}
//删除联系人
void deleteperson(books *abs) {
string name;
cout << "请输入要删除的名字:";
cin >> name;
int ret = IsExit(abs, name); //int ret = IsExit(&abs, name);为什么在这不用&?
//因为本地在传参数时候,abs已经加了*,在下面使用时,不需要&
if (ret ==-1) {
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->size; i++)
{
//数据迁移
abs->Personarr[i] = abs->Personarr[i + 1];
}
abs->size--; //更新数据
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
//查找联系人
void findperson(books* abs) {
string name;
cout << "请输入要查找的名字:";
cin >> name;
int ret = IsExit(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->size; i++)
{
cout << "姓名:" << abs->Personarr[i].name << "\t";
cout << "性别:" << abs->Personarr[i].sex << "\t";
cout << "年龄:" << abs->Personarr[i].age << "\t";
cout << "联系电话:" << abs->Personarr[i].phonenum << "\t";
cout << "家庭住址:" << abs->Personarr[i].address << endl;
}
}
system("pause");
system("cls");
}
查找联系人
注释
1、检测函数:判断是否存在,存在,返回通讯录的位置,删除;不存在,显示查无此人,返回-1(同删除功能的检测函数)
2、判断返回值对应的信息是否与所求信息相同,并且调用显示函数中对各个信息显示部分,但数组的下标是当前return返回的结果。
代码展示
//查找联系人
void findperson(books* abs) {
string name;
cout << "请输入要查找的名字:";
cin >> name;
int ret = IsExit(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->size; i++)
{
cout << "姓名:" << abs->Personarr[i].name << "\t";
cout << "性别:" << abs->Personarr[i].sex << "\t";
cout << "年龄:" << abs->Personarr[i].age << "\t";
cout << "联系电话:" << abs->Personarr[i].phonenum << "\t";
cout << "家庭住址:" << abs->Personarr[i].address << endl;
}
}
system("pause");
system("cls");
}
清空联系人
注释
判断是否要清空,若要清空,只要通讯录记录的人数值为零,则就清空联系人
代码展示
//清空通讯录列表
void clearperson(books* abs) {
cout << "是否清空?" << endl;
cout << "1---是" << endl;
cout << "2---否" << endl;
int num;
while (true) {
cin >> num;
if (num == 1) {
abs->size = 0; //当前通讯录人数为零,做逻辑上的清空
cout << "当前通讯录列表为空" << endl;
break;
}
else if (num == 2) {
break;
}
else
cout << "请输入正确选项" << endl;
}
system("pause");
system("cls");
}
主函数代码
int main() {
//主菜单显示
int n; //选择项记忆符号
struct books abs;
abs.size = 0;
while (1) {
showMenu();
cout << "输入你的选择:" << endl;
cin >> n;
switch (n) {
case 0://退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
case 1://查找联系人
findperson(&abs);
break;
case 2://添加联系人
add(&abs); //利用地址传递来修饰实参
break;
case 3://修改联系人
modifyperson(&abs);
break;
case 4: //删除联系人
deleteperson(&abs);
break;
case 5: //显示联系人
Showperson(&abs);
break;
case 6: //清空联系人
clearperson(&abs);
break;
}
}
system("pause");
return 0;
}
注意
清屏操作
system("pause");
system("cls");
所有代码
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000 //上限人数
//主菜单
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 name;
int age;
string sex;
string phonenum; //连续数字要用字符串表示
string address;
};
struct books {
struct person Personarr[MAX]; //通讯录保存的联系人数组
int size; //记录通讯录人数
};
//添加函数
void add(books* abs) {
if (abs->size > 1000) {
cout << "添加人数已达上限" << endl;
return;
}
else {
cout << "请输入姓名:";
cin >> abs->Personarr[abs->size].name;
cout << "请输入正确性别:" << endl;
int sex = 0;
//如果输入的是1或者2的话,才可跳出循环,否则一直循环
while (true) {
cout << "1---男" << endl;
cout << "2---女" << endl;
cin >> sex;
if (sex == 1){
abs->Personarr[abs->size].sex = "男";
break;
}
else if (sex == 2) {
abs->Personarr[abs->size].sex = "女";
break;
}
else {
cout << "输入有误,请重新输入" << endl;
}
}
cout << "请输入年龄:";
cin >> abs->Personarr[abs->size].age;
cout << "请输入联系电话:";
cin >> abs->Personarr[abs->size].phonenum;
cout << "请输入家庭住址:";
cin >> abs->Personarr[abs->size].address;
}
//更新联系人的人数:当前记录数要自加1
abs->size++;
cout << "添加成功" << endl;
//清屏操作
system("pause");
system("cls");
}
//显示已添加联系人
void Showperson(books* abs) {
//判断当前人数是否为空,为空就现实通讯录为空,否则再输入
if (abs->size == 0) {
cout << "通讯录为空" << endl;
}
else
{
int i;
for (i = 0; i < abs->size; i++) {
cout << "姓名:" << abs->Personarr[i].name << "\t";
cout << "性别:" << abs->Personarr[i].sex << "\t";
cout << "年龄:" << abs->Personarr[i].age << "\t";
cout << "联系电话:" << abs->Personarr[i].phonenum << "\t";
cout << "家庭住址:" << abs->Personarr[i].address << endl;
}
cout << endl;
}
system("pause");
system("cls");
}
//检测联系人是否存在:判断是否存在,存在,返回通讯录的位置,删除;不存在,显示查无此人,并且返回-1
//参数一:通讯录;参数二:对比姓名
int IsExit(books* abs,string name) {
int i;
for (i = 0; i < abs->size; i++) {
if (abs->Personarr[i].name == name) {
cout << "找到此人!" << endl;
return i;
}
}
return -1;
}
//删除联系人
void deleteperson(books *abs) {
string name;
cout << "请输入要删除的名字:";
cin >> name;
int ret = IsExit(abs, name); //int ret = IsExit(&abs, name);为什么在这不用&?
//因为本地在传参数时候,abs已经加了*,在下面使用时,不需要&
if (ret ==-1) {
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->size; i++)
{
//数据迁移
abs->Personarr[i] = abs->Personarr[i + 1];
}
abs->size--; //更新数据
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
//查找联系人
void findperson(books* abs) {
string name;
cout << "请输入要查找的名字:";
cin >> name;
int ret = IsExit(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->size; i++)
{
cout << "姓名:" << abs->Personarr[i].name << "\t";
cout << "性别:" << abs->Personarr[i].sex << "\t";
cout << "年龄:" << abs->Personarr[i].age << "\t";
cout << "联系电话:" << abs->Personarr[i].phonenum << "\t";
cout << "家庭住址:" << abs->Personarr[i].address << endl;
}
}
system("pause");
system("cls");
}
//修改联系人
void modifyperson(books* abs) {
string name;
cout << "查找联系人的信息:";
cin >> name;
int ret = IsExit(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else
{
cout << "请输入要修改的信息:" << endl;
cout << "请输入姓名:";
cin >> abs->Personarr[abs->size].name;
cout << "请输入正确性别:" << endl;
int sex = 0;
//如果输入的是1或者2的话,才可跳出循环,否则一直循环
while (true) {
cout << "1---男" << endl;
cout << "2---女" << endl;
cin >> sex;
if (sex == 1) {
abs->Personarr[ret].sex = "男";
break;
}
else if (sex == 2) {
abs->Personarr[ret].sex = "女";
break;
}
else {
cout << "输入有误,请重新输入" << endl;
}
}
cout << "请输入年龄:";
cin >> abs->Personarr[ret].age;
cout << "请输入联系电话:";
cin >> abs->Personarr[ret].phonenum;
cout << "请输入家庭住址:";
cin >> abs->Personarr[ret].address;
}
system("pause");
system("cls");
}
//清空通讯录列表
void clearperson(books* abs) {
cout << "是否清空?" << endl;
cout << "1---是" << endl;
cout << "2---否" << endl;
int num;
while (true) {
cin >> num;
if (num == 1) {
abs->size = 0; //当前通讯录人数为零,做逻辑上的清空
cout << "当前通讯录列表为空" << endl;
break;
}
else if (num == 2) {
break;
}
else
cout << "请输入正确选项" << endl;
}
system("pause");
system("cls");
}
int main() {
//主菜单显示
int n; //选择项记忆符号
struct books abs;
abs.size = 0;
while (1) {
showMenu();
cout << "输入你的选择:" << endl;
cin >> n;
switch (n) {
case 0://退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
case 1://查找联系人
findperson(&abs);
break;
case 2://添加联系人
add(&abs); //利用地址传递来修饰实参
break;
case 3://修改联系人
modifyperson(&abs);
break;
case 4: //删除联系人
deleteperson(&abs);
break;
case 5: //显示联系人
Showperson(&abs);
break;
case 6: //清空联系人
clearperson(&abs);
break;
}
}
system("pause");
return 0;
}