C++学习案例记录&分享十(结构体案例)

老师结构体下的学生子结构体

#include <iostream>
#include <random>
using namespace std;

struct Student 
{
	string name;
	int score = 0;
};

struct Teacher 
{
	string name;
	struct Student sArray[5];
};

void allocatespace(struct Teacher tArray[], int len)
{
	string nameseed = "ABCDE";
	for (int i = 0;i < len;i++)
	{
		tArray[i].name = "teacher_";
		tArray[i].name += nameseed[i];
		
		for (int j = 0;j < 5;j++) 
		{
			random_device rd;
			mt19937 gen(rd());
			uniform_int_distribution<>dis(40, 100);

			tArray[i].sArray[j].name = "student_";
			tArray[i].sArray[j].name += nameseed[j];
			tArray[i].sArray[j].score = dis(gen);
		}
	}
 

}

void print(struct 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() 
{
	struct Teacher tArray[3];

	int len = sizeof(tArray) / sizeof(tArray[0]);

	allocatespace(tArray, len);

	print(tArray, len);


	return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