第四次作业

6-1 按等级统计学生成绩

1.设计思路

(1)主要描述题目算法

①由题可知需要根据学生的成绩score设置其等级grade

②定义数据结构

③利用for循环逐次判断

④利用if语句进行判断

(2)流程图

 

2.实验代码

#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 count = 0, i;
    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.本题调试过程碰到问题及解决办法

①无

Git地址:https://git.coding.net/shaonian29/123456.git

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

1.设计思路

(1)主要描述题目算法

①定义结构体

②进行循环嵌套

③利用if语句进行调换顺序

2.实验代码

#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<n;i++,p++)
    {
        p->sum = p->score[0]+p->score[1]+p->score[2];
    }
}

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

 


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

git地址:https://git.coding.net/shaonian29/654321.git

 

posted @ 2018-05-03 20:40  郑宏泽  阅读(144)  评论(1编辑  收藏  举报