第四次作業


題目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)的代碼行數和所用時間、博客字數和所用時間


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM