老师结构体下的学生子结构体
#include <iostream>
#include <random>
using namespace std;
struct Student
{
string name;
int score = 0;
};
struct Teacher
{
string name;
struct Student sArray[5];
};
void allocatespace(struct Teacher tArray[], int len)
{
string nameseed = "ABCDE";
for (int i = 0;i < len;i++)
{
tArray[i].name = "teacher_";
tArray[i].name += nameseed[i];
for (int j = 0;j < 5;j++)
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<>dis(40, 100);
tArray[i].sArray[j].name = "student_";
tArray[i].sArray[j].name += nameseed[j];
tArray[i].sArray[j].score = dis(gen);
}
}
}
void print(struct Teacher tArray[], int len)
{
for (int i = 0;i < len;i++)
{
cout << "老师的姓名:" << tArray[i].name << endl;
for (int j = 0;j < 5;j++)
{
cout << "\t学生的姓名为:" << tArray[i].sArray[j].name << "分数为:" <<
tArray[i].sArray[j].score << endl;
}
}
}
int main()
{
struct Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocatespace(tArray, len);
print(tArray, len);
return 0;
}