C++ 通讯录管理系统
Posted a people
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 通讯录管理系统相关的知识,希望对你有一定的参考价值。
该项目属于C++的入门练习,旨在加深对程序的3种控制结构、函数的定义与使用、结构体、数组等概念理解。
using namespace std;
struct person
{
string m_name;
int m_sex;
int m_age;
string m_phone;
string m_addr;
};
struct Addressbooks
{
struct person personArray[MAX];
int m_size=0;
};
void showmenu();
void addPerson(Addressbooks* abs);
void showPerson(Addressbooks* abs);
void deletePerson(Addressbooks* abs);
void findPerson(Addressbooks* abs);
void modifyPerson(Addressbooks* abs);
void cleanPerson(Addressbooks* abs);
int main()
{
Addressbooks abs;
abs.m_size = 0;
int select = 0;
while (true)
{
showmenu();
cin >> select;
switch (select)
{
case 1://添加联系人
addPerson(&abs);
break;
case 2://显示联系人
showPerson(&abs);
break;
case 3://删除联系人
{
cout << "请输入删除联系人的姓名:" << endl;
string name;
cin >> name;
deletePerson(&abs);
}
break;
case 4://查找联系人
findPerson(&abs);
break;
case 5://修改联系人
modifyPerson(&abs);
break;
case 6://清空联系人
cleanPerson(&abs);
break;
case 0://退出通讯录
cout << "您已退出程序,欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
}
//菜单显示
void showmenu()
{
cout << "1 添加联系人" << endl;
cout << "2 显示联系人" << endl;
cout << "3 删除联系人" << endl;
cout << "4 查找联系人" << endl;
cout << "5 修改联系人" << endl;
cout << "6 清空联系人" << endl;
cout << "0 退出通讯录" << endl;
}
//添加联系人
void addPerson(Addressbooks* abs)
{
if (abs->m_size == MAX) { cout << "通讯录已满,无法添加!" << endl; }
else
{
//名字
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_size].m_name = name;
//性别
int sex;
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_size].m_sex = sex;
break;
}
cout << "输入有误,请重新输入!" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age;
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");
}
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)
{
cout << "请输入您要删除的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; ret < 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 modifyPerson(Addressbooks* abs)
{
string name;
cout << "请输入您要修改的联系人:" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
//姓名
string name;
cin >> name;
abs->personArray[ret].m_name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_size].m_sex = sex;
break;
}
cout << "输入有误,请重新输入!" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age;
cin >> age;
abs->personArray[ret].m_age = age;
//电话
string phone;
cout << "请输入电话号码:" << endl;
cin >> phone;
abs->personArray[ret].m_phone = phone;
//住址
string address;
cout << "请输入住址:" << endl;
cin >> address;
abs->personArray[ret].m_addr = address;
cout << "您已修改成功!" << endl;
}
else
{
cout << "无此人" << endl;
}
system("pause");
system("cls");
}
//清空联系人
void cleanPerson(Addressbooks* abs)
{
abs->m_size = 0;
cout << "通讯录已清空!" << endl;
system("pause");
system("cls");
}
很高兴您能坚持看到这,以上便是该项目的全部代码。
写文章不易,大家的点赞,收藏还有大大的关注便是我持续创作的不懈动力!
以上是关于C++ 通讯录管理系统的主要内容,如果未能解决你的问题,请参考以下文章