?创建一个结构体,输出一个具有老师姓名,以及每个老师所带有学生的姓名和分数的结构体嵌套表示
struct student
{
string name;
int score;
};
struct teacher
{
string name;
struct student arr1[5];
};
//赋值函数
void list(struct teacher arr[],int len,int len1)
{
string name_ = "ABCDE";
srand(unsigned int(time(NULL)));
for (int i = 0;i < len;i++)
{
arr[i].name = "teacher_";
arr[i].name += name_[i];
for (int j = 0;j < len1;j++)
{
int score = rand() % 61 + 40;//随机数成绩在40-100之间
arr[i].arr1[j].name = "student_";
arr[i].arr1[j].name += name_[j];
arr[i].arr1[j].score = score;
}
}
}
//成绩打印函数
void print(struct teacher arr[], int len, int len1)
{
for (int i = 0;i < len;i++)
{
cout << "老师姓名" << arr[i].name << endl;
for (int j = 0;j < len1;j++)
{
cout << "\t学生姓名_" << arr[i].arr1[j].name ;
cout << "\t学生成绩_" << arr[i].arr1[j].score << endl;
}
}
}
int main()
{
struct teacher arr[3];
struct student arr1[5];
int len = sizeof(arr) / sizeof(arr[0]);
int len1 = sizeof(arr1) / sizeof(arr1[0]);
list(arr,len,len1);//老师学生成绩赋值
print(arr, len, len1);//成绩打印
return 0;
}
下期功能优化
|