// VS2020
#include<iostream>
#include<string>
using namespace std;
#define MAX 60 //最大人数
#include"通讯录.h"
struct linkman {
string name;
string sex;
int age = 0;
string phoneNumber;
string homeAddress;
};
struct Addressbooks {
struct linkman personArray[MAX]; // 通讯录中保存的联系人数组
int m_size = 0; // 通讯录中人员个数
};
//显示菜单
void showMeru() {
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 IncreaseIink(Addressbooks *abs) {
//判断通讯录是否已满,如果满了就不再添加
if (abs->m_size == MAX) {
cout << "通讯录已满,无法添加;" << endl;
return;
}else{
//添加具体联系人
//姓名:
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->personArray[abs->m_size].name = name;
//性别
string sex;
cout << "请输入性别: " << endl;
cin >> sex;
abs->personArray[abs->m_size].sex = sex;
//年龄
int age;
cout << "请输入年龄: " << endl;
cin >> age;
string num_input_string;
while (cin.fail())
{
cin.clear();
cin >> num_input_string;
cout << "输入的" << num_input_string << "不是数字,请输入正确年龄:" << endl;;
cin >> age;
}
abs->personArray[abs->m_size].age = age;
//电话
string phoneNumber;
cout << "请输入联系电话: " << endl;
cin >> phoneNumber;
abs->personArray[abs->m_size].phoneNumber = phoneNumber;
//住址
string homeAddress;
cout << "请输入家庭住址: " << endl;
cin >> homeAddress;
abs->personArray[abs->m_size].homeAddress = homeAddress;
//更新通讯录人数
abs->m_size++;
}
cout << "添加成功" << endl;
system("pause");// 请按任意键继续
system("cls"); //清屏操作
}
//显示联系人
void showIink(Addressbooks* abs) {
if (abs->m_size == 0) {
cout << "通讯录为空" << endl;
}
else {
for (int i = 0; i < abs->m_size; i++) {
cout << "姓名: " << abs->personArray[i].name << "\t";
cout << " 性别: " << abs->personArray[i].sex << "\t";
cout << " 年龄 " << abs->personArray[i].age << "\t";
cout << " 电话号码 " << abs->personArray[i].phoneNumber << "\t";
cout << " 家庭住址 " << abs->personArray[i].homeAddress << endl;;
}
}
system("pause");
system("cls");
}
//判断是否存在
int isExist(Addressbooks* abs, string name) {
for (int i = 0; i < abs->m_size; i++) {
if (abs->personArray[i].name ==name) {
return i;
}
}
return -1;
}
//删除联系人
void deleteIink(Addressbooks* abs) {
//请输入联系人姓名
string name;
cout << "请输入联系人的姓名:" << endl;
cin >> name;
//ret==-1未查到
//ret==1查到了
int ret = isExist(abs, name);
if (ret != -1) { //找到了,要进行删除操作
for (int i = ret; i < abs->m_size; i++) {
//数据迁移
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_size--;//更新通讯录中的人员数
cout << "删除成功:" << endl;
}
else { //没有找到
cout << "通讯录中没有联系人," << name << endl;
}
system("pause");
system("cls");
}
//查找联系人
void findPerson(Addressbooks* abs) {
cout << "请输入你要查找的联系人: " << endl;
string name;
cin >> name;
int ret = isExist(abs, name); // 判断指定的联系人是否存在通讯录中。
if (ret != -1) { //找到了,打印此人信息
cout << "姓名 " << abs->personArray[ret].name << "\t";
cout << "年龄 " << abs->personArray[ret].age << "\t";
cout << "性别 " << abs->personArray[ret].sex << "\t";
cout << "电话号码 " << abs->personArray[ret].phoneNumber<< "\t";
cout << "家庭地址 " << abs->personArray[ret].homeAddress << endl;
}
else {
cout << "通讯录中没有此人" << endl;
}
system("pause");
system("cls");
}
//修改联系人
void reviseIink(Addressbooks* abs) {
cout << "请输入你要修改的联系人: " << endl;
string name;
cin >> name;
int ret = isExist(abs, name); // 判断指定的联系人是否存在通讯录中。
if (ret != -1) { //找到了,修改此人信息
//性别
string sex;
cout << "请输入性别: " << endl;
cin >> sex;
abs->personArray[ret].sex = sex;
//年龄
int age;
cout << "请输入年龄: " << endl;
cin >> age;
string num_input_string;
while (cin.fail())
{
cin.clear();
cin >> num_input_string;
cout << "输入的" << num_input_string << "不是数字,请输入正确年龄:" << endl;;
cin >> age;
}
abs->personArray[ret].age = age;
//电话
string phoneNumber;
cout << "请输入联系电话: " << endl;
cin >> phoneNumber;
abs->personArray[ret].phoneNumber = phoneNumber;
//住址
string homeAddress;
cout << "请输入家庭住址: " << endl;
cin >> homeAddress;
abs->personArray[ret].homeAddress = homeAddress;
cout << "修改成功" << endl;
}
else {
cout << "通讯录中没有此人" << endl;
}
system("pause");
system("cls");
}
//清空联系人
void emptyIink(Addressbooks* abs) {
cout << "确定清空联系人(Y/N):" << endl;;
string isflag;
cin >> isflag;
if (isflag == "Y") {
abs->m_size = 0;// 逻辑清空
cout << "通讯录已清空:" << endl;
}
system("pause");
system("cls");
}
int main() {
//创建通讯录结构体变量
Addressbooks abs;
//初始化通讯录中当前人员个数
abs.m_size = 0;
int select; //创建用户选择的变量
while (1) {
showMeru();
cin >> select;
switch (select) {
case 1: //1.添加联系人
IncreaseIink(&abs);
break;
case 2: //2.显示联系人
showIink(&abs);
break;
case 3: //3.删除联系人
deleteIink(&abs);
break;
case 4: //4.查找联系人
findPerson(&abs);
break;
case 5: //5.修改联系人
reviseIink(&abs);
break;
case 6: // 6.清空联系人
emptyIink(&abs);
break;
case 0: // 0.退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
}
通讯录管理系统(C++)
最新推荐文章于 2024-08-23 21:39:27 发布