黑马C++教程第一个C++基础案例
本次案例要求是做一个通讯录管理系统,这次案例在C++基础部分教学之后,算是比较简单的一个小案例,适合初学者边学边上手C++,话不多说先上要求。
![通讯录管理系统要求](https://i-blog.csdnimg.cn/blog_migrate/4d1b7b008f8a15a63d449c0a7815362b.png)
#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
struct person
{
string name;
int age;
int gender;
string phone;
};
struct addressbooks
{
int p_number;
person persons[MAX];
};
void display()
{
cout << "************************" << endl;
cout << "***** 1.添加联系人 *****" << endl;
cout << "***** 2.显示联系人 *****" << endl;
cout << "***** 3.删除联系人 *****" << endl;
cout << "***** 4.查找联系人 *****" << endl;
cout << "***** 5.修改联系人 *****" << endl;
cout << "***** 6.清空联系人 *****" << endl;
cout << "***** 7. 退出 *****" << endl;
cout << "************************" << endl;
}
void addPerson(addressbooks* ab)
{
cout << "请输入联系人姓名:" << endl;
string name;
cin >> name;
ab->persons[ab->p_number].name = name;
cout << "请输入联系人年龄:" << endl;
int age;
cin >> age;
ab->persons[ab->p_number].age = age;
int gender = 0;
while (true)
{
cout << "请输入联系人性别:" << endl;
cout << "1-->男,0-->女" << endl;
cin >> gender;
if (gender == 1 || gender == 0)
{
ab->persons[ab->p_number].gender = gender;
break;
}
else
{
cout << "输入有误" << endl;
}
}
cout << "请输入联系人电话:" << endl;
string phone;
cin >> phone;
ab->persons[ab->p_number].phone = phone;
ab->p_number++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
void showPerson(addressbooks * ab)
{
cout << "性别中1表示男,0表示女" << endl;
for (int i = 0; i < ab->p_number; i++) {
cout << "姓名:" << ab->persons[i].name << " \t"
<< "年龄:" << ab->persons[i].age << " \t"
<< "性别:" << ab->persons[i].gender << " \t"
<< "电话:" << ab->persons[i].phone << endl;
}
system("pause");
system("cls");
}
int isExist(addressbooks* ab, string name)
{
for (int i = 0; i < ab->p_number; i++)
{
if (ab->persons[i].name == name)
{
return i;
}
}
return -1;
}
void deletePerson(addressbooks * ab)
{
string name;
cout << "请输入要删除的联系人姓名" << endl;
cin >> name;
int ie;
ie = isExist(ab, name);
if (ie != -1)
{
for (int i = ie; i < ab->p_number; i++)
{
ab->persons[i] = ab->persons[i + 1];
}
cout << "删除成功" << endl;
ab->p_number--;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void findPerson(addressbooks* ab)
{
cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
int ie = isExist(ab, name);
if (ie != -1)
{
cout << "姓名:" << ab->persons[ie].name << " \t"
<< "年龄:" << ab->persons[ie].age << " \t"
<< "性别:" << ab->persons[ie].gender << " \t"
<< "电话:" << ab->persons[ie].phone << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void modify(addressbooks* ab)
{
cout << "请输入要修改的联系人的姓名" << endl;
string name;
cin >> name;
int ie = isExist(ab, name);
if (ie != -1)
{
cout << "请输入联系人姓名:" << endl;
string name;
cin >> name;
ab->persons[ie].name = name;
cout << "请输入联系人年龄:" << endl;
int age;
cin >> age;
ab->persons[ie].age = age;
int gender = 0;
while (true)
{
cout << "请输入联系人性别:" << endl;
cout << "1-->男,0-->女" << endl;
cin >> gender;
if (gender == 1 || gender == 0)
{
ab->persons[ie].gender = gender;
break;
}
else
{
cout << "输入有误" << endl;
}
}
cout << "请输入联系人电话:" << endl;
string phone;
cin >> phone;
ab->persons[ie].phone = phone;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void empty(addressbooks* ab)
{
while (true)
{
cout << "是否真的要清空联系人?请输入Y/N" << endl;
char isEmpty;
cin >> isEmpty;
if (isEmpty == 'Y')
{
ab->p_number = 0;
cout << "清空成功" << endl;
break;
}
else if (isEmpty == 'N')
{
cout << "好的,不清空" << endl;
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
system("pause");
system("cls");
}
int main()
{
addressbooks ab;
ab.p_number = 0;
while (true)
{
display();
cout << "请选择你的操作,请输入数字" << endl;
int num;
cin >> num;
switch (num)
{
case 1:
addPerson(&ab);
break;
case 2:
showPerson(&ab);
break;
case 3:
deletePerson(&ab);
break;
case 4:
findPerson(&ab);
break;
case 5:
modify(&ab);
break;
case 6:
empty(&ab);
break;
case 7:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;//退出程序
break;
default:
cout << "输入数据有误,请重新输入" << endl;
break;
}
}
system("pause");
return 0;
}
- 其中包括两个结构体person和addressbooks,其中person用来存储基本信息,而addressbooks存储了通讯录中人数和一个person数组。
- 7个方法对应7个需求,其中isExist方法用以判断通讯录中是否有对应成员。
- 代码的健壮性并不强,还要多加学习。