C++通讯录管理系统
利用结构体创建的C++通讯录管理系统,这次从头来过,重拾C++…
#include <iostream>
using namespace std;
#define MAX 1000
// 菜单函数
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;
}
// 联系人的结构体
struct Person {
string name;
int sex;
int age;
string phone;
string address;
};
// 通讯录结构体
struct Addressbooks {
// 存放联系人的数组
struct Person personArray[MAX];
// 记录通讯录当前人数
int currentSize;
};
// 函数声明
void addPerson(Addressbooks* abs);
void showPerson(const Addressbooks* p);
void deletePerson(Addressbooks* p);
void findPersion(const const Addressbooks* p);
void modifyPerson(Addressbooks* p);
void clearPerson(Addressbooks* p);
int main() {
// 创建一个通讯录结构体变量,初始化大小为0
Addressbooks abs;
abs.currentSize = 0;
// 3、菜单选择
while (true) {
//显示菜单
showMenu();
// 用户选择输入
int select;
cout << "请输入您的选择:";
cin >> select;
switch (select) {
case 1: // 添加联系人
addPerson(&abs);
break;
case 2: // 显示联系人
showPerson(&abs);
break;
case 3: // 删除联系人
deletePerson(&abs);
break;
case 4: // 查找联系人
findPersion(&abs);
break;
case 5: // 修改联系人
modifyPerson(&abs);
break;
case 6: // 清空通讯录
clearPerson(&abs);
break;
case 0: // 退出系统
cout << "成功退出,欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
// 添加联系人函数
void addPerson(Addressbooks* abs) {
// 判断通讯录是否已满
if (abs->currentSize == MAX) {
cout << "通讯录已满, 无法添加!!!" << endl;
}
else {
// 创建一个联系人
// 姓名
string name;
cout << "请输入姓名:";
cin >> name;
// 性别, 判断性别输入是否有效
int sex;
while (true) {
cout << "请输入性别(1-男,2-女):";
cin >> sex;
if (sex == 1 || sex == 2) {
break;
}
else {
cout << "输入性别无效,请重新输入" << endl;
}
}
// 年龄
int age;
cout << "请输入年龄:";
cin >> age;
// 手机号
string phone;
cout << "输入手机号:";
cin >> phone;
// 住址
string address;
cout << "请输入地址:";
cin >> address;
// 创建联系人
Person p = { name, sex, age, phone, address };
// 写进通讯录结构体
abs->personArray[abs->currentSize] = p;
// 数目增加
abs->currentSize++;
cout << "添加成功" << endl;
system("pause");
system("cls"); // 清屏操作
}
}
// 显示联系人函数
void showPerson(const Addressbooks *p) {
// 判断是不是有联系人
if (p->currentSize == 0) {
cout << "当前记录为空" << endl;
}
else {
for (int i = 0; i < p->currentSize; i++) {
cout << "姓名:" << p->personArray[i].name << "\t";
cout << "年龄:"<< p->personArray[i].age << "\t";
cout << "性别:"<< (p->personArray[i].sex == 1 ? "男":"女") << "\t";
cout << "手机号: "<< p->personArray[i].phone << "\t";
cout << "住址:"<< p->personArray[i].address<<endl;
}
}
system("pause");
system("cls");
}
// 检测联系人是否存在,根据姓名进行判断,如果存在就返回索引,不存在返回-1
int isExist(const Addressbooks* p, string name) {
for (int i = 0; i < p->currentSize; i++) {
if (p->personArray[i].name == name) {
return i;
}
}
return -1; // 没有找到就返回-1
}
// 删除函数
void deletePerson(Addressbooks* p) {
cout << "请输入删除人的姓名:";
string name;
cin >> name;
int findResult = isExist(p, name);
if (findResult == -1) {
cout << "查无此人,删除失败" << endl;
}
else {
for (int i = findResult; i < p->currentSize; i++) {
// 数据前移
p->personArray[i] = p->personArray[i + 1];
}
// 更新通讯录人数
p->currentSize--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
// 查找联系人
void findPersion(const Addressbooks* p) {
cout << "请输入查询人的姓名:";
string name;
cin >> name;
int index = isExist(p, name);
if (index != -1) {
cout << "姓名:" << p->personArray[index].name << "\t";
cout << "年龄:" << p->personArray[index].age << "\t";
cout << "性别:" << (p->personArray[index].sex == 1 ? "男" : "女") << "\t";
cout << "手机号: " << p->personArray[index].phone << "\t";
cout << "住址:" << p->personArray[index].address << endl;
}
else {
cout << "查无此人,查询失败" << endl;
}
system("pause");
system("cls");
}
// 修改联系人
void modifyPerson(Addressbooks* p) {
cout << "请输入修改人的姓名:";
string name;
cin >> name;
int index = isExist(p, name);
if (index != -1) {
// 性别, 判断性别输入是否有效
int sex;
while (true) {
cout << "请输入性别(1-男,2-女):";
cin >> sex;
if (sex == 1 || sex == 2) {
break;
}
else {
cout << "输入性别无效,请重新输入" << endl;
}
}
// 年龄
int age;
cout << "请输入年龄:";
cin >> age;
// 手机号
string phone;
cout << "输入手机号:";
cin >> phone;
// 住址
string address;
cout << "请输入地址:";
cin >> address;
// 创建联系人
Person person = { name, sex, age, phone, address };
p->personArray[index] = person;
cout << "修改成功" << endl;
}
else {
cout<<"查无此人,修改失败"<<endl;
}
system("pause");
system("cls");
}
// 清空
void clearPerson(Addressbooks* p) {
if (p->currentSize == 0) {
cout << "本来就为空" << endl;
system("pause");
system("cls");
}
else {
cout << "确定删除所有?Y/N" << endl;
char select;
cin >> select;
if (select == 'Y') {
p->currentSize = 0;
cout << "清空完成" << endl;
system("pause");
system("cls");
}
else {
cout << "取消清空" << endl;
system("pause");
system("cls");
}
}
}
注:未经许可,禁止转载