花了一下午的时间,编写通讯录管理系统,大部分是自己写的,和黑马的思路一样,我感觉最重要的就是思路。比如黑马封装了一个查找人姓名,返回数组序号的函数,这就极大方便了,这个联系人显示,修改、删除的功能,还有就是,黑马利用switch选择结构,而我最开始没想到,还是多练才能打开思路啊,以下附上该项目全部代码:
#include <iostream>
using namespace std;
#include<string>
//创建联系人的结构体
struct person {
string name;
int sex;
int age;
string phone;
string address;
};
#define MAXPEOPLE 100
// 创建通讯录结构体
struct peoplebooks {
struct person personmessage[MAXPEOPLE];
int people_size;
};
// 在main函数中创建通讯录
// 封装添加联系人的函数
//封装显示联系人的函数
//创建菜单显示
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(struct peoplebooks* arr) {
//添加联系人
if (arr->people_size > MAXPEOPLE) {
cout << "通讯录已满,无法添加" << endl;
return;
}
else {
cout << "请添加姓名:" << endl;
string name;
cin >> name;
arr->personmessage[arr->people_size].name=name;
cout << "请添加性别(1男、2女):" << endl;
int sex;
cin >> sex;
while (1) {
if (sex == 1 || sex == 2) {
arr->personmessage[arr->people_size].sex=sex;
break;
}
else {
cout << "输入不正确,请重新输入(1男、2女):";
}
}
cout << "请添加年龄:" << endl;
int age;
cin >> age;
arr->personmessage[arr->people_size].age = age;
cout << "请添加电话号码:" << endl;
string phone;
cin >> phone;
arr->personmessage[arr->people_size