第四次作业

题目6-1 计算两数的和与差

1.设计思路

(1)

第一步:阅读题目要求。

第二步:根据题意设置变量编写程序。

(2):流程图

2.实验代码

 1 #include <stdio.h>
 2 #define MAXN 10
 3 
 4 struct student{
 5     int num;
 6     char name[20];
 7     int score;
 8     char grade;
 9 };
10 
11 int set_grade( struct student *p, int n );
12 
13 int main()
14 {   struct student stu[MAXN], *ptr;
15     int n, i, count;
16 
17     ptr = stu;
18     scanf("%d\n", &n);
19     for(i = 0; i < n; i++){
20        scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
21     } 
22    count = set_grade(ptr, n);
23    printf("The count for failed (<60): %d\n", count);
24    printf("The grades:\n"); 
25    for(i = 0; i < n; i++)
26        printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade);
27     return 0;
28 }
29 int set_grade( struct student *p, int n ){
30     int count = 0, i;
31     for(i = 0;i<n;i++,p++){
32         if(p->score<60){
33             p->grade = 'D';
34             count++;
35         }
36         else if((p->score<70)&&(p->score>=60)){
37             p->grade = 'C';
38         }
39         else if((p->score<85)&&(p->score>=70)){
40             p->grade = 'B';
41         }
42         else{
43             p->grade = 'A';
44         }
45     }
46     return count;
47 }

3.本题调试过程中碰到的问题及解决办法

git地址:https://coding.net/u/Drunktea/p/8.1/git/blob/master/8.1?public=true

 

6-2 结构体数组按总分排序

1.设计思路

(1)

第一步:阅读题目要求。

第二步:根据题意设置变量编写程序。

(2)流程图

2.实验代码

 1 #include <stdio.h>
 2 struct student                    
 3 {
 4 int num;
 5 char name[15];
 6 float score[3];
 7 float sum;
 8 };
 9 void calc(struct student *p,int n);     
10 void sort(struct student *p,int n);
11 int main()
12 {
13 struct student stu[5];
14 int i,j;
15 float f;
16 for(i=0;i<5;i++)
17 {
18     scanf("%d%s",&stu[i].num,stu[i].name);
19     for(j=0;j<3;j++)
20     { 
21         scanf("%f",&f);
22         stu[i].score[j]=f;
23     }
24 }
25 calc(stu,5);
26 sort(stu,5);
27 for(i=0;i<5;i++)
28 {
29     printf("%5d%15s",stu[i].num,stu[i].name);
30     printf("  %.1f  %.1f  %.1f  %.1f\n",stu[i].score[0],stu[i].score[1],stu[i].score[2], stu[i].sum);
31 }
32 return 0;
33 void calc(struct student *p,int n)
34 {
35     int i;
36     for(i=0;i<n;i++,p++)
37     {
38         p->sum = p->score[0]+p->score[1]+p->score[2];
39     }
40 }
41 
42 void sort(struct student *p,int n)
43 {
44     int i=0,j=0;
45     struct student t;
46     for(i=0;i<n;i++)
47     {
48         for(j=0;j<n-1;j++)
49         {
50             if(p[j].sum < p[j+1].sum)
51             {
52                 t      = p[j];
53                 p[j]   = p[j+1];
54                 p[j+1] = t;
55             }
56         }
57     }
58 }

3.本题调试过程中遇到的问题及解决方法:

git地址:https://coding.net/u/Drunktea/p/8.1/git/blob/master/8.2?public=true

近两周来所学知识点:

1.结构体的概念

2.定义结构体类型变量的方法

3.结构体数组

请用表格和折线图呈现你本周(4/9 8:00~4/26 8:00)的代码行数和所用时间、博客字数和所用时间

posted on 2018-04-21 20:53  王浩印  阅读(160)  评论(10编辑  收藏  举报

导航