结构体案例

 1 #include <iostream>
 2 #include <ctime>
 3 using namespace std;
 4 struct student
 5 {
 6     string s_name;
 7     int age;
 8     int score;
 9 };
10 struct teacher
11 {
12     string t_name;
13     struct student sarray[5];
14 };
15 void allocatespace(struct teacher tarray[],int len)
16 {
17     string nameseed="ABCDE";
18     for(int i=0;i<len;i++)
19     {
20         tarray[i].t_name = "teacher_";
21         tarray[i].t_name += nameseed[i];
22         for(int j=0;j<5;j++)
23         {
24             tarray[i].sarray[j].s_name ="student_";
25             tarray[i].sarray[j].s_name += nameseed[j];
26             int random = rand () % 61+40;   //40~100
27             tarray[i].sarray[j].score=random;
28         }
29 
30     }
31 }
32 void printinfo(struct teacher tarray[],int len)
33 {
34     for(int i=0;i<len;i++)
35     {
36         cout<<"老师姓名"<<tarray[i].t_name<<endl;
37         for(int j=0;j<5;j++)
38         {
39             cout<<"\t学生姓名"<<tarray[i].sarray[j].s_name<<" ";
40             cout<<"学生分数"<<tarray[i].sarray[j].score<<endl;
41         }
42     }
43 }
44 int main() {
45     srand((unsigned int)time(NULL));
46 struct teacher tarray[3];
47 int len= sizeof(tarray)/ sizeof(tarray[0]);
48     allocatespace(tarray,len);
49     printinfo(tarray,len);
50     return 0;
51 }

 

posted @ 2022-03-13 18:43  赵凤武  阅读(51)  评论(0)    收藏  举报