通讯管理系统
2019-08-19 本文已影响0人
Bug2Coder
#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
// 设计联系人结构体
struct Person {
//姓名
string m_Name;
//性别 1:男 2:女
int m_Sex;
//年龄
int m_Age;
//电话
string m_Phone;
//住址
string m_Addr;
};
//设计通讯录结构体
struct Addressbooks {
//通讯录中保存的联系人数组
struct Person personArray[MAX];
//通讯录中当前记录联系人个数
int m_Size;
};
void addPerson(Addressbooks* abs) {
if (abs->m_Size == MAX)
{
cout << "通讯录已满,无法添加!";
return;
}
else {
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:";
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
// 性别
int sex = 0; //输出指定性别信息,否则循环输入
while (true) {
cout << "请输入性别(男:1、女:2):";
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
else
{
cout << "输入有误、重新输入。。。" << endl;
}
}
// 年龄
int age;
cout << "请输入年龄:";
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
// 电话
string phone;
cout << "请输入电话:";
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
// 住址
string addr;
cout << "请输入住址:";
cin >> addr;
abs->personArray[abs->m_Size].m_Addr = addr;
// 更新通讯录人数
abs->m_Size++;
cout << "添加成功。。。" << endl;
system("pause");
system("cls");
}
}
void showPerson(Addressbooks* abs) {
//判断通讯录中人数是否为0
if (abs->m_Size == 0) {
cout << "通讯录中人数为空、请先添加联系人" << endl;
system("pause");
system("cls");
}
else {
for (int i = 0; i < abs->m_Size; i++) {
cout << "姓名:" << abs->personArray[i].m_Name << "\t";
cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[i].m_Age << "\t";
cout << "电话:" << abs->personArray[i].m_Phone << "\t";
cout << "住址:" << abs->personArray[i].m_Addr << endl;
}
system("pause");
system("cls");
}
}
// 检测联系人是否存在,存在则返回下标,不存在则返回-1
int isExist(Addressbooks * abs, string nameperson) {
for (int i = 0; i < abs->m_Size; i++) {
if (abs->personArray[i].m_Name == nameperson) {
return i;
}
}
return -1;
}
void deletePerson(Addressbooks * abs) {
cout << "请输入您要删除的联系人姓名:";
string name;
cin >> name;
int res = isExist(abs, name);
if (res == -1) {
cout << "查无此人" << endl;
system("pause");
system("cls");
}
else {
cout << "您确定要删除此人吗?确定输入y,其他输入则取消:";
string in;
cin >> in;
if (in == "y" || in == "Y") {
//删除
for (int i = res; i < abs->m_Size; i++) {
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--; //更新通讯录中的人数
cout << "删除成功。。。" << endl;
system("pause");
system("cls");
}
else {
cout << "取消删除中。。。" << endl;
system("pause");
system("cls");
}
}
}
void findPersion(Addressbooks * abs) {
cout << "输入您要查找的联系人:";
string name;
cin >> name;
int res = isExist(abs, name);
if (res == -1) {
cout << "查无此人" << endl;
system("pause");
system("cls");
}
else {
cout << "姓名:" << abs->personArray[res].m_Name << "\t";
cout << "性别:" << (abs->personArray[res].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[res].m_Age << "\t";
cout << "电话:" << abs->personArray[res].m_Phone << "\t";
cout << "住址:" << abs->personArray[res].m_Addr << endl;
system("pause");
system("cls");
}
}
void changePerson(Addressbooks * abs) {
cout << "输入您要修改的联系人:";
string name;
cin >> name;
int res = isExist(abs, name);
if (res == -1) {
cout << "查无此人" << endl;
system("pause");
system("cls");
}
else {
string name;
int sex;
int age;
string phone;
string addr;
cout << "新姓名:";
cin >> name;
abs->personArray[res].m_Name = name;
cout << "新性别(男:1、女:2):";
cin >> sex;
abs->personArray[res].m_Sex = sex;
cout << "新年龄:";
cin >> age;
abs->personArray[res].m_Age = age;
cout << "新电话:";
cin >> phone;
abs->personArray[res].m_Phone = phone;
cout << "新住址:";
cin >> addr;
abs->personArray[res].m_Addr = addr;
system("pause");
system("cls");
}
}
void clearPerson(Addressbooks* abs) {
abs->m_Size = 0; // 将当前联系人数量置为0,做逻辑清空操作
cout << "通讯录以清空。。。" << endl;
system("pause");
system("cls");
}
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;
}
int main() {
// 创建通讯录结构体本变量
Addressbooks abs;
//初始化通讯录中当前人员个数
abs.m_Size = 0; //为0时,无法查询到数据,可做逻辑删除处理
int num;
while (true)
{
showMenu(); // 显示功能界面
cout << "请选择功能、输入功能列表前的数字:";
cin >> num;
cout << endl;
switch (num) {
case 1: //添加联系人
addPerson(&abs); //利用地址传递,可以修改实参
break;
case 2: //显示联系人
showPerson(&abs);
break;
case 3: // 删除联系人
deletePerson(&abs);
break;
case 4: // 查找联系人
findPersion(&abs);
break;
case 5: // 修改联系人
changePerson(&abs);
break;
case 6: // 清空联系人
clearPerson(&abs);
break;
case 0: // 退出通讯录
system("pause");
return 0;
break;
default:
break;
}
}
}