代码是照着黑马教程写的,教程链接如下:
黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili
在windows系统中,用的Visual Studio 2019
分成了3个文件,分别是头文件head.h、子函数文件function.cpp、包含了main函数的AddressbookManagement.cpp。
一、头文件代码:
#include<iostream>
using namespace std;
#include<string.h>
#define MAX 10
typedef struct Person { //联系人结构体
string name; //姓名
int sex; //性别
int age; //年龄
string phone; //联系电话
string address; //联系地址
}Person;
typedef struct Addressbooks { //通讯录结构体
Person personArray[MAX]; //通讯录中保存的联系人数组
int size = 0; //通讯录中联系人个数
}Addressbooks;
void showMenu(); //菜单界面
void addPerson(Addressbooks *abs); //添加联系人
void showPerson(Addressbooks abs); //显示联系人
int isExist(Addressbooks abs,string name); //检测是否存在,并返回下标
void deletePerson(Addressbooks* abs); //删除联系人
void findPerson(Addressbooks abs); //查找联系人并显示相应信息
void changePerson(Addressbooks* abs); //修改联系人信息
void clearPerson(Addressbooks* abs); //清空联系人
二、子函数文件function.cpp
#include"head.h"
//菜单界面
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(Addressbooks *abs)
{
//先判断通讯录是否已经满了,满了就直接返回
if (abs->size >= MAX)
{
cout << "通讯录已经满了,无法新增联系人!" << endl;
return;
}
//输入姓名
cout << "请输入姓名:" << endl;
cin >> abs->personArray[abs->size].name;
//输入性别
cout << "请输入性别:1--男,2--女" << endl;
int sex = 1;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->size].sex = sex;
break;
}
cout << "性别输入有误,请重新输入!" << endl;
}
//输入年龄
cout << "请输入年龄:" << endl;
cin >> abs->personArray[abs->size].age;
//输入联系电话
cout << "请输入联系电话:" << endl;
cin >> abs->personArray[abs->size].phone;
//输入联系地址
cout << "请输入联系地址:" << endl;
cin >> abs->personArray[abs->size].address;
//增加通讯录联系人数
abs->size++;
//任意按键继续,并进行清屏操作
system("pause");
system("cls");
}
void showPerson(Addressbooks abs)
{
string sex;
if (abs.size == 0) {
cout << "当前通讯录中没有联系人,请添加。" << endl;
}
else {
cout << "通讯录目前有以下" << abs.size << "位联系人" << endl;
for (int i = 0; i < abs.size; i++)
{
sex = abs.personArray[i].sex == 1 ? "男" : "女";
cout << i+1 << "--\t姓名:" << abs.personArray[i].name << "\t性别:" << sex << "\t年龄:" << abs.personArray[i].age
<< "\t电话:" << abs.personArray[i].phone << "\t地址:" << abs.personArray[i].address << endl;
}
}
system("pause");
system("cls");
}
int isExist(Addressbooks abs,string name)
{
for (int i = 0; i < abs.size; i++) {
if (abs.personArray[i].name == name)
return i;
}
cout << "查无此人" << endl;
return -1;
}
void deletePerson(Addressbooks* abs)
{
string name; //存放待查找用户的姓名
int index; //存放找到的用户下标
cout << "请输入待删除联系人的姓名:" << endl;
cin >> name;
index = isExist(*abs, name);
if (index != -1) {
for (int i = index; i < abs->size-1 ; i++) {
abs->personArray[i] = abs->personArray[i + 1];
}
abs->size--; //更新联系人数
}
system("pause");
system("cls");
}
void findPerson(Addressbooks abs) //查找联系人并显示相应信息
{
string name, sex;
int i;
cout << "请输入待查找的联系人姓名:" << endl;
cin >> name;
i = isExist(abs, name);
if (i != -1) {
cout << i + 1 << "--\t姓名:" << abs.personArray[i].name << "\t性别:" << sex << "\t年龄:" << abs.personArray[i].age
<< "\t电话:" << abs.personArray[i].phone << "\t地址:" << abs.personArray[i].address << endl;
}
system("pause");
system("cls");
}
void changePerson(Addressbooks* abs) //修改联系人信息
{
string name;
int i;
cout << "请输入待修改联系人的姓名:" << endl;
cin >> name;
i = isExist(*abs, name);
if (i != -1) {
//输入姓名
cout << "请输入姓名:" << endl;
cin >> abs->personArray[i].name;
//输入性别
cout << "请输入性别:1--男,2--女" << endl;
int sex = 1;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[i].sex = sex;
break;
}
cout << "性别输入有误,请重新输入!" << endl;
}
//输入年龄
cout << "请输入年龄:" << endl;
cin >> abs->personArray[i].age;
//输入联系电话
cout << "请输入联系电话:" << endl;
cin >> abs->personArray[i].phone;
//输入联系地址
cout << "请输入联系地址:" << endl;
cin >> abs->personArray[i].address;
}
system("pause");
system("cls");
}
void clearPerson(Addressbooks* abs)
{
abs->size = 0;
cout << "通讯录联系人已清空!" << endl;
system("pause");
system("cls");
}
三、包含了main函数的AddressbookManagement.cpp
#include"head.h"
int main()
{
//创建通讯录
Addressbooks abs;
abs.size = 0;
int select = 0; //存放用户输入的选择
while (true) {
showMenu(); //显示菜单
cin >> select; //用户输入
switch (select) { //根据选项进行相应操作
case 1: //1、添加联系人
addPerson(&abs);
break;
case 2: //2、显示联系人
showPerson(abs);
break;
case 3: //3、删除联系人
deletePerson(&abs);
break;
case 4: //4、查找联系人
findPerson(abs);
break;
case 5: //5、修改联系人
changePerson(&abs);
break;
case 6: //6、清空联系人
clearPerson(&abs);
break;
case 0: //0、退出通讯录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
default:
cout << "您的输入序号不在0~6之内,请重新输入!" << endl;
break;
}
}
free(abs.personArray);
}