c++ 结构体 3名教师 5名学生信息
作者:互联网
#include
#include <string.h>
#include<time.h>
using namespace std;
struct student {
string sname;
int score;
};
struct teacher {
string tname;
struct student sarry[5];
};
void allocatespace(struct teacher tarray[], int len)
{
//名字种子
string nameseed = “ABCDE”;
// 给老师开始赋值
for (int i = 0; i < len; i++)
{
tarray[i].tname = "teacher_";
tarray[i].tname += nameseed[i];
//给学生开始赋值
for (int j = 0; j < 5; j++)
{
int score = rand() % 61 + 41;
tarray[i].sarry[j].sname = "student_";
tarray[i].sarry[j].sname += nameseed[i];
tarray[i].sarry[j].score = score;
}
}
}
void printInfo(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].sarry[j].sname <<" 分数为:"<< tarray[i].sarry[j].score<< endl;
}
}
}
int main ()
{
// 随机数种子
srand((unsigned int)time(NULL));
//创建3名老师的数组
struct teacher tarray[3];
// 通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
int len = sizeof(tarray) / sizeof(tarray[0]);
allocatespace(tarray, len);
struct student sarray[5];
// 打印所有信息
printInfo(tarray, len);
return 0;
}
标签:教师,struct,int,c++,学生,teacher,score,len,tarray 来源: https://blog.csdn.net/self_up/article/details/115433983