C++实现通讯录的相关功能
1.实现功能
1.显示操作目录
2.添加联系人
3.显示联系人
4.删除联系人
5.修改联系人
6.清空通讯录
7.退出通讯录
2.思路梳理
创建两个结构体:一个表示通讯录(包括,联系人和联系人数量),一个表示联系人(保存联系人的姓名、年龄、性别、电话、家庭住址).对结构体进行相关操作
3.结构体的建立
struct Person{
string name;
string sex;
int age;
string tele;
string address;
};
struct addressBook
{
struct Person myFriend[MAX];
int num;
};
4.主函数设计
int main() {
int flag = 1;
addressBook myBook;
myBook.num = 0;
while (flag) {
showMenu();
cout << endl << "请选择操作:" << endl;
cin >> flag;
switch (flag)
{
case 1:
addBook(&myBook);
break;
case 2:
showBook(&myBook);
break;
case 3:
deleBook(&myBook);
break;
case 4:
findBook(&myBook);
break;
case 5:
modifyBook(&myBook);
break;
case 6:
cleanBook(&myBook);
break;
default:
break;
}
}
cout << "欢迎下次使用!";
}
5.显示菜单
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;
}
6.添加联系人
void addBook(struct addressBook *book) {
if (book->num >= MAX) {
cout << "超出限制";
}
else {
cout << "请输入联系人姓名:";
cin >> book->myFriend[book->num].name;
cout << "请输入联系人性别:";
cin >> book->myFriend[book->num].sex;
cout << "请输入联系人年龄:";
cin >> book->myFriend[book->num].age;
cout << "请输入联系人电话:";
cin >> book->myFriend[book->num].tele;
cout << "请输入联系人地址:";
cin >> book->myFriend[book->num].address;
book->num += 1;
}
cout << "添加成功!" << endl << endl;
}
7.显示通讯录
void showBook(struct addressBook *book) {
if (book->num == 0) {
cout << "没有联系人" << endl;
}
else {
for (int i = 0; i < book->num; i++) {
cout << "联系人" << i+1 << ":" << endl;
cout << "联系人姓名:" << book->myFriend[i].name << endl;
cout << "联系人性别:" << book->myFriend[i].sex << endl;
cout << "联系人年龄:" << book->myFriend[i].age << endl;
cout << "联系人电话:" << book->myFriend[i].tele << endl;
cout << "联系人地址:" << book->myFriend[i].address << endl << endl;
}
}
}
8.判断联系人是否存在
int isExist(string deleName,struct addressBook book) {
for (int i = 0; i < book.num; i++) {
if (deleName == book.myFriend[i].name) {
return i;
}
}
return -1;
}
9.删除联系人
int deleBook(struct addressBook* book) {
string deleName;
int flag=0;
if (book->num == 0) {
cout << "没有联系人" << endl;
return 0;
}
cout << "输入删除联系人姓名:";
cin >> deleName;
flag = isExist(deleName,*book);
if (flag != -1) {
for (int i = flag; i < book->num - 1; i++) {
book->myFriend[i] = book->myFriend[i + 1];
}
book->num -= 1;
cout << "删除成功!" << endl << endl;
}
else {
cout << "没有联系人" << endl << endl;
}
}
10.查找联系人
void findBook(struct addressBook* book) {
string findName;
int flag;
cout << "输入查询联系人姓名:";
cin >> findName;
flag = isExist(findName, *book);
if (flag != -1) {
cout << "联系人姓名:" << book->myFriend[flag].name << endl;
cout << "联系人性别:" << book->myFriend[flag].sex << endl;
cout << "联系人年龄:" << book->myFriend[flag].age << endl;
cout << "联系人电话:" << book->myFriend[flag].tele << endl;
cout << "联系人地址:" << book->myFriend[flag].address << endl << endl;
}
else {
cout << "没有该联系人!" << endl << endl;
}
}
11.修改联系人
void modifyBook(struct addressBook* book) {
string modifyName;
cout << "输入需要修改的联系人姓名:" ;
cin >> modifyName;
int flag;
flag = isExist(modifyName, *book);
if (flag != -1) {
cout << "请输入联系人姓名:";
cin >> book->myFriend[flag].name;
cout << "请输入联系人性别:";
cin >> book->myFriend[flag].sex;
cout << "请输入联系人年龄:";
cin >> book->myFriend[flag].age;
cout << "请输入联系人电话:";
cin >> book->myFriend[flag].tele;
cout << "请输入联系人地址:";
cin >> book->myFriend[flag].address;
cout << "修改成功!" << endl << endl;
}
else {
cout << "没有该联系人!" << endl << endl;
}
}
12.清空联系人
void cleanBook(struct addressBook* book) {
char flag;
cout << "确定清空联系人? 输入‘y’确定";
cin >> flag;
if (flag == 'y') {
book->num = 0;
cout << "清空成功!" << endl << endl;
}
else {
cout << "取消清空!" << endl << endl;
}
}
**
【注】C++学习新人,如有错误请指正
**