#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#define MAX_NUM 10
#include "address_books.h"
using namespace std;
// 菜单显示
void show_menu()
{
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;
}
// 0. 退出通讯录
int exit_address_books()
{
int i = 5;
while (i != 0 && i != 1)
{
cout << "确定要退出吗?(0.退出、1.不退出)" << endl;
cin >> i;
if (i == 0)
{
cout << "欢迎下次使用!" << endl;
return 0;
}
else if (i == 1)
{
cout << "继续使用!" << endl;
return 1;
}
else
cout << "输入错误,请重新输入:" << endl;
}
}
// 1. 添加联系人
// 1.1 创建联系人结构体
struct person
{
string name;
int sex;
int age;
string phone;
string address;
};
// 1.2 创建通讯录结构体
struct address_books
{
struct person arr[MAX_NUM];
int person_num;
};
// 1.3 添加联系人函数
void add_person(struct address_books* ab)
{
if (ab->person_num > MAX_NUM) // 检查通讯录是否已满员
{
cout << "人数已满!" << endl;
}
else
{
string name; // 建立姓名字符串
cout << "请输入姓名:" << endl;
cin >> name;
ab->arr[ab->person_num].name = name;// 把姓名存进通讯录的第一个内
while (true)
{
int sex = 0;
cout << "请输入性别:(0.女、1.男)" << endl;
cin >> sex;
if (sex == 0 || sex == 1)
{
ab->arr[ab->person_num].sex = sex;
break;
}
cout << "输入错误,请重新输入!" << endl;
}
int age = 0;
cout << "请输入年龄:" << endl;
cin >> age;
ab->arr[ab->person_num].age = age;
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
ab->arr[ab->person_num].phone = phone;
string address;
cout << "请输入地址:" << endl;
cin >> address;
ab->arr[ab->person_num].address = address;
ab->person_num++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
}
// 2. 显示联系人
// 2.1 函数
void print_person(struct address_books* ab)
{
for (int i = 0; i < ab->person_num; i++)
{
cout << ab->arr[i].name << "\t";
cout << (ab->arr[i].sex == 0 ? "女" : "男") << "\t";
cout << ab->arr[i].age << "\t";
cout << ab->arr[i].phone << "\t";
cout << ab->arr[i].address << "\t" << endl;
}
system("pause");
system("cls");
}
// 3. 删除联系人
// 3.1 检查有没有这个人
int check_person(struct address_books* ab, string re_name)
{
for (int i = 0; i < ab->person_num; i++)
{
if (ab->arr[i].name == re_name)
{
return i;
}
else
return -1;
}
}
// 3.2 删除函数
void remove_person(struct address_books* ab)
{
string re_name;
cout << "请输入要删除联系人的姓名:" << endl;
cin >> re_name;
int re = 0;
re = check_person(ab, re_name); // 检查人是否存在
if (re != -1)
{
for (int i = re; i < ab->person_num; i++)
{
ab->arr[i] = ab->arr[i + 1]; // 删除就是将下一个赋值给要删除的对象
}
ab->person_num--;
cout << "删除成功!" << endl;
}
else
cout << "查无此人!" << endl;
system("pause");
system("cls");
}
// 4. 查找联系人
void seek_person(struct address_books* ab)
{
string se_name;
cout << "请输入要查找的人的姓名:" << endl;
cin >> se_name;
int i = check_person(ab, se_name);
if (i != -1)
{
cout << ab->arr[i].name << "\t";
cout << (ab->arr[i].sex == 0 ? "女" : "男") << "\t";
cout << ab->arr[i].age << "\t";
cout << ab->arr[i].phone << "\t";
cout << ab->arr[i].address << "\t" << endl;
}
else
cout << "查无此人!" << endl;
system("pause");
system("cls");
}
// 5. 修改联系人
void alter_person(struct address_books* ab)
{
string al_name;
cout << "请输入要修改人的姓名:" << endl;
cin >> al_name;
int i = check_person(ab, al_name);
if (i != -1)
{
string name; // 建立姓名字符串
cout << "请输入姓名:" << endl;
cin >> name;
ab->arr[i].name = name;// 把姓名存进通讯录的第一个内
while (true)
{
int sex = 0;
cout << "请输入性别:(0.女、1.男)" << endl;
cin >> sex;
if (sex == 0 || sex == 1)
{
ab->arr[i].sex = sex;
break;
}
cout << "输入错误,请重新输入!" << endl;
}
int age = 0;
cout << "请输入年龄:" << endl;
cin >> age;
ab->arr[i].age = age;
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
ab->arr[i].phone = phone;
string address;
cout << "请输入地址:" << endl;
cin >> address;
ab->arr[i].address = address;
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
// 6. 清空联系人
void clean_person(struct address_books* ab)
{
ab->person_num = 0;
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
// 主函数
int main()
{
int choose = 1;
struct address_books ab; // 创建通讯录
ab.person_num = 0;
while (choose != 0)
{
show_menu();
cout << "请输入你要进行的操作:" << endl;
cin >> choose;
switch (choose)
{
case 1:
add_person(&ab);
break;
case 2:
print_person(&ab);
break;
case 3:
remove_person(&ab);
break;
case 4:
seek_person(&ab);
break;
case 5:
alter_person(&ab);
break;
case 6:
clean_person(&ab);
break;
case 0: // 退出通讯录
choose = exit_address_books();
break;
default:
break;
}
}
system("pause");
return 0;
}
C++通讯录管理系统
最新推荐文章于 2023-07-31 20:21:47 发布