C++結構體案例1
1、案例描述
學校有3個老師,每名老師帶5個學生
需求如下:設計學生和老師結構體,其中在老師結構體中有老師姓名和一個學生數組,學生的成員有姓名、考試分數,通過創建數組存放三個老師,通過函數給每個老師以及學生賦值,並且打印出來結果。
2、自己寫的垃圾代碼
#include<iostream>
#include<string>
using namespace std;
//結構體案例
struct student
{
string name;
int score;
};
struct teacher
{
string name;
student stu[5];
};
void print1(const teacher *teach,int len1,int len2) {
for (int i = 0; i < len1; i++)
{
for (int j = 0; j <len2; j++)
{
cout << "老師名字:" << teach[i].name << "\t學生:" << teach[i].stu[j].name << "\t分數:" << teach[i].stu[j].score << endl;
}
}
}
int main() {
teacher teach[3];
student stu[5];
//第一個老師以及他的學生和分數
teach[0].name = "大王";
teach[0].stu[0].name = "小莊";
teach[0].stu[0].score = 90;
teach[0].stu[1].name = "小虎";
teach[0].stu[1].score = 70;
teach[0].stu[2].name = "小明";
teach[0].stu[2].score = 80;
teach[0].stu[3].name = "小愛";
teach[0].stu[3].score = 90;
teach[0].stu[4].name = "小西";
teach[0].stu[4].score = 100;
//第二個老師以及他的學生和分數
teach[1].name = "小路";
teach[1].stu[0].name = "小二";
teach[1].stu[0].score = 90;
teach[1].stu[1].name = "小三";
teach[1].stu[1].score = 70;
teach[1].stu[2].name = "小四";
teach[1].stu[2].score = 80;
teach[1].stu[3].name = "小五";
teach[1].stu[3].score = 90;
teach[1].stu[4].name = "小東";
teach[1].stu[4].score = 100;
//第三個老師及學生分數
teach[2].name = "小張";
teach[2].stu[0].name = "小莊";
teach[2].stu[0].score = 90;
teach[2].stu[1].name = "小虎";
teach[2].stu[1].score = 70;
teach[2].stu[2].name = "小明";
teach[2].stu[2].score = 80;
teach[2].stu[3].name = "小愛";
teach[2].stu[3].score = 90;
teach[2].stu[4].name = "小西";
teach[2].stu[4].score = 100;
int len1 = sizeof(teach) / sizeof(teach[0]);
int len2 = sizeof(stu) / sizeof(stu[0]);
print1(teach, len1,len2);
system("pause");
return 0;
}
老師名字:大王 學生:小莊 分數:90
老師名字:大王 學生:小虎 分數:70
老師名字:大王 學生:小明 分數:80
老師名字:大王 學生:小愛 分數:90
老師名字:大王 學生:小西 分數:100
老師名字:小路 學生:小二 分數:90
老師名字:小路 學生:小三 分數:70
老師名字:小路 學生:小四 分數:80
老師名字:小路 學生:小五 分數:90
老師名字:小路 學生:小東 分數:100
老師名字:小張 學生:小莊 分數:90
老師名字:小張 學生:小虎 分數:70
老師名字:小張 學生:小明 分數:80
老師名字:小張 學生:小愛 分數:90
老師名字:小張 學生:小西 分數:100
請按任意鍵繼續. . .
代碼寫的比較垃圾,重複語句較多
3、看完視頻後寫的代碼
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
struct Student
{
string Sname;
int score;
};
struct Teacher
{
string Tname;
struct Student Stu[5];
};
void InfoCin(Teacher Tarray[], int len) {
//輸入老師的姓名
string numstring = "ABCDE";
for (int i = 0; i <len; i++)
{
Tarray[i].Tname = "Teacher_";
Tarray[i].Tname += numstring[i];
//輸入學生的信息
for (int j = 0; j < 5; j++)
{
Tarray[i].Stu[j].Sname = "Stu_";
Tarray[i].Stu[j].Sname += numstring[j];
int random = rand() % 61 + 40;
Tarray[i].Stu[j].score = random;
}
}
}
void InfoCout(Teacher Tarray[],int len) {
for (int i = 0; i < len; i++)
{
//打印老師
cout << "老師名字: " << Tarray[i].Tname<<endl;
for (int j = 0; j < 5; j++)
{
cout << "\t學生姓名: " << Tarray[i].Stu[j].Sname << "\t考試分數: "
<< Tarray[i].Stu[j].score << endl;
}
}
}
int main() {
srand((unsigned int)time(NULL));
Teacher Tarray[3];
int len = sizeof(Tarray) / sizeof(Tarray[0]);
InfoCin(Tarray,len);
InfoCout(Tarray, len);
system("pause");
return 0;
}
老師名字: Teacher_A
學生姓名: Stu_A 考試分數: 89
學生姓名: Stu_B 考試分數: 50
學生姓名: Stu_C 考試分數: 65
學生姓名: Stu_D 考試分數: 70
學生姓名: Stu_E 考試分數: 44
老師名字: Teacher_B
學生姓名: Stu_A 考試分數: 70
學生姓名: Stu_B 考試分數: 74
學生姓名: Stu_C 考試分數: 77
學生姓名: Stu_D 考試分數: 85
學生姓名: Stu_E 考試分數: 75
老師名字: Teacher_C
學生姓名: Stu_A 考試分數: 98
學生姓名: Stu_B 考試分數: 97
學生姓名: Stu_C 考試分數: 41
學生姓名: Stu_D 考試分數: 57
學生姓名: Stu_E 考試分數: 79
請按任意鍵繼續. . .
重新寫後的代碼是真的漂亮!
4、案例二
姓名,年齡,性別
{"劉備",23,"男"},
{"關羽",22,"男"},
{"張飛",20,"男"},
{"趙雲",21,"男"},
{"月英",19,"女"}
對上面四組數據按年齡進行冒泡排序
#include<iostream>
#include<string>
using namespace std;
struct Hero
{
string name;
int age;
string sex;
};
void BubbleSort(Hero heroarr[],int len) {
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len-i-1; j++)
{
if (heroarr[j].age>heroarr[j+1].age)
{
struct Hero temp = heroarr[j];
heroarr[j] = heroarr[j + 1];
heroarr[j + 1] = temp;
}
}
}
}
void InfoCin(Hero heroarr[], int len) {
for (int i = 0; i < len; i++)
{
cout << "英雄姓名:" << heroarr[i].name << "\t英雄年齡:" <<
heroarr[i].age << "\t英雄性別:" << heroarr[i].sex << endl;
}
}
int main() {
Hero heroarr[5] = {
{"劉備",23,"男"},
{"關羽",22,"男"},
{"張飛",20,"男"},
{"趙雲",21,"男"},
{"月英",19,"女"}
};
int len = sizeof(heroarr) / sizeof(heroarr[0]);
cout << "排序前打印" << endl;
InfoCin(heroarr, len);
cout << "排序後打印" << endl;
//按照年齡冒泡排序
BubbleSort(heroarr, len);
InfoCin(heroarr, len);
system("pause");
return 0;
}
排序前打印
英雄姓名:劉備 英雄年齡:23 英雄性別:男
英雄姓名:關羽 英雄年齡:22 英雄性別:男
英雄姓名:張飛 英雄年齡:20 英雄性別:男
英雄姓名:趙雲 英雄年齡:21 英雄性別:男
英雄姓名:月英 英雄年齡:19 英雄性別:女
排序後打印
英雄姓名:月英 英雄年齡:19 英雄性別:女
英雄姓名:張飛 英雄年齡:20 英雄性別:男
英雄姓名:趙雲 英雄年齡:21 英雄性別:男
英雄姓名:關羽 英雄年齡:22 英雄性別:男
英雄姓名:劉備 英雄年齡:23 英雄性別:男
請按任意鍵繼續. . .