#define MAX 100 // 通讯录最大人数
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
// 人物结构体
struct Person_info{
string name; // 姓名
string sex; // 性别
int age; // 年龄
string tel; // 电话
string address; // 住址
};
// 通讯录结构体
struct address_book{
struct Person_info person_array[MAX];
int len; // 通讯录人数
};
// 显示主菜单
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;
//system("pause");
}
// 添加联系人
void add_person(struct address_book *list){
if (list->len == MAX){
cout << "通讯录最大人数已满" << endl;
}
else{
string name;
string sex;
int age;
string tel;
string address;
cout << "请输入姓名:" << endl;
cin >> name;
list->person_array[list->len].name = name;
cout << "请输入性别:" << endl;
cin >> sex;
list->person_array[list->len].sex = sex;
cout << "请输入年龄:" << endl;
cin >> age;
list->person_array[list->len].age = age;
cout << "请输入电话:" << endl;
cin >> tel;
list->person_array[list->len].tel = tel;
cout << "请输入住址:" << endl;
cin >> address;
list->person_array[list->len].address = address;
cout << "添加成功!" << endl;
list->len++; // 更新人数
}
}
// 显示联系人
void show_person(struct address_book *list){
if (list->len == 0){
cout << "通讯录为空!" << endl;
}
else{
for (int i = 0; i < list->len; i++){
cout << "姓名:" << list->person_array[i].name << "\t性别:" << list->person_array[i].sex << "\t年龄:" << list->person_array[i].age << "\t电话:" << list->person_array[i].tel << "\t住址:" << list->person_array[i].address << endl;
}
}
}
// 查找联系人
void select_person(struct address_book *list){
string select_name;
int flag = 0; // 查找标记
cout << "请输入要查找人员的姓名:" << endl;
cin >> select_name;
for (int i = 0; i < list->len; i++){
if (list->person_array[i].name == select_name){
flag = 1;
cout << "姓名:" << list->person_array[i].name << "\t性别:" << list->person_array[i].sex << "\t年龄:" << list->person_array[i].age << "\t电话:" << list->person_array[i].tel << "\t住址:" << list->person_array[i].address << endl;
break;
}
}
if (flag == 0){
cout << "通讯录中没有此人!" << endl;
}
}
// 删除联系人
void delete_person(struct address_book *list){
string delete_name;
int flag = 0; // 查找标记
cout << "请输入要删除人员的姓名:" << endl;
cin >> delete_name;
for (int i = 0; i < list->len; i++){
if (list->person_array[i].name == delete_name){
flag = 1;
for (int j = i; j < list->len; j++){
list->person_array[j] = list->person_array[j + 1];
}
list->len--;
cout << "删除成功!" << endl;
break;
}
}
if (flag == 0){
cout << "通讯录中没有此人" << endl;
}
}
// 修改联系人
void modify_person(struct address_book *list){
string modify_name;
int flag = 0; // 查找标记
cout << "请输入要修改人员的姓名:" << endl;
cin >> modify_name;
for (int i = 0; i < list->len; i++){
if (list->person_array[i].name == modify_name){
flag = 1;
int ans = 0; // 修改标记
while(1){
cout << "请输入要修改的信息(没有就按0):" << endl;
string cin_info;
cin >> cin_info;
if (cin_info == "姓名"){
cout << "请输入修改后的信息:" << endl;
string new_info;
cin >> new_info;
list->person_array[i].name = new_info;
}
else if (cin_info == "性别"){
cout << "请输入修改后的信息:" << endl;
string new_info;
cin >> new_info;
list->person_array[i].sex = new_info;
}
else if (cin_info == "年龄"){
cout << "请输入修改后的信息:" << endl;
int new_info;
cin >> new_info;
list->person_array[i].age = new_info;
}
else if (cin_info == "电话"){
cout << "请输入修改后的信息:" << endl;
string new_info;
cin >> new_info;
list->person_array[i].tel = new_info;
}
else if (cin_info == "住址"){
cout << "请输入修改后的信息:" << endl;
string new_info;
cin >> new_info;
list->person_array[i].address = new_info;
}
else if (cin_info == "0"){
break;
}
}
cout << "修改成功!" << endl;
break;
}
}
if (flag == 0){
cout << "通讯录中没有此人" << endl;
}
}
// 清空联系人
void clear_person(struct address_book *list){
cout << "确定要清空联系人吗?(1或0)" << endl;
int number;
cin >> number;
if (number){
list->len = 0;
cout << "清空成功!" << endl;
}
else{
cout << "取消成功!" << endl;
}
}
int main(){
struct address_book list;
list.len = 0; // 初始人数
while (1){
show_menu();
int select = 0; // 初始选择数字
cout << "输入要执行的功能:" << endl;
cin >> select;
switch (select){
case 1:
add_person(&list);
break;
case 2:
show_person(&list);
break;
case 3:
delete_person(&list);
break;
case 4:
select_person(&list);
break;
case 5:
modify_person(&list);
break;
case 6:
clear_person(&list);
break;
case 0:
cout << "欢迎下次使用" << endl;
break;
}
}
}
C++ 通讯录管理系统
最新推荐文章于 2024-07-27 15:32:33 发布