点击蓝字
关注我们
来源于网络,侵删
一、前言
建议亲手写一遍代码,感受指针神奇的魅力;
可以帮助你更好的巩固知识体系,熟悉指针,结构体与函数一起使用时的妙处
完成通讯录管理系统所需知识体系
结构体
指针
函数的封装
指针与函数的结合使用
指针与结构体的结合使用
二、结构体
1、联系人结构体
struct person
{string name;//姓名string sex; //性别int age; //年龄string phone;//手机号string home;//地址};
2、通讯录结构体
struct addressbook
{struct person personArray[MAX]; //通讯录扩展到100;int size=0; //当前联系人个数(后面就相当于i++)
};
三、函数模块
void menu();//菜单
void putit(addressbook* add);//添加联系人
void showperson(addressbook* add);// 显示联系人
int if_include(addressbook* add, string name);//判断联系人
void deleteperson(addressbook* add, int i);//删除联系人
void findPerson( addressbook* add);//查找联系人
void cleanperson(struct addressbook* add);//清空所有联系人
1、菜单
void menu()
{cout << endl;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;
}
2、添加联系人
void putit(addressbook* add)//添加联系人功能
{if (add->size == MAX)cout << "通讯录已满" << endl;else{string name;string sex;int age;string phone;string home;cout << "请输入姓名" << endl;cin >> name;add->personArray[add->size].name = name;cout << "请输入姓别" << endl;cin >> sex;add->personArray[add->size].sex = sex;cout << "请输入年龄" << endl;cin >> age;add->personArray[add->size].age = age;cout << "请输入电话号码" << endl;cin >> phone;add->personArray[add->size].phone = phone;cout << "请输入家庭住址" << endl;cin >> home;add->personArray[add->size].home = home;add->size++;cout << "添加联系人成功" << endl;}system("pause");system("cls");menu();
}
添加联系人函数中为什么要使用指针?
因为值传递中形参无法改变实参;
而利用指针的地址传递可以通过函数中的形参改变实参;
3、显示联系人
void showperson(addressbook* add)
{for (int i = 0; i <add->size; i++){cout << "姓名:" << add->personArray[i].name;cout << "\t姓别:" << add->personArray[i].sex;cout << "\t年龄:" << add->personArray[i].age;cout << "\t电话号码:" << add->personArray[i].phone;cout << "\t家庭住址:" << add->personArray[i].home << endl;}system("pause");system("cls");menu();
}
4、判断联系人
int if_include(addressbook* add, string name)
{for (int i = 0; i < add->size; i++){if (name == add->personArray[i].name){return i;}else{return -1;}}}
5、删除联系人
void deleteperson(addressbook* add, int i)
{for (; i < add->size; i++){add->personArray[i] = add->personArray[i + 1];}system("pause");system("cls");
}
6、查找联系人
void findPerson( addressbook* add)
{cout << "请输入您想要查找的联系人:" << endl;string name;cin >> name;int ret = if_include(add, name);if (ret == -1){cout << "查无此人" << endl;}else{ //查到此人,进行显示操作int i = ret;cout << "姓名:" << add->personArray[i].name << "\t";cout << "性别:" << add->personArray[i].sex << "\t";cout << "年龄:" << add->personArray[i].age << "\t";cout << "联系方式:" << add->personArray[i].phone << "\t";cout << "地址:" << add->personArray[i].home << endl;}//按任意键清屏system("pause");system("cls");
}
7、清空所有联系人
void cleanperson(struct addressbook* add)//清空所有联系人
{cout << "是否确认清空?" << endl;cout << "1 --- 是" << endl;cout << "2 --- 否" << endl;int a;cin >> a;if (a == 1){add->size = 0;//将当前记录联系人数量置为0,做逻辑上的清空操作cout << "通讯录已清空" << endl;}system("pause");system("cls");
}
四、main函数
int main()
{menu();addressbook add;//定义一个通讯录int choice=1;while (choice != 0){cin >> choice;switch (choice)//选择{case 1: putit(&add);break;case 2: showperson(&add);break;case 3: {cout << "请输入你要删除的人的名字" << endl;string aname;cin >> aname;if (if_include(&add, aname) == -1){cout << "查无此人" << endl;break;}if (if_include(&add, aname)){deleteperson(&add, if_include(&add, aname));}}break; case 4:break;case 5:break;case 6:break;case 0: cout << "欢迎下次使用" << endl;return 0;break;default: {cout << "输入不合法,请重新输入" << endl;break; }}}
}
五、源代码
#include<iostream>
using namespace std;
#include<string>
#define MAX 100struct person
{string name;//姓名string sex; //性别int age; //年龄string phone;//手机号string home;//地址};
struct addressbook
{struct person personArray[MAX]; //通讯录扩展到100;int size=0; //当前联系人个数(后面就相当于i++)
};
void menu();//菜单
void putit(addressbook* add);
void showperson(addressbook* add);
int if_include(addressbook* add, string name);
void deleteperson(addressbook* add, int i);
void findPerson(struct addressbooks* add);//查找联系人
void cleanperson(struct addressbook* add);//清空所有联系人
int main()
{menu();addressbook add;int choice=1;while (choice != 0){cin >> choice;switch (choice){case 1: putit(&add);break;case 2: showperson(&add);break;case 3: {cout << "请输入你要删除的人的名字" << endl;string aname;cin >> aname;if (if_include(&add, aname) == -1){cout << "查无此人" << endl;break;}if (if_include(&add, aname)){deleteperson(&add, if_include(&add, aname));}}break; case 4:break;case 5:break;case 6:break;case 0: cout << "欢迎下次使用" << endl;return 0;break;default: {cout << "输入不合法,请重新输入" << endl;break; }}}
}
void menu()
{cout << endl;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 putit(addressbook* add)//添加联系人功能
{if (add->size == MAX)cout << "通讯录已满" << endl;else{string name;string sex;int age;string phone;string home;cout << "请输入姓名" << endl;cin >> name;add->personArray[add->size].name = name;cout << "请输入姓别" << endl;cin >> sex;add->personArray[add->size].sex = sex;cout << "请输入年龄" << endl;cin >> age;add->personArray[add->size].age = age;cout << "请输入电话号码" << endl;cin >> phone;add->personArray[add->size].phone = phone;cout << "请输入家庭住址" << endl;cin >> home;add->personArray[add->size].home = home;add->size++;cout << "添加联系人成功" << endl;}system("pause");system("cls");menu();
}
void showperson(addressbook* add)
{for (int i = 0; i <add->size; i++){cout << "姓名: " << add->personArray[i].name;cout << "\t姓别: " << add->personArray[i].sex;cout << "\t年龄: " << add->personArray[i].age;cout << "\t电话号码: " << add->personArray[i].phone;cout << "\t家庭住址: " << add->personArray[i].home << endl;}system("pause");system("cls");menu();
}
int if_include(addressbook* add, string name)
{for (int i = 0; i < add->size; i++){if (name == add->personArray[i].name){return i;}else{return -1;}}}
void deleteperson(addressbook* add, int i)
{for (; i < add->size; i++){add->personArray[i] = add->personArray[i + 1];}system("pause");system("cls");
}void findPerson( addressbook* add)
{cout << "请输入您想要查找的联系人:" << endl;string name;cin >> name;int ret = if_include(add, name);if (ret == -1){cout << "查无此人" << endl;}else{ //查到此人,进行显示操作int i = ret;cout << "姓名:" << add->personArray[i].name << "\t";cout << "性别:" << add->personArray[i].sex << "\t";cout << "年龄:" << add->personArray[i].age << "\t";cout << "联系方式:" << add->personArray[i].phone << "\t";cout << "地址:" << add->personArray[i].home << endl;}//按任意键清屏system("pause");system("cls");
}
void cleanperson(struct addressbook* add)//清空所有联系人
{cout << "是否确认清空?" << endl;cout << "1 --- 是" << endl;cout << "2 --- 否" << endl;int a;cin >> a;if (a == 1){add->size = 0;//将当前记录联系人数量置为0,做逻辑上的清空操作cout << "通讯录已清空" << endl;}system("pause");system("cls");
}
建议大家可以试着分文件编写代码,也方便查看
六、运行结果
如果你年满18周岁以上,又觉得学【C语言】太难?想尝试其他编程语言,那么我推荐你学Python,现有价值499元Python零基础课程限时免费领取,限10个名额!
▲扫描二维码-免费领取
戳“阅读原文”我们一起进步