一、题目要求

码云链接:https://git.oschina.net/awdx/homework4.git

 1 #include<stdio.h>
 2 struct Student
 3 {
 4     long int num;
 5     char name[30];
 6     char sex;
 7     char addr[100];
 8     
 9 };
10 int main()
11 {
12     struct Student a={10101,"LiLin",'M',"123 bei jing road"};
13     printf("num:%d\nname:%s\nsex:%c\naddr:%s\n",a.num,a.name,a.sex,a.addr);
14 } 
9.1
 1 #include<stdio.h>
 2 struct stu
 3 {
 4     int num;
 5     char name[30];
 6     float score;
 7 };
 8 int main()
 9 {
10     struct stu student1,student2;
11     scanf("%d%s%f",&student1.num,student1.name,&student1.score);
12     scanf("%d%s%f",&student2.num,student2.name,&student2.score);
13     if(student1.score>student2.score)
14     printf("%d  %s  %6.2f",student1.num,student1.name,student1.score);
15     else if(student1.score<student2.score)
16     printf("%d  %s %6.2f",student2.num,student2.name,student2.score);
17     else
18     {
19     printf("%d  %s  %6.2f",student1.num,student1.name,student1.score);
20     printf("%d  %s  %6.2f",student2.num,student2.name,student2.score);
21     }
22 }
9.2
 1 #include<string.h>
 2 #include<stdio.h>
 3 struct people
 4 {
 5     char name[20];
 6     int count;
 7 };
 8 int main()
 9 {
10      struct people a[3]={{"zhao",0},{"li",0},{"sun",0}};
11      int i,j;
12      char b[40];
13      for(i=0;i<=10;i++)
14      {scanf("%s",b);
15       for(j=0;j<3;j++)
16       if(strcmp(b,a[j].name)==0)
17          a[j].count++;
18      }
19       for(i=0;i<3;i++)
20       {printf("%s,%d\n",a[i].name,a[i].count);} 
21        return 0;
22 }
23  
9.3
 1 #include<stdio.h>
 2 struct student
 3 {
 4     int num;
 5     char name[20];
 6     float score;
 7 };
 8 int main()
 9 {
10    struct student Stu[5]={{10101,"Zhang",78},
11                           {10103,"Wang",98.5},
12                           {10106,"Li",86},
13                           {10108,"Ling",73.5},
14                           {10110,"Sun",100}};
15     struct student temp;
16     const int n=5;
17     int i,j,k;
18     printf("The order is:\n");
19     for(i=0;i<n-1;i++)
20     { k=i;
21     for(j=i+1;j<n;j++)
22     if(Stu[j].score>Stu[k].score)
23              k=j;
24     temp=Stu[k];Stu[k]=Stu[i];Stu[i]=temp;
25     }
26     for(i=0;i<n;i++)
27     printf("%6d %8s %6.2f\n",Stu[i].num,Stu[i].name,Stu[i].score);
28     printf("\n");
29     return 0;
30 } 
9.4
 1 #include<stdio.h>
 2 #include<string.h>
 3 struct Student
 4 {
 5     long num;
 6     char name[20];
 7     char sex;
 8     float score;
 9 };
