#include <iostream>#include <string>#include <ctime>using namespace std;// 定义学生结构体struct Student { string name; int age; int score;};// 定义老师结构体struct Teacher { string name; struct Student stds[5];};void allocateSpace(struct Teacher teacherArray[], int len);void printAllInfo(struct Teacher teacherArray[], int len);int main() { // 随机数种子 srand((unsigned int) time(NULL)); // 1.创建3名老师的数组 struct Teacher teacherArray[3]; int len = sizeof(teacherArray) / sizeof(teacherArray[0]); // 2.通过函数给3名老师的信息赋值,并且给老师带的学生信息也赋值 allocateSpace(teacherArray, len); // 3.打印所有老师及所带的学生信息 printAllInfo(teacherArray, len); return 0;}void allocateSpace(struct Teacher teacherArray[], int len) { for (int i = 0; i < len; i++) { teacherArray[i].name = "teacher_"; teacherArray[i].name += i; for (int j = 0; j < 5; j++) { teacherArray[i].stds[j].name = "student_" + i; teacherArray[i].stds[j].age = 10 + i; int s = rand() % 61 + 40; teacherArray[i].stds[j].score = s; } }}void printAllInfo(struct Teacher teacherArray[], int len) { for (int i = 0; i < len; i++) { cout << "teacher :" << teacherArray[i].name << endl; for (int j = 0; j < 5; j++) { cout << "\tstudent name :" << "teacher_" << teacherArray[i].name << teacherArray[i].stds[j].name << endl; cout << "\tstudent age :" << "teacher_" << teacherArray[i].name << teacherArray[i].stds[j].age << endl; cout << "\tstudent score :" << "teacher_" << teacherArray[i].name << teacherArray[i].stds[j].score << endl; } }}