目录
1.信息存储
1.设置联系人结构体
/*设置联系人结构体*/
struct Pepole {
//姓名
string L_name;
//性别 1 男 2女
int L_sex;
//年龄
int L_age;
//电话
string L_phone;
//住址
string L_addr;
};
2.设置通讯录结构体
/*设置通讯录结构体*/
struct Addressbooks {
//通讯录中保存的联系人数组
struct Pepole person[MAX];
//当前记录通讯录中联系人的个数
int L_size;
};
2.菜单界面
/*菜单界面*/
void ShowJM()
{
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;
}
3.判断联系人是否存在
/*判断联系人是否存在*/
int isExist(Addressbooks* variablea,string name)
{
for (int i = 0; i < variablea->L_size; i++)
{
if (variablea->person[i].L_name == name)
{
return i;
}
}
return -1;
}
4.功能
1.添加功能
/*1、添加联系人*/
void addPepole(Addressbooks *variablea)
{
//判断通讯录是否已满,如果满了就不再添加
if (variablea->L_size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
//添加联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
variablea->person[variablea->L_size].L_name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1--男,2--女" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
variablea->person[variablea->L_size].L_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0)
{
variablea->person[variablea->L_size].L_age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//电话
cout << "请输入电话:" << endl;
string phone ;
cin >> phone;
variablea->person[variablea->L_size].L_phone = phone;
//住址
cout << "请输入住址:" << endl;
string addr;
cin >> addr;
variablea->person[variablea->L_size].L_addr = addr;
variablea->L_size++;
cout << "添加成功" << endl;
system("papuse");
system("cls"); //清屏操作
}
}
2.显示功能
/*2、显示联系人*/
void showPepole(Addressbooks* variablea)
{
//判断通讯录人数是否为空
if (variablea->L_size == 0) cout << "当前为空" << endl;
else {
for (int i = 0; i < variablea->L_size; i++)
{
cout << "姓名:" << variablea->person[i].L_name << "\t";
cout << "性别:" << (variablea->person[i].L_sex ==1 ? "男":"女") << "\t";
cout << "年龄:" << variablea->person[i].L_sex << "\t";
cout << "电话:" << variablea->person[i].L_phone <<"\t";
cout << "住址:" << variablea->person[i].L_addr << endl;
}
}
system("pause");
system("cls");
}
3.删除功能
/*3、删除联系人*/
void deletePepole(Addressbooks * variablea)
{
cout << "请输入您要删除的联系人" << endl;
string name;
cin >> name;
int len = isExist(variablea, name);
if (len == -1) {
cout << "查无此人" << endl;
}
else
{
for (int i = len; i < variablea->L_size; i++)
{
//数据前移
variablea->person[i] = variablea->person[i + 1];
}
variablea->L_size--;//更新通讯录中的人员数
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
4.查找功能
void findPepole(Addressbooks *variablea)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
int len = isExist(variablea, name);
if (len == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "姓名:" << variablea->person[len].L_name << "\t";
cout << "性别:" << (variablea->person[len].L_sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << variablea->person[len].L_sex << "\t";
cout << "电话:" << variablea->person[len].L_phone << "\t";
cout << "住址:" << variablea->person[len].L_addr << endl;
}
system("pause");
system("cls");
}
5.修改功能
/*5、修改联系人*/
void fixPepole(Addressbooks* variablea)
{
cout << "请输入您要修改的联系人" << endl;
string name;
cin >> name;
int len = isExist(variablea, name);
if (len == -1)
{
cout << "查无此人" << endl;
}
else
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
variablea->person[len].L_name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1--男,2--女" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
variablea->person[len].L_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0)
{
variablea->person[len].L_age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
variablea->person[len].L_phone = phone;
//住址
cout << "请输入住址:" << endl;
string addr;
cin >> addr;
variablea->person[len].L_addr = addr;
cout << "修改成功" << endl;
}
system("pause");
system("cls");
}
6.清空功能
/*6、清空联系人*/
void cleanPepole(Addressbooks* variablea)
{
variablea->L_size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
5.主函数
int main()
{
//创建通讯录结构体变量
Addressbooks variable;
//初始化通讯录中当前人员个数
variable.L_size = 0;
int select_input = 0; //用户选择输入
while (true) {
ShowJM();
cin >> select_input;
switch (select_input)
{
case 1: //1、添加联系人
addPepole(&variable);//利用地址传递,可以修改实参
break;
case 2: //2、显示联系人
showPepole(&variable);
break;
case 3: //3、删除联系人
deletePepole(&variable);
break;
case 4: //4、查找联系人
findPepole(&variable);
break;
case 5: //5、修改联系人
fixPepole(&variable);
break;
case 6: // 6、清空联系人
{
cout << "您是否确定要清空所要联系人" << endl;
cout << "输入Yes为确定清空,输入No为放弃清空" << endl;
char ch[10];
while (true)
{
cin >> ch;
if (strcmp(ch, "Yes") == 0)
{
cleanPepole(&variable);
break;
}
else if (strcmp(ch, "No") == 0) {
system("pause");
system("cls");
break;
}
cout << "请按规定重新输入" << endl;
system("pause");
system("cls");
}
}
break;
case 0: //0、退出通讯录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
return 0;
}
6.总体代码
#include<iostream>
using namespace std;
#include<cstring>
#define MAX 1000
/*菜单界面*/
void ShowJM()
{
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 Pepole {
//姓名
string L_name;
//性别 1 男 2女
int L_sex;
//年龄
int L_age;
//电话
string L_phone;
//住址
string L_addr;
};
/*设置通讯录结构体*/
struct Addressbooks {
//通讯录中保存的联系人数组
struct Pepole person[MAX];
//当前记录通讯录中联系人的个数
int L_size;
};
/*1、添加联系人*/
void addPepole(Addressbooks *variablea)
{
//判断通讯录是否已满,如果满了就不再添加
if (variablea->L_size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
//添加联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
variablea->person[variablea->L_size].L_name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1--男,2--女" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
variablea->person[variablea->L_size].L_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0)
{
variablea->person[variablea->L_size].L_age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//电话
cout << "请输入电话:" << endl;
string phone ;
cin >> phone;
variablea->person[variablea->L_size].L_phone = phone;
//住址
cout << "请输入住址:" << endl;
string addr;
cin >> addr;
variablea->person[variablea->L_size].L_addr = addr;
variablea->L_size++;
cout << "添加成功" << endl;
system("papuse");
system("cls"); //清屏操作
}
}
/*2、显示联系人*/
void showPepole(Addressbooks* variablea)
{
//判断通讯录人数是否为空
if (variablea->L_size == 0) cout << "当前为空" << endl;
else {
for (int i = 0; i < variablea->L_size; i++)
{
cout << "姓名:" << variablea->person[i].L_name << "\t";
cout << "性别:" << (variablea->person[i].L_sex ==1 ? "男":"女") << "\t";
cout << "年龄:" << variablea->person[i].L_sex << "\t";
cout << "电话:" << variablea->person[i].L_phone <<"\t";
cout << "住址:" << variablea->person[i].L_addr << endl;
}
}
system("pause");
system("cls");
}
/*判断联系人是否存在*/
int isExist(Addressbooks* variablea,string name)
{
for (int i = 0; i < variablea->L_size; i++)
{
if (variablea->person[i].L_name == name)
{
return i;
}
}
return -1;
}
/*3、删除联系人*/
void deletePepole(Addressbooks * variablea)
{
cout << "请输入您要删除的联系人" << endl;
string name;
cin >> name;
int len = isExist(variablea, name);
if (len == -1) {
cout << "查无此人" << endl;
}
else
{
for (int i = len; i < variablea->L_size; i++)
{
//数据前移
variablea->person[i] = variablea->person[i + 1];
}
variablea->L_size--;//更新通讯录中的人员数
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
/*4、查找联系人*/
void findPepole(Addressbooks *variablea)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
int len = isExist(variablea, name);
if (len == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "姓名:" << variablea->person[len].L_name << "\t";
cout << "性别:" << (variablea->person[len].L_sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << variablea->person[len].L_sex << "\t";
cout << "电话:" << variablea->person[len].L_phone << "\t";
cout << "住址:" << variablea->person[len].L_addr << endl;
}
system("pause");
system("cls");
}
/*5、修改联系人*/
void fixPepole(Addressbooks* variablea)
{
cout << "请输入您要修改的联系人" << endl;
string name;
cin >> name;
int len = isExist(variablea, name);
if (len == -1)
{
cout << "查无此人" << endl;
}
else
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
variablea->person[len].L_name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1--男,2--女" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
variablea->person[len].L_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0)
{
variablea->person[len].L_age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
variablea->person[len].L_phone = phone;
//住址
cout << "请输入住址:" << endl;
string addr;
cin >> addr;
variablea->person[len].L_addr = addr;
cout << "修改成功" << endl;
}
system("pause");
system("cls");
}
/*6、清空联系人*/
void cleanPepole(Addressbooks* variablea)
{
variablea->L_size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
int main()
{
//创建通讯录结构体变量
Addressbooks variable;
//初始化通讯录中当前人员个数
variable.L_size = 0;
int select_input = 0; //用户选择输入
while (true) {
ShowJM();
cin >> select_input;
switch (select_input)
{
case 1: //1、添加联系人
addPepole(&variable);//利用地址传递,可以修改实参
break;
case 2: //2、显示联系人
showPepole(&variable);
break;
case 3: //3、删除联系人
deletePepole(&variable);
break;
case 4: //4、查找联系人
findPepole(&variable);
break;
case 5: //5、修改联系人
fixPepole(&variable);
break;
case 6: // 6、清空联系人
{
cout << "您是否确定要清空所要联系人" << endl;
cout << "输入Yes为确定清空,输入No为放弃清空" << endl;
char ch[10];
while (true)
{
cin >> ch;
if (strcmp(ch, "Yes") == 0)
{
cleanPepole(&variable);
break;
}
else if (strcmp(ch, "No") == 0) {
system("pause");
system("cls");
break;
}
cout << "请按规定重新输入" << endl;
system("pause");
system("cls");
}
}
break;
case 0: //0、退出通讯录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
return 0;
}