C++学习记录---结构体2

C++学习记录---结构体2,第1张

一、结构体案例
#include
#include
#include
using namespace std;

//定义学生结构体
struct student
{
	string name;
	int score
};
//定义老师结构体
struct teacher
{
	string name;
	struct student stu[5];
};
//
void printinfo(struct teacher tea,int len)
{
	for (int i = 0; i < len; i++)
	{
		cout <<"老师的姓名:"<< tea[i].name << endl;
		for (int j = 0; j < 5; j++)
		{
			cout <<"\t学生的姓名:"<< tea[i].stu[j].name << "  分数:" << tea[i].stu[j].score << endl;
		}
	}
}
//给老师和学生赋值
void allocateSpace(struct teacher tea[],int len)
{
	//老师赋值
	string nameSeed = "ABCDE";
	for (int i = 0; i < len; i++)
	{
		tea[i].name = "teacher_";
		tea[i].name += nameSeed[i]
		//学生赋值
		for (int j = 0; j < 5; j++)
		{
			tea[i].stu[j].name = "student_";
			tea[i].stu[j].name += nameSeed[j];
			int random = rand % 61 + 40;//60-100d的随机数
			tea[i].stu[j].score = radom
		}
	}
}
int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));
	//创建3位老师的数组
	struct teacher tea[3];
	
	//给三位老师的信息赋值,并给老师带的学生赋值
	int len = sizeof(tea)/sizeof(tae[0]);
	allocateSpace(tea,len);
	//打印所有老师及带的学生信息
	printinfo(struct teacher tea,int len)
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/3002841.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存