#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
enum option//枚举让switch函数里面更清晰
{
EXIT,
ADD,
SHOW,
DEL,
MADIFY,
EMPTY,
SEARCH
};
struct person
{
string m_Name;
int m_sex;//1 男 2女
int m_age;
string m_phone;
string m_address;
};
struct contact//通讯录结构体
{
struct person personarr[MAX];
int m_size;
};
void AddPerson(contact* con)//添加
{
if (con->m_size==MAX)
{
cout << "已满" << endl;
return;
}
else
{
string name;
cout << "请输入姓名" << endl;
cin >> name;
con->personarr[con->m_size].m_Name = name;
int sex=0;
cout << "请输入性别" << endl;
cout << "1 为男 2 为女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
con->personarr[con->m_size].m_sex = sex;
break;
}
cout << "输入错误,请重新输入" << endl;
}
int age;
cout << "请输入年龄" << endl;
cin >> age;
con->personarr[con->m_size].m_age = age;
string phone;
cout << "请输入电话" << endl;
cin >> phone;
con->personarr[con->m_size].m_phone = phone;
string address;
cout << "请输入地址" << endl;
cin >> address;
con->personarr[con->m_size].m_address = address;
con->m_size++;
cout << " 添加成功" << endl;
system("pause");
system("cls");
}
}
void ShowPerson(contact* con)//显示
{
if (con->m_size == 0)
{
cout << "为空" << endl;
}
else
{
for (int i = 0; i < con->m_size; i++)
{
cout << "姓名:" << con->personarr[i].m_Name << "\t";
cout << "性别:" << (con->personarr[i].m_sex==1 ? "男":"女") << "\t";
cout << "年龄:" << con->personarr[i].m_age << "\t";
cout << "电话:" << con->personarr[i].m_phone << "\t";
cout << "地址:" << con->personarr[i].m_address << endl;
}
system("pause");
system("cls");
}
}
int isExit(contact* con, string name)//判断是否存在
{
for (int i = 0; i < con->m_size; i++)
{
if (con->personarr[i].m_Name == name)
{
return i;
}
}
return -1;
}
void DelPerson(contact* con)//删除
{
cout << "请输入删除人的姓名" << endl;
string name;
cin >> name;
int ret = isExit(con, name);
if (ret == -1)
{
cout << "不存在" << endl;
}
else
{
for (int i = ret; i < con->m_size; i++)
{
con->personarr[i] = con->personarr[i + 1];
con->m_size--;
}
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void MadifyPerson(contact* con)//修改
{
cout << "请输入修改人的姓名" << endl;
string name;
cin >> name;
int ret = isExit(con, name);
if (ret == -1)
{
cout << "不存在" << endl;
}
else
{
string name;
cout << "请重新输入姓名" << endl;
cin >> name;
con->personarr[ret].m_Name = name;
int sex = 0;
cout << "请重新输入性别" << endl;
cout << "1 为男 2 为女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
con->personarr[ret].m_sex = sex;
break;
}
cout << "输入错误,请重新输入" << endl;
}
int age;
cout << "请重新输入年龄" << endl;
cin >> age;
con->personarr[ret].m_age = age;
string phone;
cout << "请重新输入电话" << endl;
cin >> phone;
con->personarr[ret].m_phone = phone;
string address;
cout << "请重新输入地址" << endl;
cin >> address;
con->personarr[ret].m_address = address;
cout << " 修改成功" << endl;
}
system("pause");
system("cls");
}
void CleanPerson(contact* con)//清空
{
con->m_size = 0;
cout << "清空完毕" << endl;
system("pause");
system("cls");
}
void SearchPerson(contact* con)//查找
{
cout << "请输入查找人的姓名" << endl;
string name;
cin >> name;
int ret = isExit(con, name);
if (ret == -1)
{
cout << "不存在" << endl;
}
else
{
cout << "姓名:" << con->personarr[ret].m_Name << "\t";
cout << "性别:" << (con->personarr[ret].m_sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << con->personarr[ret].m_age << "\t";
cout << "电话:" << con->personarr[ret].m_phone << "\t";
cout << "地址:" << con->personarr[ret].m_address << endl;
cout << "查找成功" << endl;
}
system("pause");
system("cls");
}
void menu()//菜单
{
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()
{
int select;
contact con;//创建结构体变量
con.m_size = 0;//初始化
do
{
menu();
cout << "请选择:" << endl;
cin >> select;
switch (select)
{
case ADD:
AddPerson(&con);//添加联系人
break;
case SHOW:
ShowPerson(&con);//显示
break;
case DEL:
DelPerson(&con);//删除
break;
case MADIFY:
MadifyPerson(&con);//修改
break;
case EMPTY:
CleanPerson(&con);//清空
break;
case SEARCH:
SearchPerson(&con);//查找
break;
case EXIT:
cout << "欢迎下次使用" << endl;//退出
break;
default:
cout << "输入错误,请重新输入"<<endl;
break;
}
} while (select);
system("pause");
return 0;
}
c++实现通讯录的增删查改功能
最新推荐文章于 2025-05-16 00:26:46 发布