学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员。学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值。最终打印出老师数据以及老师所带的学生数据。
//以下为教师同学结构体应用
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int socre;
};
struct teacher
{
string name;
struct student array[5];
};
void allocateSpace(teacher s[], int t, int x)
{
string name = "教师";
string t_name = "ABCDE";
for(int i = 0; i < t;i++)
{
s[i].name = name + t_name[i];
cout << s[i].name << ' ' << endl;
for (int j = 0; j < x; j++)
{
s[i].array[j].name = t_name[j];
s[i].array[j].socre = rand() % 100;
cout << "\t姓名:" << s[i].array[j].name << " 分数:" << s[i].array[j].socre << endl;
}
}
}
int main()
{
int t = 5;
int x = 5;
struct teacher arr[5];
allocateSpace(arr , t, x);
system("pause");
return 0;
}