The fourth homework of c language

6-1 Statistical student grades
1. Design ideas
(1) The first step: observe the meaning of the question to understand the meaning of each parameter and the required function in the question; the
second step: design an algorithm to write a function, and let the function of the function realize the question The function required in the
third step: use the object-oriented pointing, run the program to detect whether there is an error.
(2) Flowchart
None
2. Experimental code

//项目名称:按等级统计学生成绩

#include <stdio.h>
#define MAXN 10

struct student{                                             //构造结构体
    int num;
    char name[20];
    int score;
    char grade;
};

int set_grade( struct student *p, int n );

int main()
{   struct student stu[MAXN], *ptr;
    int n, i, count;

    ptr = stu;
    scanf("%d\n", &n);
    for(i = 0; i < n; i++){
       scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
    }
   count = set_grade(ptr, n);
   printf("The count for failed (<60): %d\n", count);
   printf("The grades:\n");
   for(i = 0; i < n; i++)
       printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade);
    return 0;
}

int set_grade( struct student *p, int n )
{
    int i,count = 0;
    for(i=0;i<n;i++,p++)
    {
       if(p->score<60) {
       p->grade='D';
       count++;
       }
       else if((p->score<70)&&(p->score>=60))
       {
       p->grade='C';
       }
       else if((p->score<85)&&(p->score>=70))
       {
       p->grade='B';
       }
       else{
       p->grade='A';
       }
    }
    return count;
}

3. Problems encountered in the debugging process of this question and solutions
Error message 1: No
Error reason: No
Correction method: No
Git address: https://coding.net/u/zhong123456/p/pta4/git/blob/master/6 -1?public=true

6-2 The structure array is sorted according to the total score
1. Design ideas
(1) The first step: observe the meaning of the question to understand the meaning of each parameter and the required function in the question; the
second step: design the algorithm to write the function, so that the function of the function Implement the functions required in the question;
Step 3: This question uses bubble sorting and the pointing to the object in the structure to run the program to detect whether there is an error.
(2) Flowchart
None
2. Experimental code

#include <stdio.h>
struct student
{
int num;
char name[15];
float score[3];
float sum;
};
void calc(struct student *p,int n);
void sort(struct student *p,int n);
int main()
{
struct student stu[5];
int i,j;
float f;
for(i=0;i<5;i++)
{
    scanf("%d%s",&stu[i].num,stu[i].name);
    for(j=0;j<3;j++)
    {
        scanf("%f",&f);
        stu[i].score[j]=f;
    }
}
calc(stu,5);
sort(stu,5);
for(i=0;i<5;i++)
{
    printf("%5d%15s",stu[i].num,stu[i].name);
    printf("  %.1f  %.1f  %.1f  %.1f\n",stu[i].score[0],stu[i].score[1],stu[i].score[2], stu[i].sum);
}
return 0;
}

void calc(struct student *p,int n){
    int i;
    for(i=0;i<5;i++,p++)
    {
        p->sum=p->score[0]+p->score[1]+p->score[2];
    }
}

void sort(struct student *p,int n){
    struct student max;
    int i,j;
    for(i=0;i<n-1;i++){
        for(j=0;j<n-1-i;j++)
            if((p+j)->sum<(p+j+1)->sum)
        {
            max = *(p+j);
            *(p+j)=*(p+j+1);
            *(p+j+1)=max;
        }
    }
}

3. Problems encountered in the debugging process of this question and solutions
Error message 1: No
Error reason: No
Correction method: No
Git address: https://coding.net/u/zhong123456/p/pta4/git/blob/master/6 -2?public=true

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324654699&siteId=291194637
Recommended