有问题可以私,也在成长阶段,界面非常简单
//通讯录管理系统
#include<iostream>
#include<string>
#include<cstdlib>//system有时候会出现红色下划线,加此头文件
using namespace std;
#define MAX 1000//可随时修改大小
//设计联系人的结构体
struct Person
{
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//设计通讯录结构体
struct Addressbook
{
//通讯录中保存的联系人的数组
struct Person personArray[MAX];
//通讯录中当前联系人个数
int m_size;
};
//菜单界面
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;
}
void addPerson(Addressbook *abs)//添加联系人,包括姓名、性别、年龄、联系电话、家庭住址
{
//判断通讯录是否已满
if (abs->m_size == MAX) {
cout << "通讯录已满" << endl;
return;
}
else {//添加具体联系人
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_size].m_Name = name;
int sex=0;
cout << "请输入性别:" << endl;
cout << "1--男,2--女" << endl;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personArray[abs->m_size].m_Sex = sex;
break;
}
else
cout << "输入错误,重新输入" << endl;
}
int age;
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(Addressbook* 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 << "\t" << endl;
}
}
system("pause");
}
int isExist(Addressbook* 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(Addressbook* abs)
{
if (abs->m_size == 0) {
cout << "通讯录为空" << endl;
return;
}
else {
cout << "请输入要删除的联系人姓名:" << endl;
string name;
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];//i+1是空的
}
abs->m_size--;
cout << "删除成功" << endl;
}
else {
cout << "查无此人" << endl;
}
}
}
void searchPerson(Addressbook* abs)
{
cout << "请输入要查找的联系人姓名:" << endl;
string name;
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 << "\t" << endl;
}
else {
cout << "通讯录没有这个人" << endl;
}
}
void updatePerson(Addressbook* abs)
{
//判断通讯录是否为空
if (abs->m_size == 0) {
cout << "通讯录为空" << endl;
return;
}
else {//修改具体联系人
cout << "即将修改通讯录信息:" << endl;
string name;
cout << "请输入要修改的姓名:" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret!=-1) {
string name1;
cout << "请输入姓名:" << endl;
cin >> name1;
abs->personArray[ret].m_Name = name1;
int sex = 0;
cout << "请输入性别:" << endl;
cout << "1--男,2--女" << endl;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personArray[ret].m_Sex = sex;
break;
}
else
cout << "输入错误,重新输入" << endl;
}
int age;
cout << "请输入年龄:" << endl;
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;
}
else {
cout << "要修改的人不在通讯录中" << endl;
}
}
cout << "修改成功" << endl;
}
void deteleall(Addressbook* abs)
{
if (abs->m_size == 0) {
cout << "通讯录本就为空" << endl;
return;
}
else
{
while (abs->m_size != 0) {
for (int i = 0; i < abs->m_size; i++) {
abs->personArray[i] = abs->personArray[i + 1];
abs->m_size--;
}
}
}
cout << "通讯录已清空" << endl;
}
int main()
{
Addressbook abs;
abs.m_size = 0;
int select = 0;
while (true) {
showMenu();
cout << "请输入你的选择:" << endl;
cin >> select;
switch (select)
{
case 1://添加联系人
addPerson(&abs);
break;
case 2://显示联系人
showPerson(&abs);
break;
case 3://删除联系人
deletePerson(&abs);
break;
case 4://查找联系人
searchPerson(&abs);
break;
case 5://修改联系人
updatePerson(&abs);
break;
case 6://清空联系人
deteleall(&abs);
break;
case 0://退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}