这道题比较有意思,考察的比较完全,大部分知识点全都考到了,我给大家演示一下:
#include <iostream>
using namespace std;
#include<string>
struct student
{//学生结构体定义
string sname;//学生姓名
int score;//学生成绩
};
struct Teacher
{//老师结构体定义
string tname;//老师名字
struct student sArray[5];//学生数组
};
void fuzhi(struct Teacher tArray[], int len)
{
string nam = "ABCDE";
//给老师赋值
for (int i = 0;i < 3;i++)
{
tArray[i].tname = "Teacher_";
tArray[i].tname += nam[i];
for (int j = 0;j < 5;j++)
{//通过循环给学生赋值
tArray[i].sArray[j].sname = "Student";
tArray[i].sArray[j].sname += nam[j];
int random = rand ()% 61 + 40;
tArray[i].sArray[j].score = random;
}
}
}
void printinf(struct Teacher tArray[],int len)//打印所有信息
{
for (int i = 0;i < 3;i++)
{
cout << tArray[i].tname << endl;
for(int j = 0;j < 5;j++)
{
cout << "\t学生姓名" << tArray[i].sArray[j].sname << "学生成绩" << tArray[i].sArray[j].score << endl;
}//美观使用\t
}
}
int main()
{
struct Teacher tArray[3];//创建三名老师的数组
int len = (sizeof tArray) / (sizeof tArray[0]);
fuzhi(tArray, len);
printinf(tArray, len);
return 0;
}
运行结果:
仔细看一下,加油,与君共勉!