案例:有三名老师,分别辅导五名学生,每名学生会在一百道题中随机分到一道题并得到题目序号
code
//案例:有三名老师,分别辅导五名学生,每名学生会在一百道题中随机分到一道题并得到题目序号
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct student//定义学生结构体
{
string name;
int num;
};
struct teacher//定义老师结构体
{
string name;
struct student s[5];
};
//给老师和同学的信息赋值
void allocatSpace(struct teacher t[])
{
string nameSeed = "ABCDE";
for (int i = 0; i < 3; i++)
{
t[i].name = "Teacher_";
t[i].name += nameSeed[i];
for (int j = 0; j < 5; j++)
{
t[i].s[j].name = "Student_";
t[i].s[j].name += nameSeed[j];
int random = rand() % 101;
t[i].s[j].num = random;
}
}
}
//定义打印信息的函数
void print_(struct teacher t[])
{
for (int i = 0; i < 3; i++)
{
cout << t[i].name << endl ;
for (int j = 0; j < 5; j++)
{
cout << "\t" << t[i].s[j].name << " " << t[i].s[j].num;
}
cout << endl;
}
}
int main()
{
srand((unsigned int)time(NULL));
struct teacher t[3];
allocatSpace(t);
print_(t);
return 0;
}