Learn C++ from scratch, create and print a data structure containing teacher and student information

This code is a simple C++ program that is mainly used to create and print a data structure containing teacher and student information.

Two structures are defined in the code: Studentand Teacher. StudentThe struct contains the names and grades of the students, Teacherthe struct contains the names of the teachers, and an array of 5 students.

function allocateSpaceto allocate space for the teachers array and fill it with data. It accepts an array of teachers and the length of the array as parameters. Inside the function, set the name for each teacher by looping through the teachers array, and then set the name and randomly generated grade for the students in each teacher's students array.

The function printTeachersis used to print the data in the teachers array. It accepts an array of teachers and the length of the array as parameters. Inside the function, print the name of each teacher by looping through the teachers array, and then print the name and grade of each student in each teacher's students array.

In mainthe function, first set the random number seed, and then define an array of teachers containing 3 teachers tArray. Create and print the data by calculating the length of the teachers array, passing the length as an argument to the allocateSpaceand function. printTeachersFinally, use system("pause")a function to make the program pause after outputting the result so you can see the result.

To sum up, the program stores and manages the information of teachers and students through structures and arrays, and fills the data and prints the results by calling the corresponding functions.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// 定义学生结构体
struct Student
{
	string name; // 学生姓名
	int score; // 学生成绩
};

// 定义教师结构体
struct Teacher
{
	string name; // 教师姓名
	Student sArray[5]; // 学生数组,包含5个学生
};

// 为教师数组分配空间并填充数据
void allocateSpace(Teacher tArray[], int len)
{
	string tName = "教师"; // 教师姓名前缀
	string sName = "学生"; // 学生姓名前缀
	string nameSeed = "ABCDE"; // 姓名生成的种子,用于拼接学生姓名
	for (int i = 0; i < len; i++)
	{
		tArray[i].name = tName + nameSeed[i]; // 设置教师姓名
		
		// 填充学生数组的数据
		for (int j = 0; j < 5; j++)
		{
			tArray[i].sArray[j].name = sName + nameSeed[j]; // 设置学生姓名
			tArray[i].sArray[j].score = rand() % 61 + 40; // 随机生成学生成绩(40-100之间)
		}
	}
}

// 打印教师数组中的数据
void printTeachers(Teacher tArray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << tArray[i].name << endl; // 打印教师姓名
		for (int j = 0; j < 5; j++)
		{
			cout << "\t姓名:" << tArray[i].sArray[j].name << " 分数:" << tArray[i].sArray[j].score << endl; // 打印学生姓名和成绩
		}
	}
}

int main() {
	srand((unsigned int)time(NULL)); // 设置随机数种子,用于生成随机学生成绩

	Teacher tArray[3]; // 定义包含3个教师的教师数组

	int len = sizeof(tArray) / sizeof(Teacher); // 计算教师数组的长度

	allocateSpace(tArray, len); // 创建数据

	printTeachers(tArray, len); // 打印数据

	system("pause"); // 暂停程序,以便查看结果

	return 0;
}

Guess you like

Origin blog.csdn.net/dsafefvf/article/details/131401437