案例1
案例描述
学校正在做毕业设计项目,每名老师带领5个学生,总共有三名老师,需求如下:
设计学生与老师的结构体,其中在老师的结构体中,有老师姓名和一个存放五名学生的数组作为成员,学生的成员有姓名,考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据
实例:
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
//定义学生结构体
struct student {
string name;
int score = 0;
};
//定义老师结构体
struct teacher {
string tname;
//创建学生结构体变量
//定义数组来存储学生结构体
struct student stuarray[5];
};
//创建写入数据的函数
void evaluate(struct teacher tarray[],int len) {
string a = "ABCDE";
//外层循环,给老师赋值
for (int i = 0; i < len; i++) {
tarray[i].tname = "teacher_";
tarray[i].tname += a[i];
//内层循环给学生赋值
for (int j = 0; j < 5; j++) {
int num = rand