#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) {
//姓名
string name;
cout << "请输入联系人的姓名: " << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
//性别 输入1为男 输入2为女
int sex = 0;
cout << "请输入联系人的性别: " << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "你输入的性别有误,请重新输入!" << endl;
}
//年龄
int age = 0;
cout << "请输入联系人的年龄: " << endl;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
//电话
string phone;
cout << "请输入联系人的电话: " << endl;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
//地址
string address;
cout << "请输入联系人的地址: " << endl;
cin >> address;
abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout << "联系人添加成功!" << endl;
//请按任意键操作
system("pause");
//清屏操作
system("cls");
}
//显示联系人功能
void showPerson(AddressBooks * abs) {
if (abs->m_Size == 0) {
cout << "当前通讯录为空!" << endl;
}
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 name) {
for (int i = 0; i < abs->m_Size; i++) {
if (abs->personArray[i].m_Name == name) {
return i;
}
}
return -1;
}
//定义一个方法来根据联系人名称删除指定联系人
void deletePerson(AddressBooks * abs) {
string name;
cout << "请输入你要删除联系人的名字:" << endl;
cin >> name;
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 << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//定义一个方法来查找联系人
void findPerson(AddressBooks * abs) {
string name;
cout << "请输入你要查找的联系人的姓名:" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
cout << "姓名: " << abs->personArray[ret].m_Name << "\t";
cout << "性别: " << (abs->personArray[ret].m_Sex==1?"男":"女") << "\t";
cout << "年龄: " << abs->personArray[ret].m_Age << "\t";
cout << "电话: " << abs->personArray[ret].m_Phone << "\t";
cout << "地址: " << abs->personArray[ret].m_Addr << endl;
}
else {
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//定义修改联系人的方法
void updatePerson(AddressBooks * abs) {
string name;
cout << "请输入你要修改的联系人的姓名:" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
string nextName;
cout << "请输入修改的姓名:" << endl;
cin >> nextName;
abs->personArray[ret].m_Name = nextName;
int nextSex;
cout << "请输入修改的性别:" << endl;
cin >> nextSex;
abs->personArray[ret].m_Sex = nextSex;
int nextAge;
cout << "请输入修改的年龄:" << endl;
cin >> nextAge;
abs->personArray[ret].m_Age = nextAge;
string nextPhone;
cout << "请输入修改的电话:" << endl;
cin >> nextPhone;
abs->personArray[ret].m_Phone = nextPhone;
string nextAddr;
cout << "请输入修改的地址:" << endl;
cin >> nextAddr;
abs->personArray[ret].m_Addr = nextAddr;
cout << "修改成功!" << endl;
}
else {
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//定义一个方法清空通讯录
void cleanAddress(AddressBooks * abs) {
abs->m_Size = 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;
//初始化通讯录中联系人为0
abs.m_Size = 0;
int select = 0;//定义一个用户的选项,默认为0
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //添加联系人
addPerson(&abs); //将通讯录的变量地址传入
break;
case 2: //显示联系人
showPerson(&abs);
break;
case 3: //删除联系人
deletePerson(&abs);
break;
case 4: //查找联系人
findPerson(&abs);
break;
case 5: //修改联系人
updatePerson(&abs);
break;
case 6: //清空通讯录
cleanAddress(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
C++实现通讯录管理系统
最新推荐文章于 2025-03-12 10:43:43 发布