C++,黑马程序员,通讯录
学完课程后自己写的,可以正常运行,或许有bug
#include<iostream>
using namespace std;
#define MAX 1000
//菜单界面
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 M_name;
int M_age;
int M_sex;
string M_phone;
string M_addion;
};
struct AddRessBooks{
struct Person ressbook_arry[MAX];
int arry_size;
};
void addPerson(AddRessBooks *abs) {
if (abs->arry_size == MAX )
{
cout << "本通讯录已满" << endl;
}
else
{
string name;
cout << "请输入联系人姓名" << endl;
cin >> name;
abs->ressbook_arry[abs->arry_size].M_name = name;
int sex;
cout << "请输入联系人性别" << endl;
cout << "1代表女性,2代表男性" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
break;
}
else
{
cout << "请重新输入" << endl;
}
}
abs->ressbook_arry[abs->arry_size].M_sex = sex;
int age;
cout << "请输入联系人年龄" << endl;
cin >> age;
abs->ressbook_arry[abs->arry_size].M_age = age;
string phone;
cout << "请输入联系人电话" << endl;
cin >> phone;
abs->ressbook_arry[abs->arry_size].M_phone = phone;
string addion;
cout << "请输入联系人住址" << endl;
cin >> addion;
abs->ressbook_arry[abs->arry_size].M_addion = addion;
abs->arry_size++;
cout << "添加成功,请继续下一步操作" << endl;
cout << "现在本通讯录共有" << abs->arry_size << "人" << endl;
system("pause");
system("cls");
}
}
void showAllPerson(AddRessBooks *abs) {
if (abs->arry_size == 0)
{
cout << "本通讯录为空" << endl;
}
else
{
cout << "本通讯录共有" << abs->arry_size << "人" << endl;
for (int i = 0; i < abs->arry_size; i++)
{
cout << "第" << i + 1 << "位联系人的姓名为:" << abs->ressbook_arry[i].M_name << endl;
if (abs->ressbook_arry[i].M_sex == 1 )
{
cout << "第" << i + 1 << "位联系人的性别为:" << "女" << endl;
}
else
{
cout << "第" << i + 1 << "位联系人的性别为:" << "男" << endl;
}
cout << "第" << i + 1 << "位联系人的年龄为:" << abs->ressbook_arry[i].M_age << endl;
cout << "第" << i + 1 << "位联系人的电话为:" << abs->ressbook_arry[i].M_phone << endl;
cout << "第" << i + 1 << "位联系人的住址为:" << abs->ressbook_arry[i].M_addion << endl;
}
cout << "本通讯录已经显示完毕" << endl;
system("pause");
system("cls");
}
}
int isExit(AddRessBooks *abs,string name) {
for (int i = 0; i < abs->arry_size; i++)
{
if (abs->ressbook_arry[i].M_name == name)
{
return i;
}
}
return -1;
}
void delPerson(AddRessBooks* abs) {
if (abs->arry_size == 0)
{
cout << "本通讯录为空" << endl;
}
else
{
cout << "请输入要删除人姓名" << endl;
string name;
cin >> name;
int delflage = isExit(abs, name);
if (delflage == -1)
{
cout << "查无此人,无法删除" << endl;
}
else
{
abs->ressbook_arry[delflage] = abs->ressbook_arry[delflage + 1];
abs->arry_size--;
cout << "该联系人已被成功删除" << endl;
}
cout << "现在本通讯录共有" << abs->arry_size << "人" << endl;
system("pause");
system("cls");
}
}
void showPeroson(AddRessBooks* abs) {
if (abs->arry_size == 0)
{
cout << "本通讯录为空" << endl;
}
else
{
cout << "请输入要显示人姓名" << endl;
string name;
cin >> name;
int delflage = isExit(abs, name);
if (delflage == -1)
{
cout << "查无此人,无法显示" << endl;
}
else
{
cout << "该联系人的姓名为:" << abs->ressbook_arry[delflage].M_name << endl;
if (abs->ressbook_arry[delflage].M_sex == 1)
{
cout << "该联系人的性别为:" << "女" << endl;
}
else
{
cout << "该联系人的性别为:" << "男" << endl;
}
cout << "该联系人的年龄为:" << abs->ressbook_arry[delflage].M_age << endl;
cout << "该联系人的电话为:" << abs->ressbook_arry[delflage].M_phone << endl;
cout << "该联系人的住址为:" << abs->ressbook_arry[delflage].M_addion << endl;
system("pause");
system("cls");
}
}
}
void repairPerson(AddRessBooks* abs) {
if (abs->arry_size == 0)
{
cout << "本通讯录为空" << endl;
}
else
{
cout << "请输入要修改人姓名" << endl;
string name;
cin >> name;
int flage = isExit(abs, name);
if (flage == -1)
{
cout << "查无此人,无法显示" << endl;
}
else
{
cout << "是否修改联系人姓名" << endl;
cout << "Y表示修改,N表示不修改" << endl;
string ynname;
while (true)
{
cin >> ynname;
if (ynname == "Y" || ynname == "N")
{
break;
}
else
{
cout << "请重新输入" << endl;
}
}
if (ynname == "Y")
{
cout << "请重新输入联系人名字" << endl;
string newname;
cin >> newname;
abs->ressbook_arry[flage].M_name = newname;
cout << "联系人姓名已被修改" << endl;
}
cout << "是否修改联系人性别" << endl;
cout << "Y表示修改,N表示不修改" << endl;
string ynsex;
while (true)
{
cin >> ynsex;
if (ynsex == "Y" || ynsex == "N")
{
break;
}
else
{
cout << "请重新输入" << endl;
}
}
if (ynsex == "Y")
{
int newsex;
cout << "请重新输入联系人性别" << endl;
cout << "1代表女性,2代表男性" << endl;
while (true)
{
cin >> newsex;
if (newsex == 1 || newsex == 2)
{
break;
}
else
{
cout << "请重新输入" << endl;
}
}
abs->ressbook_arry[flage].M_sex = newsex;
cout << "联系人姓名已被修改" << endl;
}
cout << "是否修改联系人年龄" << endl;
cout << "Y表示修改,N表示不修改" << endl;
string ynage;
while (true)
{
cin >> ynage;
if (ynage == "Y" || ynage == "N")
{
break;
}
else
{
cout << "请重新输入" << endl;
}
}
if (ynage == "Y")
{
cout << "请重新输入联系人年龄" << endl;
int newage;
cin >> newage;
abs->ressbook_arry[flage].M_age = newage;
cout << "联系人年龄已被修改" << endl;
}
cout << "是否修改联系人电话" << endl;
cout << "Y表示修改,N表示不修改" << endl;
string ynphone;
while (true)
{
cin >> ynphone;
if (ynphone == "Y" || ynphone == "N")
{
break;
}
else
{
cout << "请重新输入" << endl;
}
}
if (ynphone == "Y")
{
cout << "请重新输入联系人电话" << endl;
string newphone;
cin >> newphone;
abs->ressbook_arry[flage].M_phone = newphone;
cout << "联系人电话已被修改" << endl;
}
cout << "是否修改联系人住址" << endl;
cout << "Y表示修改,N表示不修改" << endl;
string ynaddion;
while (true)
{
cin >> ynaddion;
if (ynaddion == "Y" || ynaddion == "N")
{
break;
}
else
{
cout << "请重新输入" << endl;
}
}
if (ynaddion == "Y")
{
cout << "请重新输入联系人住址" << endl;
string newaddion;
cin >> newaddion;
abs->ressbook_arry[flage].M_addion = newaddion;
cout << "联系人住址已被修改" << endl;
}
system("pause");
system("cls");
}
}
}
void clsPerson(AddRessBooks* abs) {
cout << "确定清空本通讯录吗,输入Y表示确定" << endl;
string flage;
cin >> flage;
if (flage == "Y")
{
abs->arry_size = 0;
cout << "通讯录已清空" << endl;
}
system("pause");
system("cls");
}
int main() {
int select = 0;
AddRessBooks abs;
abs.arry_size = 0; // 通讯录初始化
while (true)
{
showMenu();
cout << "请输入操作" << endl;
cin >> select;
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showAllPerson(&abs);
break;
case 3:
delPerson(&abs);
break;
case 4:
showPeroson(&abs);
break;
case 5:
repairPerson(&abs);
break;
case 6:
clsPerson(&abs);
break;
case 0: // 退出
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
system("pause");
system("cls");
}
system("pause");
system("cls");
return 0;
}