Scx' 2022-12-29 21:29 采纳率: 90.9%
浏览 56
已结题

可以帮看一下这个代码吗

问题遇到的现象和发生背景
遇到的现象和发生背景,请写出第一个错误信息
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define N 3

typedef struct
{
    unsigned int day;    // should be checked against 28/29/30/31
    unsigned int month;    // considering using an enum type:January/February/... 
    unsigned int year;    // should be given a reasonable range
} DATE;

typedef struct
{
    char    ID[20];        // avoid using as an interger
    char    name[20];
    char    gender;        // considering using an enum type:M/F/O
    DATE    birthday;
    float    height;
    float    weight;
    float    BMI;
    char    BMI_grade[20];
} STAFF;

//BMI grading strings
const char* BMI_grade[5] = {
    "under Weight",
    "Normal",
    "Over Weight",
    "Obese",
    "Extremely Obese" };

int main(int argc, char* argv[])
{
    // function declarations
    int input(STAFF * p);
    int process(STAFF * p);
    int sort(STAFF * p);
    int output(STAFF * p);
    int save(char* filename, STAFF * p);

    //array definitions
    STAFF staff[N];
    char filename[20];

    // parameter processing
    if (argc != 2)
    {
        printf("Too many or few parameters provided!\n");
        printf("Usage: exp01.exe HealthCheck.txt\n");
        exit(0);
    }
    else
    {
        strcpy(filename, argv[1]);
    }

    // function calls
    input(staff);
    process(staff);
    sort(staff);
    output(staff);
    save(filename, staff);

    return 0;
}

int input(STAFF* p)
{
    printf("Stasff data inputting...\n");
    for (int i = 0; i < N; i++, p++)
    {
        setbuf(stdin, NULL); // to empty the keyboard file buffer

        printf("\nNow Please input the %d(th) person's data\n", i);

        printf("  ID: ");
        gets_s(p->ID);

        printf("  Name: ");
        gets_s(p->name);

        printf("  Gender(F/M): ");
        p->gender = getchar();
        while (p->gender != 'm' && p->gender != 'M' && p->gender != 'f' && p->gender != 'F')
        {
            printf("  Invalid gender input! Retry as (f/F/m/M): ");
            getchar();
            p->gender = getchar();
        }

        printf("  Birthday(year month day): ");
        scanf("%d%d%d",
            &(p->birthday.year),
            &(p->birthday.month),
            &(p->birthday.day));

        // The following codes can be further tuned to chenck the day element in a month-wise way
        while (p->birthday.year > 2000 ||
            p->birthday.year < 1950 ||
            p->birthday.month < 1 ||
            p->birthday.month > 12 ||
            p->birthday.day < 0 ||
            p->birthday.day   > 31)
        {
            printf("  Invalid month/day input! Retry Please : ");
            scanf("%d%d%d",
                &(p->birthday.year),
                &(p->birthday.month),
                &(p->birthday.day),50,50,50);
        }

        printf("  Height(cm): ");
        scanf_s("%f", &(p->height));

        printf("  Weight(kg): ");
        scanf_s("%f", &(p->weight));
    }
    printf("Stasff data inputting DONE\n");

    return 0;
}

int process(STAFF* p)
{
    printf("Stasff data processing...");
    for (int i = 0; i < N; i++, p++)
    {
        p->BMI = p->weight / p->height / p->height * 10000.0;

        if (p->BMI < 18.5)
            strcpy(p->BMI_grade, BMI_grade[0]);
        else if (p->BMI < 25.0)
            strcpy(p->BMI_grade, BMI_grade[1]);
        else if (p->BMI < 28.0)
            strcpy(p->BMI_grade, BMI_grade[2]);
        else if (p->BMI < 32.0)
            strcpy(p->BMI_grade, BMI_grade[3]);
        else
            strcpy(p->BMI_grade, BMI_grade[4]);
    }
    printf(" DONE\n");

    return 0;
}

int sort(STAFF* p)
{
    int i, j, k;
    STAFF temp;

    printf("Stasff data sorting...");
    for (i = 0; i < N - 1; i++)
    {
        k = i;
        for (j = i + 1; j < N; j++)
            if (p[j].BMI < p[k].BMI)
                k = j;
        if (k != i)
        {
            temp = p[i];
            p[i] = p[k];
            p[k] = temp;
        }
    }
    printf(" DONE\n");

    return 0;
}
int output(STAFF* p)
{
    printf("\nID\tName\tGender\tBirthday\tHeight(cm)\tWeight(kg)\tBMI\tBMI_Grade\n");
    for (int i = 0; i < N; i++, p++)
    {
        printf("%s\t%s\t%c\t%d-%d-%d\t%.2f\t%.2f\t%.2f\t%s\n",
            p->ID,
            p->name,
            p->gender,
            p->birthday.year,
            p->birthday.month,
            p->birthday.day,
            p->height,
            p->weight,
            p->BMI,
            p->BMI_grade
        );
    }

    return 0;
}
int save(char* filename, STAFF* p)
{
    FILE* fp;
    if ((fp = fopen(filename, "w")) == NULL)
    {
        printf("File open error.\n");
        exit(0);
    }

    printf("Stasff data saving...");
    fprintf(fp, "ID\tName\tGender\tBirthday\tHeight(cm)\tWeight(kg)\tBMI\tBMI_Grade\n");
    for (int i = 0; i < N; i++, p++)
    {
        fprintf(fp, "%s\t%s\t%c\t%d-%d-%d\t%.2f\t%.2f\t%.2f\t%s\n",
            p->ID,
            p->name,
            p->gender,
            p->birthday.year,
            p->birthday.month,
            p->birthday.day,
            p->height,
            p->weight,
            p->BMI,
            p->BMI_grade
        );
    }
    printf(" DONE\n");

    fclose(fp);

    return 0;
}

运行结果及详细报错内容

打开调试一会就自动关掉了,scanf显示返回值被忽略

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”
  • 写回答

1条回答 默认 最新

  • 浪客 2022-12-29 21:54
    关注

    112行的3个50可以删了。
    返回值被忽略可以被忽略

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月6日
  • 已采纳回答 12月29日
  • 修改了问题 12月29日
  • 创建了问题 12月29日

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题