#include#include using namespace std; //学生结构体定义 struct Student { //姓名 string sName; //分数 int score; }; //老师结构体定义 struct Teacher { //姓名 string tName; //学生数组 struct Student sArray[5]; }; //给老师和学生赋值的函数 void allocateSpace(struct Teacher tArray[],int length) { string nameSeed = "ABCDE"; //给老师开始赋值 for (int i = 0; i < length; i++ ) { tArray[i].tName = "Teacher_"; tArray[i].tName += nameSeed[i]; //通过循环给每名老师所带的学生赋值 for (int j = 0; j < 5; j++) { tArray[i].sArray[j].sName = "Student_"; tArray[i].sArray[j].sName += nameSeed[i]; tArray[i].sArray[j].score = 90; } } } //打印所有信息 void printInfo(struct Teacher tArray[],int length) { for (int i = 0; i < length; i ++) { cout << "老师姓名:" << tArray[i].tName << endl; for (int j = 0; j < 5; j++) { cout << "学生姓名:" << tArray[i].sArray[j].sName << "考试分数:" << tArray[i].sArray[j].score << endl; } } } int main8() { //1.创建3名老师的数组 struct Teacher tArray[3]; //2.通过函数给3名老师的信息赋值,并给老师带的学生赋值 int length = sizeof(tArray) / sizeof(tArray[0]); allocateSpace(tArray,length); //.打印所有老师及所有的学生信息 printInfo(tArray,length); system("pause"); return 0; }



