结构体嵌套一个例子

有3名老师每名老师带5名学生做毕业设计

初始化信息

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct stu{
 5     string name;
 6     int age,score;
 7 };
 8 
 9 struct tec{
10     string name;
11     stu a[5];
12 };
13 
14 void Initinfo(tec ta[],int len)
15 {
16     string c = "ABCDE";
17     for(int i = 0;i < len;i++)
18     {
19         ta[i].name = "Teacher";  
20         ta[i].name += c[i];
21         for(int j = 0;j < 5;j++)
22         {
23             ta[i].a[j].name = "Student";
24             ta[i].a[j].name += c[j];
25             int ra = rand()%101;
26             int rb = rand()%6 + 18;
27             ta[i].a[j].score = ra;
28             ta[i].a[j].age = rb;
29         }
30     }
31 }
32 
33 void print(tec ta[],int len)
34 {
35     for(int i = 0;i < len;i++)
36     {
37         cout << "Teachername " << ta[i].name << endl;
38         for(int j = 0;j < 5;j++)
39         {
40             cout << "\t" << ta[i].a[j].name << " " << ta[i].a[j].age << " " << ta[i].a[j].score << endl;
41         }
42     }
43 }
44 
45 int main()
46 {
47     tec ta[3];
48     int len = sizeof(ta)/sizeof(ta[0]);
49     srand(int(time(0)));
50     Initinfo(ta,len);
51     print(ta,len);
52     
53     return 0;
54 }

猜你喜欢

转载自www.cnblogs.com/mch5201314/p/11573506.html
今日推荐