C++通讯录管理系统
作者:互联网
通讯录管理系统
- 菜单显示
- 添加联系人
- 显示联系人
- 删除联系人
- 查找联系人
- 修改联系人
- 退出通讯录
菜单显示
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;
//性别:1男 2女
int m_Sex;
//年龄
int m_Age;
//电话
string m_Phone;
//住址
string m_Addr;
};
//通讯录结构体
struct Addressbooks
{
//通讯录中保存的结构体数组
Person personArray[1000];
//通讯录中人数的个数
int m_Size;
};
//添加联系人
void Addperson(Addressbooks *abs)
{
//判断通讯录是否为满
if(abs->m_Size == 1000 )
{
cout << "通讯录已满" << endl;
return;
}
else
{
//添加姓名
string name;
cout << "请输入要添加的名字" << endl;
cin >> name;
abs->personArray[abs->m_Size].name = name;
//添加性别
cout << "请输入性别" << endl;
cout <<"1---男" << endl;
cout << "2---女" << endl;
int sex = 0;
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;
//添加地址
cout << "请输入地址" << endl;
string address;
cin >> address;
abs->personArray[abs->m_Size].m_Addr = address;
//添加电话
cout << "请输入联系电话" << endl;
string phone;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
//更新通讯录人数
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].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;
}
}
}
删除联系人
- 判断联系人是否存在
- 若存在则后面一个覆盖前面一个
//检测联系人是否存在
int isExit(Addressbooks *abs,string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if(abs->personArray[i].name == name)
{
return i;//返回该联系人的下标
}
}
return -1;
}
//删除联系人
void delectPerson(Addressbooks *abs)
{
string name;
cin >> name;
//判断这个人是否存在
int rev = isExit(abs,name);
if (rev != -1)
{
for ( int i = rev; i < abs->m_Size; i++)
{
abs->personArray[i] = abs->personArray[i+1];
}
abs->m_Size--;
cout << "删除成功" << endl;
}
else
{
cout << "此人不存在" << endl;
}
}
查找联系人
- 判断该联系人是否存在
- 存在则显示出他的全部信息
//查找联系人
void findPerson(Addressbooks *abs)
{
cout << "请输入你要查找的人联系人" << endl;
string name;
cin >> name;
//判断该联系人是否存在
int rev = isExit(abs,name);
if(rev != -1)
{
cout << "姓名:" << abs->personArray[rev].name << "\t";
cout << "性别 :"<< (abs->personArray[rev].m_Sex == 1 ? "男":"女") << "\t";
cout << "年龄:" << abs->personArray[rev].m_Age << "\t";
cout << "电话:" << abs->personArray[rev].m_Phone << "\t";
cout << "住址:" << abs->personArray[rev].m_Addr << endl;
}
else
{
cout << "此人不存在" << endl;
}
}
修改联系人
- 判断该联系人是否存在
- 存在则修改
//修改联系人
void modifyPerson(Addressbooks *abs)
{
cout << "请输入你要修改的人联系人名字" << endl;
string name;
cin >> name;
//判断该联系人是否存在
int rev = isExit(abs,name);
if(rev != -1)
{
//添加姓名
string name;
cout << "请输入要修改的名字" << endl;
cin >> name;
abs->personArray[rev].name = name;
cout << "请输入性别" << endl;
cout <<"1---男" << endl;
cout << "2---女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if(sex == 1 || sex ==2)
{
abs->personArray[rev].m_Sex = sex;
break;
}
cout << "你输入的性别有错误,请重新输入" << endl;
}
//添加年龄
cout << "请输入年龄"<< endl;
int age;
cin >> age;
abs->personArray[rev].m_Age = age;
//添加地址
cout << "请输入地址" << endl;
string address;
cin >> address;
abs->personArray[rev].m_Addr = address;
//添加电话
cout << "请输入联系电话" << endl;
string phone;
cin >> phone;
abs->personArray[rev].m_Phone = phone;
//提醒用户添加成功
cout << "修改成功" << endl;
}
else
{
cout << "此人不存在" << endl;
}
}
清空联系人
//清空联系人
void cleanPerson(Addressbooks *abs)
{
abs->m_Size = 0;
cout << "通讯录已经清空" << endl;
}
完整代码
#include <iostream>
#include <string>
using namespace std;
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;
//性别:1男 2女
int m_Sex;
//年龄
int m_Age;
//电话
string m_Phone;
//住址
string m_Addr;
};
//通讯录结构体
struct Addressbooks
{
//通讯录中保存的结构体数组
Person personArray[1000];
//通讯录中人数的个数
int m_Size;
};
//添加联系人
void Addperson(Addressbooks *abs)
{
//判断通讯录是否为满
if(abs->m_Size == 1000 )
{
cout << "通讯录已满" << endl;
return;
}
else
{
//添加姓名
string name;
cout << "请输入要添加的名字" << endl;
cin >> name;
abs->personArray[abs->m_Size].name = name;
//添加性别
cout << "请输入性别" << endl;
cout <<"1---男" << endl;
cout << "2---女" << endl;
int sex = 0;
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;
//添加地址
cout << "请输入地址" << endl;
string address;
cin >> address;
abs->personArray[abs->m_Size].m_Addr = address;
//添加电话
cout << "请输入联系电话" << endl;
string phone;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
//更新通讯录人数
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].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;
}
}
}
//检测联系人是否存在
int isExit(Addressbooks *abs,string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if(abs->personArray[i].name == name)
{
return i;//返回该联系人的下标
}
}
return -1;
}
//删除联系人
void delectPerson(Addressbooks *abs)
{
string name;
cin >> name;
//判断这个人是否存在
int rev = isExit(abs,name);
if (rev != -1)
{
for ( int i = rev; i < abs->m_Size; i++)
{
abs->personArray[i] = abs->personArray[i+1];
}
abs->m_Size--;
cout << "删除成功" << endl;
}
else
{
cout << "此人不存在" << endl;
}
}
//查找联系人
void findPerson(Addressbooks *abs)
{
cout << "请输入你要查找的人联系人" << endl;
string name;
cin >> name;
//判断该联系人是否存在
int rev = isExit(abs,name);
if(rev != -1)
{
cout << "姓名:" << abs->personArray[rev].name << "\t";
cout << "性别 :"<< (abs->personArray[rev].m_Sex == 1 ? "男":"女") << "\t";
cout << "年龄:" << abs->personArray[rev].m_Age << "\t";
cout << "电话:" << abs->personArray[rev].m_Phone << "\t";
cout << "住址:" << abs->personArray[rev].m_Addr << endl;
}
else
{
cout << "此人不存在" << endl;
}
}
//修改联系人
void modifyPerson(Addressbooks *abs)
{
cout << "请输入你要修改的人联系人名字" << endl;
string name;
cin >> name;
//判断该联系人是否存在
int rev = isExit(abs,name);
if(rev != -1)
{
//添加姓名
string name;
cout << "请输入要修改的名字" << endl;
cin >> name;
abs->personArray[rev].name = name;
cout << "请输入性别" << endl;
cout <<"1---男" << endl;
cout << "2---女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if(sex == 1 || sex ==2)
{
abs->personArray[rev].m_Sex = sex;
break;
}
cout << "你输入的性别有错误,请重新输入" << endl;
}
//添加年龄
cout << "请输入年龄"<< endl;
int age;
cin >> age;
abs->personArray[rev].m_Age = age;
//添加地址
cout << "请输入地址" << endl;
string address;
cin >> address;
abs->personArray[rev].m_Addr = address;
//添加电话
cout << "请输入联系电话" << endl;
string phone;
cin >> phone;
abs->personArray[rev].m_Phone = phone;
//提醒用户添加成功
cout << "修改成功" << endl;
}
else
{
cout << "此人不存在" << endl;
}
}
//清空联系人
void cleanPerson(Addressbooks *abs)
{
abs->m_Size = 0;
cout << "通讯录已经清空" << endl;
}
int main(int argc, char const *argv[])
{
int select;
//初始化通讯录
Addressbooks abs;
abs.m_Size = 0;
while (true)
{
//显示菜单
showMenu();
cin>>select;
switch (select)
{
case 1:
Addperson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
delectPerson(&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;
}
}
return 0;
}
标签:cout,管理系统,rev,C++,abs,通讯录,personArray,Size,name 来源: https://blog.csdn.net/weixin_51747047/article/details/117043829