问题定义
问题:3名老师,每个老师5名学生 老师结构体特征:姓名,学生数组 学生结构体特征:姓名,考试分数
代码
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int score;
};
struct Teacher {
string name;
Student xuesheng[5];
};
void printTeacher(const Teacher* p)
{
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;
}
|