ZBOY0408 2022-06-15 23:12 采纳率: 100%
浏览 157
已结题

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

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


 
#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;
}

为什么我运行的结果是这样的

img

  • 写回答

2条回答 默认 最新

  • 关注

    应该是你编译器没有设置好

    或者
    student st, stmax, stmin;
    改成
    struct student st, stmax, stmin;
    看看, 有的编译器声明变量时struct不能省略

    #include <stdio.h>
    #include <stdlib.h>
    #define N 10
    struct student
    {
        int num;
        char name[20];
        int score;
    };
    int main()
    {
        int i;
        struct student st, stmax, stmin;//加上    struct
        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;
    }
    

    或者改成这样

    typedef struct
    {
        int num;
        char name[20];
        int score;
    } student;
    

    就可以直接用 student st, stmax, stmin;

    
    #include <stdio.h>
    #include <stdlib.h>
    #define N 10
    typedef struct
    {
        int num;
        char name[20];
        int score;
    } student;
    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条)

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 创建了问题 6月15日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line