目录
2. adressStruct.h 和 adressStruct.cpp
3. functions.h 和 functions.cpp
一、需求
实现通讯录以下功能:
- 添加联系人:向通讯录中添加联系人,信息包括姓名、性别、年龄、手机号、地址;
- 查找联系人:从通讯录中以姓名查找联系人并打印联系人信息;
- 删除联系人:从通讯录中以姓名删除联系人;
- 修改联系人:从通讯录中以姓名修改联系人信息;
- 查看通讯录:打印当前通讯录所有联系人信息;
- 清空通讯录:删除通讯录所有联系人;
- 退出通讯录:退出当前通讯录。
二、代码文件
1. main.cpp
// main.cpp文件代码
#include <iostream>
using namespace std;
#include "adressStruct.h"
#include "functions.h"
int main()
{
// 实例化通讯录
adressbooks book;
book.m_size = 0;
while(true)
{
// 打印通讯录菜单
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 cases;
cin >> cases;
switch (cases)
{
case 1: //1 添加联系人
addPerson(&book);
break;
case 2: //2 查找联系人
findPerson(&book);
break;
case 3: //3 删除联系人
deletePerson(&book);
break;
case 4: //4 修改联系人
modifyPerson(&book);
break;
case 5: //5 查看通讯录
displaybook(&book);
break;
case 6: //6 清空通讯录
book.m_size = 0;
cout << "通讯录已清空。" << endl;
break;
case 0: //0 退出通讯录
cout << "感谢您的使用,再见。" << endl;
system("pause");
return 0;
break;
}
system("pause");
system("cls");
}
return 0;
}
2. adressStruct.h 和 adressStruct.cpp
// dressStruct.h文件代码
#pragma once
using namespace std;
#include <string>
#define MAX 1000
// 定义联系人结构体
struct Person {
// 联系人姓名
string name;
// 联系人年龄
int age;
// 联系人性别:1为男,2为女
int sex;
// 联系人手机号
string phone;
// 联系人地址
string adress;
};
//定义通讯录结构体
struct adressbooks {
Person personArr[MAX];
int m_size;
};
// adressStruct.cpp文件代码
#include "adressStruct.h"
struct Person;
struct adressbooks;
3. functions.h 和 functions.cpp
// functions.h文件代码
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "adressStruct.h"
void addPerson(adressbooks * book);
void findPerson(const adressbooks * book);
void displaybook(adressbooks * book);
void deletePerson(adressbooks * book);
void modifyPerson(adressbooks * book);
// functions.cpp文件代码
#include "functions.h"
// 添加联系人函数
void addPerson(adressbooks * book)
{
cout << "请输入联系人姓名:" << endl;
cin >> book->personArr[book->m_size].name;
cout << "请输入联系人性别:" << endl;
cout << "1 男;2 女" << endl;
int flag;
while (true) {
cin >> flag;
if (flag == 1 || flag == 2) {
book->personArr[book->m_size].sex = flag;
break;
}
else
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入联系人年龄:" << endl;
cin >> book->personArr[book->m_size].age;
cout << "请输入联系人手机号:" << endl;
cin >> book->personArr[book->m_size].phone;
cout << "请输入联系人地址:" << endl;
cin >> book->personArr[book->m_size].adress;
cout << "添加成功。" << endl;
book->m_size++;
}
// 查找联系人函数
void findPerson(const adressbooks * book) {
if (book->m_size == 0) {
cout << "通讯录为空,请先添加联系人。" << endl;
return;
}
cout << "请输入查找的联系人姓名:" << endl;
string f_name;
cin >> f_name;
string sex;
for (int i = 0; i < book->m_size; i++) {
if (f_name == book->personArr[i].name)
{
if (book->personArr[i].sex == 1)
sex = "男";
else
sex = "女";
cout << "姓名:" << book->personArr[i].name
<< "\t性别:" << sex
<< "\t年龄:" << book->personArr[i].age
<< "\t手机号:" << book->personArr[i].phone
<< "\t地址:" << book->personArr[i].adress << endl;
return;
}
}
cout << "查无此人,请核查输入是否有误。" << endl;
}
// 显示联系人函数
void displaybook(adressbooks * book) {
if (book->m_size == 0) {
cout << "通讯录为空,请先添加联系人。" << endl;
return;
}
string sex;
for (int i = 0; i < book->m_size; i++) {
if (book->personArr[i].sex == 1)
sex = "男";
else
sex = "女";
cout << "姓名:" << book->personArr[i].name
<< "\t性别:" << sex
<< "\t年龄:" << book->personArr[i].age
<< "\t手机号:" << book->personArr[i].phone
<< "\t地址:" << book->personArr[i].adress << endl;
}
}
// 删除联系人
int finds(const adressbooks * book) {
string name;
cin >> name;
for (int i = 0; i < book->m_size; i++) {
if (name == book->personArr[i].name) {
return i;
}
}
return -1;
}
void deletePerson(adressbooks * book) {
if (book->m_size == 0) {
cout << "通讯录为空,请先添加联系人。" << endl;
return;
}
cout << "请输入要删除的联系人姓名:" << endl;
int flag = finds(book);
if (flag == -1) {
cout << "查无此人,请检查输入是否有误。" << endl;
return;
}
for (int i = flag; i < book->m_size; i++) {
book->personArr[i] = book->personArr[i + 1];
}
book->m_size--;
cout << "删除成功。" << endl;
}
// 修改联系人
void modifyPerson(adressbooks * book) {
if (book->m_size == 0) {
cout << "通讯录为空,请先添加联系人。" << endl;
return;
}
cout << "请输入要修改的联系人姓名:" << endl;
int flag = finds(book);
if (flag == -1) {
cout << "查无此人,请检查输入是否有误。" << endl;
return;
}
cout << "请输入修改后联系人姓名:" << endl;
cin >> book->personArr[flag].name;
cout << "请输入修改后联系人性别:" << endl;
cout << "1 男;2 女" << endl;
int temp;
while (true) {
cin >> temp;
if (temp == 1 || temp == 2) {
book->personArr[flag].sex = temp;
break;
}
else
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入修改后联系人年龄:" << endl;
cin >> book->personArr[flag].age;
cout << "请输入修改后联系人手机号:" << endl;
cin >> book->personArr[flag].phone;
cout << "请输入修改后联系人地址:" << endl;
cin >> book->personArr[flag].adress;
cout << "修改成功。" << endl;
}