复神 2021-12-28 20:07 采纳率: 80%
浏览 232
已结题

正确的应输出最高最低分。以文本形式建立初始数据文件,文件名为,file1.dat,请输入十个学生的学号,姓名及考试成绩,读入file1.dat中的数据,输出最高分与最低分

img

img

img


#include <stdio.h>
#include <stdlib.h>
#define N 10
struct student
{
int num;
char name[20];
int score;
};
int main()
{
int i;
student st,stmax,stmin;
FILE *fp;
stmax.score=0;
stmin.score=100;
fp=fopen("file1.dat","r");
if(!fp)
exit(0);
for(i=0;i<N;i++)
{
fscanf(fp,"%d %s %d",&st.num,st.name,&st.score);
if(st.score>stmax.score)
stmax=st;
if(st.score<stmin.score)
stmin=st;
}
fclose(fp);
printf("high:%5d%15s%5d\n",stmax.num,stmax.name,stmax.score);
printf("low:%5d%15s%5d\n",stmin.num,stmin.name,stmin.score);
return 0;
}

  • 写回答

1条回答 默认 最新

  • 书山客 2021-12-28 20:24
    关注
    
    #include <stdio.h>
    #include <stdlib.h>
    #define N 10
    struct student
    {
        int num;
        char name[20];
        int score;
    };
    int main()
    {
        int i;
        student st, stmax, stmin;
        FILE* fp;
        stmax.score = 0;
        stmin.score = 100;
        fp = fopen("file1.dat", "r");
        if (!fp)
            exit(0);
        for (i = 0; i < N; i++)
        {
            fscanf(fp, "%d %s %d", &st.num, st.name, &st.score);
            if (st.score > stmax.score)
                stmax = st;
            if (st.score < stmin.score)
                stmin = st;
        }
        fclose(fp);
        printf("high:%5d%15s%5d\n", stmax.num, stmax.name, stmax.score);
        printf("low:%5d%15s%5d\n", stmin.num, stmin.name, stmin.score);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月8日
  • 已采纳回答 12月31日
  • 创建了问题 12月28日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理