#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
//联系人
struct Person {
string uname;
// 性别 1 男 2 女
int sex;
int age;
string phone;
string address;
};
//通讯录
struct AddressBooks {
//通讯录中保存的联系人数组
struct Person personArray[MAX];
//通讯录中当前记录联系人个数
int size;
};
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;
}
/**
* @func 添加联系人
*/
void addPerson(AddressBooks *abs) {
if(abs->size == MAX){
cout << "通讯录已满,无法添加!" << endl;
return;
} else {
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->size].uname = name;
//性别
int sex = 0;
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
while(true){ //必须输入正确的数据
cin >> sex;
if(sex == 1 || sex == 2){
abs->personArray[abs->size].sex = sex;
break;
}
cout << "输入数据不正确,请重新输入!" << endl;
}
//年龄
int age = 0;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArray[abs->size].age = age;
//联系电话
string phone;
cout << "请输入联系电话:" << endl;
cin >> phone;
abs->personArray[abs->size].phone = phone;
//住址
string address;
cout << "请输入家庭住址:" << endl;
cin >> address;
abs->personArray[abs->size].address = address;
//更新通讯录人数
abs->size++;
cout << "添加成功!" << endl;
system("pause"); //按任意键继续
system("cls"); //清屏
}
}
/**
* @func 显示联系人
*/
void showPerson(struct AddressBooks *abs) {
if(abs->size == 0){
cout << "当前记录为空" << endl;
} else {
for(int i=0;i < abs->size;i++){
cout << "姓名:" << abs->personArray[i].uname << "\t";
cout << "性别:" << (abs->personArray[i].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[i].age << "\t";
cout << "电话:" << abs->personArray[i].phone << "\t";
cout << "住址:" << abs->personArray[i].address << endl;
}
}
system("pause");
system("cls");
}
/**
* @func 判断联系人是否存在
*/
int isExist(struct AddressBooks *abs, string name) {
for(int i=0;i<abs->size;i++){
if(abs->personArray[i].uname == name){
return i;
}
}
return -1;
}
/**
* @func 删除联系人
*/
void delContact(AddressBooks *abs) {
cout << "请输入要删除的联系人的姓名:" << endl;
string name;
cin >> name;
int position = isExist(abs, name);
if(position == -1){
cout << "查无此人!" << endl;
} else {
cout << "已查询到该联系人,正在删除..." << endl;
for(int i=position;i<abs->size;i++){
//数据前移 ==> 删除
abs->personArray[i] = abs->personArray[i+1];
}
abs->size--; //更新通讯录中人员数
cout << "删除成功!" << endl;
}
system("pause");
system("cls");
}
/**
* @func 查找联系人
*/
void queryPerson(struct AddressBooks *abs) {
cout << "请输入要查找的联系人姓名:" << endl;
string name;
cin >> name;
int exist = isExist(abs, name);
if(exist == -1){
cout << "查无此人!" << endl;
} else {
cout << "姓名:" << abs->personArray[exist].uname << "\t";
cout << "性别:" << (abs->personArray[exist].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[exist].age << "\t";
cout << "电话:" << abs->personArray[exist].phone << "\t";
cout << "住址:" << abs->personArray[exist].address << endl;
}
system("pause");
system("cls");
}
/**
* @func 修改联系人
*/
void modifyPerson(struct AddressBooks *abs) {
cout << "请输入要修改的联系人名称:" << endl;
string name;
cin >> name;
int exist = isExist(abs, name);
if(exist == -1){
cout << "查无此人!" << endl;
} else {
cout << "以下为该联系人相关信息:" << endl;
cout << "------------------------" << endl;
cout << "姓名:" << abs->personArray[exist].uname << "\t";
cout << "性别:" << (abs->personArray[exist].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[exist].age << "\t";
cout << "电话:" << abs->personArray[exist].phone << "\t";
cout << "住址:" << abs->personArray[exist].address << endl;
cout << "------------------------" << endl;
cout << "请输入修改后的联系人信息:" << endl;
cout << "------------------------" << endl;
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[exist].uname = name;
//性别
int sex = 0;
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
while(true){ //必须输入正确的数据
cin >> sex;
if(sex == 1 || sex == 2){
abs->personArray[exist].sex = sex;
break;
}
cout << "输入数据不正确,请重新输入!" << endl;
}
//年龄
int age = 0;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArray[exist].age = age;
//联系电话
string phone;
cout << "请输入联系电话:" << endl;
cin >> phone;
abs->personArray[exist].phone = phone;
//住址
string address;
cout << "请输入家庭住址:" << endl;
cin >> address;
abs->personArray[exist].address = address;
cout << "修改成功!" << endl;
}
system("pause");
system("cls");
}
/**
* @func 清空通讯录
*/
void cleanBook(struct AddressBooks *abs) {
cout << "即将删除所有联系人数据!确认删除吗?" << endl;
cout << "1---是" << endl;
cout << "0---否" << endl;
int confirm = 0;
cin >> confirm;
if(confirm == 1){
abs->size = 0; //逻辑删除
cout << "通讯录已经清空!" << endl;
} else if(confirm == 0){
cout << "取消删除!" << endl;
} else {
cout << "请输入是/否!" << endl;
}
system("pause");
system("cls");
}
int main() {
//创建通讯录结构体变量
AddressBooks abs;
abs.size = 0;
int select = 0; //用户选择输入的变量
while(true){
//菜单调用
showMenu();
cin >> select;
switch (select) {
case 1: //添加
addPerson(&abs);
break;
case 2: //显示
showPerson(&abs);
break;
case 3:
delContact(&abs);
break;
case 4:
queryPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
cleanBook(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}