三个老师每个老师带五个学生
#include <iostream>
using namespace std;
#include <string>
#include <ctime>
//创建学生结构体变量
struct student
{
string sName;
int score;
};
//创建老师结构体变量
struct Teacher
{
string tName;
struct student sArray[5];
};
void hanshu( struct Teacher tArray[],int len)
{
//给老师开始赋值
for (int i = 0; i < len; i++)
{
//添加一个名字种子
string nameseed = "ABCDE";
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[j];
int random=rand() % 61 + 40;//生成一个分数的随机数
tArray[i].sArray[j].score = random;
}
}
}
void printoff(struct Teacher tArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << " 老师的名字是" << tArray[i].tName << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t学生的名字是" << tArray[i].sArray[j].sName
<< "\t成绩是" << tArray[i].sArray[j].score << endl;
}
}
}
int main()
{
//添加随机数种子随时间变化
srand((unsigned int)time(NULL));
//创建三名老师的数组
struct Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
//创建一个函数给老师赋值,以及给老师下边的学生赋值
hanshu(tArray, len);
//打印所有老师以及带的学生
printoff(tArray, len);
system("pause");
return 0;
}