#include<iostream>
using namespace std;
#define Max 1000
//初始化1:展示功能菜单
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;
}
//初始化2:设计联系人结构体
struct Person
{
//姓名
string m_Name;
//性别 1男 2女
int m_Sex;
//年龄
int m_Age;
//电话
string m_Phone;
};
//初始化3:设计通讯录结构体
struct Addressbooks
{
//通讯录中保存的联系人数组
struct Person personArray[Max];
//通讯录中当前记录联系人个数
int m_Size=0;
};
//检测联系人是否存在,如果存在 返回联系人所在的数组的具体位置;不存在返回-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; //没找到用户输入的姓名
}
//功能1:添加联系人
void addPerson(Addressbooks *abs)
{
if (abs->m_Size==Max) {
cout<<"通讯录已满,无法添加!" << endl;
return;
}
else {
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
//性别 1男 2女
int sex=0;
cout << "请输入性别(1男 2女):" << endl;
cin >> sex;
while ((sex!=1 ) && (sex!=2) )
{
cout << "输入错误!!!" << endl;
cout << "1——男" << endl;
cout << "2——女" << endl;
cout << "请重新输入..." << endl;
cin >> sex;
}
abs->personArray[abs->m_Size].m_Sex = sex;
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;
//添加成功 人数加一;
abs->m_Size++;
cout << "添加成功!" << endl;
system("pause"); //请按任意键继续
system("cls"); //清屏操作
}
}
//功能2:显示联系人
void showPerson(Addressbooks* abs)
{
if (abs->m_Size == 0) {
cout << "通讯录暂无数据!" << endl;
}
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<< endl;
}
system("pause"); //请按任意键继续
system("cls"); //清屏操作
}
//功能3:删除联系人
void deletPerson(Addressbooks *abs,string name)
{
int ret = isExist(abs, name);
if (ret == -1)
{
cout<<"查无此人"<< endl;
}
else {
cout << "查找成功,是通讯录第 "<< ret +1<<" 位用户" << endl;
for (int i = ret; i < abs->m_Size; i++)
{ //数据前移
abs->personArray[ret].m_Name = abs->personArray[ret +1].m_Name;
abs->personArray[ret].m_Age = abs->personArray[ret + 1].m_Age;
abs->personArray[ret].m_Phone = abs->personArray[ret + 1].m_Phone;
abs->personArray[ret].m_Sex = abs->personArray[ret + 1].m_Sex;
}
abs->m_Size--; //切忘数组减一 ,不然会有一个位置数据错误
cout <<"删除成功!"<< endl;
system("pause"); //请按任意键继续
system("cls"); //清屏操作
}
}
//功能4:查找联系人
void findPerson(Addressbooks* abs)
{
string name;
cout << "请输入要 查找 联系人的姓名..." << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
cout << "操作成功!"<< "\t";
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 << endl;
system("pause"); //请按任意键继续
system("cls"); //清屏操作
}
//功能5:更新联系人
void updataPerson(Addressbooks* abs)
{
string name;
cout << "请输入要 修改 联系人的姓名..." << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else {
cout << "请输入更新姓名..." << endl;
string name;
cin >> name;
abs->personArray[ret].m_Name = name;
cout << "请输入更新性别..." << endl;
int sex=0;
cin >> sex;
abs->personArray[ret].m_Sex = sex;
cout << "请输入更新年龄..." << endl;
int age=0;
cin >> age;
abs->personArray[ret].m_Age = age;
cout << "请输入更新电话..." << endl;
string phone;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
}
cout << "更新成功!" << endl;
system("pause"); //请按任意键继续
system("cls"); //清屏操作
}
//功能6:清空联系人
void clearPerson(Addressbooks* abs)
{
abs->m_Size = 0;
cout << "清空成功!" << endl;
system("pause"); //请按任意键继续
system("cls"); //清屏操作
}
int main()
{
//创建通讯录结构体变量
Addressbooks abs;
int select = 0;
while (true)
{
//菜单调用
showMenu();
cin >> select;
switch (select)
{
case 1: //1、添加联系人
addPerson(&abs);
break;
case 2: //2、显示联系人
showPerson(&abs);
break;
case 3: //3、删除联系人
{
string name;
cout<<"请输入要 删除 联系人的姓名..." << endl;
cin >> name;
deletPerson(&abs, name);
break;
}
case 4: //4、查找联系人
findPerson(&abs);
break;
case 5: //5、更新联系人
updataPerson(&abs);
break;
case 6: //6、清空联系人
clearPerson(&abs);
break;
case 0: //0、退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
default:
break;
}
}
}
c++——通讯录管理系统
最新推荐文章于 2024-05-23 20:19:57 发布