首页 > 代码库 > 抄写例题作业1

抄写例题作业1

1.例9.1

  (1)代码实现

 1 #include<stdio.h>
 2 int main()
 3 {
 4     struct stu
 5     {
 6         long int num;
 7         char name[20];
 8         char sex[3];
 9         char addr[20];
10     }a={1010,"董诗原","","hkj"};
11     printf("No:%d\nname:%s\nsex:%s\naddr:%s",a.num,a.name,a.sex,a.addr);
12  } 

   (2)运行结果

No:1010
name:董诗原
sex:男
addr:hkj
--------------------------------
Process exited after 0.4101 seconds with return value 0
请按任意键继续. . .

2.例9.2

  (1)代码实现

 1 #include<stdio.h>
 2 int main()
 3 {
 4     struct stu
 5     {
 6         long int num;
 7         char name[20];
 8         float score;
 9     }stu1,stu2;
10     scanf("%d%s%f",&stu1.num,stu1.name,&stu1.score);
11     scanf("%d%s%f",&stu2.num,stu2.name,&stu2.score);
12     printf("The higher score is:\n");
13     if(stu1.score>stu2.score)
14     printf("%d %s %.2f\n",stu1.num,stu1.name,stu1.score);
15     else if(stu1.score<stu2.score)
16     printf("%d %s %.2f\n",stu2.num,stu2.name,stu2.score);
17     else{
18         printf("%d %s %.2f\n",stu1.num,stu1.name,stu1.score);
19         printf("%d %s %.2f\n",stu2.num,stu2.name,stu2.score);
20     }
21  } 

  (2)运行结果

10101 wang 89
10103 ling 90
The higher score is:
10103 ling 90.00

--------------------------------
Process exited after 25.17 seconds with return value 0
请按任意键继续. . .

3.例9.3

  (1)代码实现

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     struct per{
 6         char name[20];
 7         int count;
 8     }a[3]={"li",0,"zhang",0,"sun",0};
 9     char b[20];
10     for(int i=0;i<10;i++)
11     {
12         scanf("%s",&b);
13         if(strcmp(b,a[0].name)==0)
14         a[0].count++;
15         else if(strcmp(b,a[1].name)==0)
16         a[1].count++;
17         else
18         a[2].count++;
19     }
20     printf("\nResult:\nli:%d\nzhang:%d\nsun:%d\n",a[0].count,a[1].count,a[2].count);
21  } 

  (2)运行结果

li
li
sun
zhang
zhang
sun
li
sun
zhang
li

Result:
li:4
zhang:3
sun:3

--------------------------------
Process exited after 55.91 seconds with return value 0
请按任意键继续. . .

4.例9.4

 (1)代码实现

 1 #include<stdio.h>
 2 struct stu
 3 {
 4     long int num;
 5     char name[20];
 6     float score;
 7 };
 8 int main()
 9 {
10     struct stu t;
11     struct stu s[5]={10101,"zhang",78,10103,"wang",98.5,10106,"li",86,10108,"ling",73.5,10100,"sun",100 };
12     for(int i=0;i<5;i++){
13         for(int j=1;j<5-i;j++)
14         {
15             if(s[j].score>s[j-1].score)
16             {
17                 t=s[j];
18                 s[j]=s[j-1];
19                 s[j-1]=t;
20             }
21         }
22     }
23     for(int i=0;i<5;i++)
24     {
25         printf("%d%8s%8.2f\n",s[i].num,s[i].name,s[i].score); 
26     }
27 }

  (2)运行结果

10100     sun  100.00
10103    wang   98.50
10106      li   86.00
10101   zhang   78.00
10108    ling   73.50

--------------------------------
Process exited after 0.46 seconds with return value 0
请按任意键继续. . .

5.例9.5

 (1)代码实现

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     struct stu
 6     {
 7         long int num;
 8         char name[20];
 9         char sex;
10         float score;
11     };
12     struct stu stu_1;
13     struct stu *p;
14     p=&stu_1;
15     strcpy(stu_1.name,"Li lin");
16     stu_1.num=10101;
17     stu_1.sex=M;
18     stu_1.score=89.5;
19     printf("No:%d\nname:%s\nsex:%c\nscore:%.2f\n\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
20     printf("No:%d\nname:%s\nsex:%c\nscore:%.2f",(*p).num,(*p).name,(*p).sex,(*p).score);
21 }

(2)运行结果

No:10101
name:Li lin
sex:M
score:89.50

No:10101
name:Li lin
sex:M
score:89.50
--------------------------------
Process exited after 0.4481 seconds with return value 0
请按任意键继续. . .

6.例9.6

 (1)代码实现

 1 #include<stdio.h>
 2 #include<string.h>
 3 struct stu
 4 {
 5     long int num;
 6     char name[20];
 7     char sex;
 8     int age;
 9 };
10 struct stu a[3]{10101,"Li lin",M,18,10102,"Zhang fang",M,19,10104,"Wang min",F,20};
11 int main()
12 {
13     struct stu *p;
14     printf(" No.        name     sex    age\n");
15     for(p=a;p<a+3;p++)
16     printf("%5d%13s%5c%7d\n",p->num,p->name,p->sex,p->age);
17 }

 (2)运行结果

 No.        name     sex    age
10101       Li lin    M     18
10102   Zhang fang    M     19
10104     Wang min    F     20

--------------------------------
Process exited after 0.1007 seconds with return value 0
请按任意键继续. . .

7.例9.7

 (1)代码实现

 1 #include<stdio.h>
 2 #include<string.h>
 3 struct stu
 4 {
 5     long int num;
 6     char name[20];
 7     float score[3];
 8     float aver;
 9 };
10 struct stu a[3];
11 int main()
12 {
13     printf("请输入各学生信息:学号,姓名,三门课成绩:\n");
14     for(int i=0;i<3;i++)
15     {
16         a[i].aver=0;
17         scanf("%d%s",&a[i].num,a[i].name);
18         for(int j=0;j<3;j++)
19         {
20             scanf("%f",&a[i].score[j]);
21             a[i].aver=a[i].aver+a[i].score[j];
22         }
23         a[i].aver/=3;
24     }
25     int max=a[0].aver;
26     int count;
27     for(int i=1;i<3;i++)
28     {
29         if(a[i].aver>max){
30             max=a[i].aver;
31             count=i;
32         }
33     }
34     printf("\n成绩最高的学生是:\n");
35     printf("学号:%d\n姓名:%s\n",a[count].num,a[count].name);
36     printf("三门课成绩: ");
37     for(int i=0;i<3;i++)
38     printf("%.1f ",a[count].score[i]);
39     printf("\n平均成绩:%.2f",a[count].aver);
40 }

 (2)运行结果

请输入各学生信息:学号,姓名,三门课成绩:
10101 li 78 89 98
10103 wang 98.5 87 69
10106 sun 88 76.5 89

成绩最高的学生是:
学号:10101
姓名:li
三门课成绩: 78.0 89.0 98.0
平均成绩:88.33
--------------------------------
Process exited after 64.47 seconds with return value 0
请按任意键继续. . .

 

抄写例题作业1