10 int main()
11 {
12     struct Student stu_1;
13     struct Student   *p;
14     p=&stu_1;
15     stu_1.num=10101;
16     strcpy(stu_1.name,"Li Lin");
17     stu_1.sex='M';
18     stu_1.score=89.5;
19     printf("NO:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",
20             stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
21     printf("\nNO:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",
22            (*p).num,(*p).name,(*p).sex,(*p).score);
23     return 0;
24 } 
9.5
 1 #include<stdio.h>
 2 struct student
 3 {
 4     int num;
 5     char name[20];
 6     char sex;
 7     int age;
 8     
 9 };
10  struct student stu[3]={{10101,"LiLin",'M',10},
11                         {10102,"ZhangFang",'M',19},
12                         {10104,"Wang Min",'F',20}};
13 int main()
14 {
15     struct student *p;
16     printf("No    Name                 sex age\n");
17     for(p=stu;p<stu+3;p++)
18     printf("%5d %-20s %2c %4d\n",p->num,p->name,p->sex,p->age);
19     return 0;
20 }
9.6
 1 #include<stdio.h>
 2 #define N 3
 3 struct Student
 4 {  int num;
 5    char name[20];
 6    float score[3];
 7    float aver;
 8 };
 9 void input(struct Student stu[])
10 {
11     int i;
12     printf("请输入各学生信息、学号、姓名三门课成绩:\n");
13     for(i=0;i<N;i++)
14     {scanf("%d %s %f %f %f",&stu[i].num,stu[i].name,
15       &stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
16       stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
17     }
18 }
19 struct Student max(struct Student stu[])
20 {
21     int i,m=0;
22     for(i=0;i<N;i++)
23         if(stu[i].aver>stu[m].aver) m=i;
24          return stu[m];
25 }
26 void print(struct Student stud)
27 {
28     printf("\n学生的最高成绩是:\n");
29     printf("学号:%d\n姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",
30        stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
31 }
32 int main()
33 {
34 struct Student stu[N],*p=stu;
35        input(p);
36        print(max(p));
37        return 0;     
38 }
9.7

二、运行结果

1 num:10101
2 name:LiLin
3 sex:M
4 addr:123 bei jing road
5 
6 --------------------------------
7 Process exited after 0.259 seconds with return value 50
8 请按任意键继续. . .
9.1
1 10101 Li 96
2 10102 Sun 85
3 10101  Li   96.00
4 --------------------------------
5 Process exited after 35.58 seconds with return value 17
6 请按任意键继续. . .
9.2
 1 zhao
 2 li
 3 li
 4 zhao
 5 sun
 6 sun
 7 zhao
 8 li
 9 li
10 zhao
11 zhao
12 zhao,5
13 li,4
14 sun,2
15 
16 --------------------------------
17 Process exited after 21.04 seconds with return value 0
18 请按任意键继续. . .
9.3
 1 The order is:
 2  10110      Sun 100.00
 3  10103     Wang  98.50
 4  10106       Li  86.00
 5  10101    Zhang  78.00
 6  10108     Ling  73.50
 7 
 8 
 9 --------------------------------
10 Process exited after 0.3095 seconds with return value 0
11 请按任意键继续. . .
9.4
 1 NO:10101
 2 name:Li Lin
 3 sex:M
 4 score: 89.5
 5 
 6 NO:10101
 7 name:Li Lin
 8 sex:M
 9 score: 89.5
10 
11 --------------------------------
12 Process exited after 0.2318 seconds with return value 0
13 请按任意键继续. . .
9.5
No    Name                 sex age
10101 LiLin                 M   10
10102 ZhangFang             M   19
10104 Wang Min              F   20

--------------------------------
Process exited after 0.2395 seconds with return value 0
请按任意键继续. . .
9.6
 1 请输入各学生信息、学号、姓名三门课成绩:
 2 10101 linkang 85 72 63
 3 10102 dert 62 74 83
 4 10103 qwer 74 76 80
 5 
 6 学生的最高成绩是:
 7 学号:10103
 8 姓名:qwer
 9 三门课成绩: 74.0, 76.0, 80.0
10 平均成绩: 76.67
11 
12 --------------------------------
13 Process exited after 71.77 seconds with return value 0
14 请按任意键继续. . .
9.7

三、总结

这是关于结构体所编写的程序

9.1定义了结构体及该类型变量并赋初值,输出变量及成员。

9.2定义了结构体及该类型变量,用scanf函数输入两个结构体变量的成员,再用if判断成绩的大小,并输出。

9.3定义了结构体及该类型长度为3的数组并赋初值,定义两个整型循环变量,字符型数组长度为20利用for循环输入结构体成员

再嵌套for循环并通过判断使票数自加1最后通过for输出

9.4用结构体数组存放n个学生的信息,采用选择法对各元素进行排序。

9.5定义了结构体及该类型变量与指针使其指向变量的地址并给各个结构体成员赋值以变量形式stu.??和指针形式(*p).??输出。

9.6声明结构体类型-定义结构体数组-对此数组初始化-定义结构体指针-指针指向结构体-以指向->形式输出

9.7input函数   该输入

    max函数    求平均

     print函数   需输出

    main函数    合一起