一、结构体案例
案例一
描述:学校正在做毕设项目,每名老师带领5名学生,总共有3名老师,需求如下:
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和存放5名学生的数组作为成员,学生的成员有姓名、考试分数,创建数组存放三名老师,通过函数给每个老师及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据
#include <iostream>
using namespace std;
#include<string>
//设计学生和老师的结构体
//其中在老师的结构体中,有老师姓名和存放5名学生的数组作为成员
//学生的成员有姓名、考试分数
//学生结构体
struct student
{
string sname;
int score;
};
//老师结构体
struct teacher
{
string tname;
struct student s[5];
};
//赋值函数
void give(struct teacher t[],int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师姓名:";
cin >> t[i].tname;
for (int j = 0; j < 5; j++)
{
cout << "学生姓名:";
cin >> t[i].s[j].sname;
cout << "学生分数:";
cin >> t[i].s[j].score;
}
system("cls"); //清屏操作
}
}
void prinft(struct teacher t[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "\n老师姓名:" << t[i].tname << endl;
for (int j = 0; j < 5; j++)
{
cout << "学生姓名:" << t[i].s[j].sname;
cout << " 学生分数:" << t[i].s[j].score << endl;
}
}
}
int main()
{
//创建数组存放三名老师,通过函数给每个老师及所带的学生赋值
struct teacher ter[3];
int len = sizeof(ter) / sizeof(ter[0]);
give(ter, len);
//最终打印出老师数据以及老师所带的学生数据
prinft(ter, len);
system("pause");
return 0;
}
案例二
描述:设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄
通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果
5名英雄信息如下:
1、只用一个主函数
{
“刘备”,23,“男”},
{
“关羽”,21,“男”},
{
“张飞”,20,“男”},
{
“赵云”,22,“男”},
{
“貂蝉”,19,“女”},
#include<iostream>
using namespace std;
#include<string>
//创建英雄结构体,包含姓名,年龄,性别
struct hero
{
string name;
int age;
string sex;
};
int main()
{
struct hero Her[]=
{
{
"刘备",23,"男"},
{
"关羽",21,"男"},
{
"张飞",20,"男"},
{
"赵云",22,"男"},
{
"貂蝉",19,"女"}
};
int len = sizeof(Her) / sizeof(Her[0]);
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (Her[j].age > Her[j + 1].age)
{
struct hero temp = Her[j];
Her[j] = Her[j + 1];
Her[j + 1] = temp;
}
}
}
cout << "将英雄按年龄升序排排序后:" << endl;
for (int i = 0; i < len; i++)
{
cout << Her[i].name;
cout << " " << Her[i].age;
cout << " " << Her[i].sex << endl;
}
system("pause");
return 0;
}
2、用分函数
#include<iostream>
using namespace std;
#include<string>
//创建英雄结构体,包含姓名,年龄,性别
struct hero
{
string name;
int age;
string sex;
};
//冒泡排序
void bubber(struct hero Her[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (Her[j].age > Her[j + 1].age)
{
struct hero temp = Her[j];
Her[j] = Her[j + 1];
Her[j + 1] = temp;
}
}
}
}
//打印英雄排序后的信息
void print(struct hero Her[], int len)
{
cout << "将英雄按年龄升序排排序后:" << endl;
for (int i = 0; i < len; i++)
{
cout << Her[i].name;
cout << " " << Her[i].age;
cout << " " << Her[i].sex << endl;
}
}
int main()
{
struct hero Her[]=
{
{
"刘备",23,"男"},
{
"关羽",21,"男"},
{
"张飞",20,"男"},
{
"赵云",22,"男"},
{
"貂蝉",19,"女"}
};
int len = sizeof(Her) / sizeof(Her[0]);
bubber(Her, len);
print(Her, len);
system("pause");
return 0;
}
二、通讯录管理系统
系统中需要实现的功能如下:
-
添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
-
显示联系人:显示通讯录中所有联系人信息
-
删除联系人:按照姓名进行删除指定联系人
-
查找联系人:按照姓名查看指定联系人信息
-
修改联系人:按照姓名重新修改指定联系人
-
清空联系人:清空通讯录中所有信息
-
退出通讯录:退出当前使用的通讯录
此通讯系统为初学所做,若有更加优化的方案亦或是某个模块有 更加简易的写法,欢迎提出宝贵建议
#include <iostream>
using namespace std;
#include<string>
#define MAX 1000 //通讯录最大容量为1000人
//人员信息结构体
struct person
{
string p_Name; //姓名
int p_Sex; //性别 1 男 2 女
int p_Age; //年龄
string p_Phone; //联系电话
string p_Addr; //家庭住址
};
//通讯录结构体
struct TelBook
{
struct person per[MAX]; //联系人信息
int TBnum; //通讯录中已存人数
};
void ShowMenu(); // 显示菜单
void AddPerson(struct TelBook *tbook); //添加联系人
void ShowPerson(struct TelBook *tbook); //显示联系人
void DelPerson(struct TelBook *tbook); //删除联系人
void FindPerson(struct TelBook *tbook); //查找联系人
void ChangePerson(struct TelBook *tbook); //修改联系人信息
void ClsPerson(struct TelBook *tbook); //清空联系人列表
int main()
{
struct TelBook tbook;
tbook.TBnum = 0; //初始化
int select; //选择操作
while (1)
{
system("cls");
ShowMenu();
cin >> select;
switch (select)
{
case 1: //添加联系人
AddPerson(&tbook);
break;
case 2: //显示联系人
ShowPerson(&tbook);
break;
case 3: //删除联系人
DelPerson(&tbook);
break;
case 4: //查找联系人
FindPerson(&tbook);
break;
case 5: //修改联系人
ChangePerson(&tbook);
break;
case 6: //清空联系人
ClsPerson(&tbook);
break;
case 0: //退出通讯录
cout << "操作结束,退出通讯录,感谢使用!" << endl;
system("pause");
return 0;
break;
default:
cout << "操作错误,请重新进行选择!" << endl;
break;
}
}
system("pause");
return 0;
}
//菜单显示
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;
}
//显示联系人
void ShowPerson(struct TelBook *tbook)
{
if (tbook->TBnum == 0)
{
cout << "暂无联系人" << endl;
}
for (int i = 0; i < tbook->TBnum; i++)
{
cout << i + 1;
cout << ".姓名:" << tbook->per[i].p_Name;
if (tbook->per[i].p_Sex == 1)
cout << " 性别:男";
else
cout << " 性别:女";
cout << " 年龄:" << tbook->per[i].p_Age;
cout << " 电话号码:" << tbook->per[i].p_Phone;
cout << " 家庭住址:" << tbook->per[i].p_Addr << endl;
}
system("pause");
return;
}
//添加联系人
void AddPerson(struct TelBook *tbook)
{
int flag = 1;
while (1)
{
if (tbook->TBnum == MAX)
{
cout << "通讯录已满,无法再添加联系人" << endl;
return;
}
else
{
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:\t" << endl;
cin >> name;
tbook->per[tbook->TBnum].p_Name = name;
//性别
int sex;
cout << "请输入性别:" << endl;
cout << "1 男" << endl;
cout << "2 女" << endl;
while (1)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
tbook->per[tbook->TBnum].p_Sex = sex;
break;
}
else
cout << "操作错误,请重新输入!" << endl;
}
//年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
tbook->per[tbook->TBnum].p_Age = age;
//联系电话
string phone;
cout << "请输入电话号码:" << endl;
cin >> phone;
tbook->per[tbook->TBnum].p_Phone = phone;
//家庭住址
string addr;
cout << "请输入家庭住址:" << endl;
cin >> addr;
tbook->per[tbook->TBnum].p_Addr = addr;
}
cout << "添加成功" << endl;
tbook->TBnum++;
cout << "如果继续添加联系人,请按任意键继续!" << endl;
cout << "如果不再添加联系人,请按0!" << endl;
cin >> flag;
if (flag == 0)
break;
}
return;
}
//删除联系人
void DelPerson(struct TelBook *tbook)
{
string name;
int num, flag = 1;
while (1)
{
cout << "请输入您要删除的联系人的姓名:";
cin >> name;
for (num = 0; num < tbook->TBnum; num++)
{
if (tbook->per[num].p_Name == name)
{
tbook->per[num] = tbook->per[num + 1];
break;
}
}
if (num == tbook->TBnum)
{
cout << "未查询到此联系人" << endl;
break;
}
cout << "删除成功" << endl;
tbook->TBnum--;
cout << "如果继续删除联系人,请按任意键继续!" << endl;
cout << "如果不再删除联系人,请按0!" << endl;
cin >> flag;
if (flag == 0)
break;
}
system("pause");
}
//查找联系人
void FindPerson(struct TelBook *tbook)
{
string name;
cout << "请输入您要查询联系人的姓名:";
cin >> name;
for (int i = 0; i < tbook->TBnum; i++)
{
if (tbook->per->p_Name == name)
{
cout << "姓名:" << tbook->per[i].p_Name;
if (tbook->per[i].p_Sex == 1)
cout << " 性别:男";
else
cout << " 性别:女";
cout << " 年龄:" << tbook->per[i].p_Age;
cout << " 电话号码:" << tbook->per[i].p_Phone;
cout << " 家庭住址:" << tbook->per[i].p_Addr << endl;
}
}
system("pause");
}
//修改联系人信息
void ChangePerson(struct TelBook *tbook)
{
string name;
int i;
cout << "请输入您要修改联系人的姓名:";
cin >> name;
for (i = 0; i < tbook->TBnum; i++)
{
if (tbook->per->p_Name == name)
{
//性别
int sex;
cout << "请输入性别:" << endl;
cout << "1 男" << endl;
cout << "2 女" << endl;
while (1)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
tbook->per[i].p_Sex = sex;
break;
}
else
cout << "操作错误,请重新输入!" << endl;
}
//年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
tbook->per[i].p_Age = age;
//联系电话
string phone;
cout << "请输入电话号码:" << endl;
cin >> phone;
tbook->per[i].p_Phone = phone;
//家庭住址
string addr;
cout << "请输入家庭住址:" << endl;
cin >> addr;
tbook->per[i].p_Addr = addr;
cout << "修改成功" << endl;
break;
}
}
if (i == tbook->TBnum)
{
cout << "未查询到此联系人" << endl;
}
system("pause");
}
//清空联系人列表
void ClsPerson(struct TelBook *tbook)
{
tbook->TBnum = 0;
cout << "已清空联系人列表" << endl;
system("pause");
}
此通讯系统为初学所做,若有更加优化的方案亦或是某个模块有 更加简易的写法,欢迎提出宝贵建议
【注释】 学习课程为-黑马程序C++教程