问题定义
问题:3名老师,每个老师5名学生
老师结构体特征:姓名,学生数组
学生结构体特征:姓名,考试分数
代码
#include<iostream>
#include<string>
using namespace std;
//问题:3名老师,每个老师5名学生
//老师结构体特征:姓名,学生数组
//学生结构体特征:姓名,考试分数
//学生结构体
struct Student {
string name;//姓名
int score;//考试分数
};
//老师结构体
struct Teacher {
string name;//姓名
Student xuesheng[5];//学生数组
};
//地址传递(仅存在于指针函数操作中)
void printTeacher(const Teacher* p)
{
//引入const变成常量指针可以防止函数中对结构体中数据进行修改,防止误操作
cout << "老师姓名:" << p->name << endl;
for (int i = 0; i < 5; i++) {
cout << " 学生姓名:" << p->xuesheng[i].name << " 分数:" << p->xuesheng[i].score << endl;
}
}
int main()
{
Teacher laoshi[3];
laoshi[0].name = "张老师";
laoshi[1].name = "李老师";
laoshi[2].name = "何老师";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
cout <<"请输入"<< laoshi[i].name << "的第"<<(j+1)<<"个学生信息:" << endl;
cout << "学生姓名:";
cin >> laoshi[i].xuesheng[j].name;
cout << "学生分数:";
cin >> laoshi[i].xuesheng[j].score;
}
}
for (int i = 0; i < 3; i++)
{
printTeacher(laoshi);
}
system("pause");
return 0;
}