C++学习篇 1.0
本博客是根据黑马程序员的视频中基础语法学习完后自己根据视频实现的通讯录管理系统。此系统是用C++基础语法与结构体组合实现的。
- 此系统的难点在于其两个结构体Person和Addressbook的设计上,我在自己一个人实现这个系统时,没有想到要如何设计这两个数据结构。因此,本人觉得结构体的设计是此项目的难点。在其后续课程中,类似的思想是在类中套类,对自己的启发很大。
代码的实现不难,这个系统的精髓是结构体的设计。
#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
//联系人
struct Person {
string name;
int sex;
int age;
string phone;
string adr;
};
//通讯录
struct Addressbook {
Person personArray[MAX];
int max_size;
};
//显示联系人
void showMenu(Addressbook abs) {
if(abs.max_size){
for (int i = 0; i < abs.max_size; i++) {
cout << "姓名:" << abs.personArray[i].name<< " 性别:" <<(abs.personArray[i].sex==1?"男":"女")
<<" 年龄:" << abs.personArray[i].age << " 电话:" << abs.personArray[i].phone << " 地址:"
<< abs.personArray[i].adr << endl;
}
}
else {
cout << "抱歉,当前记录为空,请重新输入" << endl;
}
}
//添加联系人
void AddPersion(Addressbook *abs,int i) {
cout << "请输入姓名:" << endl;
cin >> abs->personArray[i ].name;
cout << "请输入性别:1:男;2:女" << endl;
cin >> abs->personArray[i ].sex;
cout << "请输入年龄:" << endl;
cin >> abs->personArray[i ].age;
cout << "请输入电话:" << endl;
cin >> abs->personArray[i ].phone;
cout << "请输入地址:" << endl;
cin >> abs->personArray[i ].adr;
cout << "输入成功!谢谢" << endl;
abs->max_size++;
}
//获得联系人的下标
int XiaBiao(const Addressbook* abs, string name) {
for (int i = 0; i < abs->max_size; i++) {
if (name == abs->personArray[i].name)
{
return i;
}
}
cout << "查无此人" << endl;
}
//删除联系人
void del(Addressbook* abs){
cout << "请输入删除的联系人" << endl;
string name;
cin >> name;
int i = XiaBiao(abs,name);
for (int i = 0; i < abs->max_size; i++) {
abs->personArray[i] = abs->personArray[i + 1];
abs->max_size--;
}
cout<<"删除成功"<<endl;
}
//查找联系人
void find(const Addressbook *abs) {
cout << "请输入查找的人名" << endl;
string name;
cin >> name;
int i = XiaBiao(abs, name);
cout << "姓名:" << abs->personArray[i].name << " 性别:" << (abs->personArray[i].sex == 1 ? "男" : "女")
<< " 年龄:" << abs->personArray[i].age << " 电话:" << abs->personArray[i].phone << " 地址:"
<< abs->personArray[i].adr << endl;
}
//修改联系人
void modif(Addressbook* abs) {
cout << "输入你要修改的联系人名字:" << endl;
string name;
cin >> name;
int i = XiaBiao(abs, name);
int number;
cout << "请输入要修改的信息:" << "1:姓名 2:性别 3:年龄 4:电话 5:住址" << endl;
cin >> number;
switch (number) {
case 1:
{
cout<< "请输入姓名:" << endl;
cin >> abs->personArray[i].name;
}; break;
case 2:
{
cout << "请输入性别:1:男;2:女" << endl;
cin >> abs->personArray[i].sex;
}; break;
case 3:
{
cout << "请输入年龄:" << endl;
cin >> abs->personArray[i].age;
}; break;
case 4:
{
cout << "请输入电话:" << endl;
cin >> abs->personArray[i].phone;
}; break;
case 5:
{
cout << "请输入地址:" << endl;
cin >> abs->personArray[i].adr;
}; break;
default:break;
cout << "输入成功!谢谢" << endl;
}
}
//清空联系人
void clearPersion(Addressbook* abs) {
abs->max_size = 0;
cout << "清空成功" << endl;
}
int main()
{
Addressbook abs;
abs.max_size = 0;
int i = 1;
while (i) {
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;
cout << "请输入数字:" << endl;
int num;
cin >> num;
switch (num)
{
case 1:AddPersion(&abs, abs.max_size);//添加联系人
break;
case 2:showMenu(abs);//展示联系人
break;
case 3:del(&abs);//3、删除联系人
break;
case 4:find(&abs);//4、查找联系人
break;
case 5:modif(&abs);//5、修改联系人
break;
case 6:clearPersion(&abs); //清空联系人
break;
case 0:
{i = 0;
cout << "欢迎下次使用" << endl;
}
break;
default:cout << "输入错误,请重新输入"<<endl;
break;
}
system("pause");
system("cls");
}
return 0;
}