中国大学MOOC C语言程序设计(大连理工大学) 课后编程题 第十四周题解(个人向仅供参考)

#include <stdio.h>

struct student{
    long int num;
    char name[20];
    float score[3];
}stu[10],*p;

int rank(float *ave, float score){
    int rank = 10, i;
    for( i=0; i<10; ++i)
        if(ave[i]<score)
            rank--;
    return rank;
}

int main()
{
    FILE *fp;
    fp = fopen("student1.txt","r");
    float ave[10] = {0.0};
    int index = 0;
    for(p=stu; p<stu+10; ++p)
	{ 
        fscanf(fp,"%ld %s %f %f %f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]);
        ave[index++] = (p->score[0]+p->score[1]+p->score[2])/3.0;
    }
    printf("Number:  Name:        score:       Average    rank:\n");
    for(index = 0, p=stu; p<stu+10; ++p){
        printf("%-9ld%-10s%-5.1f%-5.1f%-8.1f%-10.1f%-d\n",p->num,p->name,p->score[0],
        p->score[1],p->score[2],ave[index++],rank(ave,ave[index]));
    }
    fclose(fp);
    fp = fopen("student2.txt","w");
    for(index = 0, p=stu; p<stu+10; ++p){
        fprintf(fp,"%-9ld%-10s%-5.1f%-5.1f%-8.1f%-10.1f%-d\n",p->num,p->name,p->score[0],
        p->score[1],p->score[2],ave[index++],rank(ave,ave[index]));
    }
    fclose(fp);
    return 0;
 }

输出结果(在屏幕上以及student2.txt文件中显示)
在这里插入图片描述

最后一周的c语言慕课作业了,希望大家多多支持。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael 2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值