quiettttt 2022-08-27 06:42 采纳率: 97.9%
浏览 124
已结题

想知道多文件那条程序到底哪里有bug?

//a.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CSIZE 4

struct name
{
    char fname[30];
    char lname[30];
};

struct student
{
    struct name sname;
    float grade[3];
    float average;
};

void getGrade(struct student a[])
{
    int n = 0, flag = 1;
    char f[30], l[30];
    int i;
    while(flag)
    {
        printf("Input the name:");
        scanf("%s %s", f, l);
        for (i = 0; i < CSIZE; ++i)
        {
            if(!strcmp(a[i].sname.fname, f) &&
            !strcmp(a[i].sname.lname, l))
            {
                if(a[i].grade[0] != 0)
                {
                    printf("This student has been registered\n");
                    break;
                }
                printf("Input 3 grade for %s%s:", f, l);
                scanf("%f %f %f",
                      &a[i].grade[0],
                      &a[i].grade[1],
                      &a[i].grade[2]);
                n++;
                if(n == CSIZE)
                {
                    flag = 0;
                }
                break;
            }
        }
        if(i == CSIZE)
        {
            printf("Student %s%s is not found\n", f, l);
        }
    }
}

void getAverage(struct student a[])
{
    for (int i = 0; i < CSIZE; ++i) {
        a[i].average = (a[i].grade[0] + a[i].grade[1] + a[i].grade[2]) / 3;
    }
}

void Print(struct student a[])
{
    for (int i = 0; i < CSIZE; ++i) {
        printf("%s%s : %5.2f %5.2f %5.2f, average = %5.2f\n", a[i].sname.fname,
               a[i].sname.lname, a[i].grade[0], a[i].grade[1], a[i].grade[2], a[i].average);
    }
}

void APrint(struct student a[])
{
    float sum = 0.0;
    for (int i = 0; i < CSIZE; ++i) {
        sum += a[i].average;
    }
    printf("Class average is %.2f\n", sum / 4);
}

int main(void)
{
    struct student stu[CSIZE] = {
            {.sname = {"1", "2"}, .grade = {0.0, 0.0, 0.0}},
            {.sname = {"3", "4"}, .grade = {0.0, 0.0, 0.0}},
            {.sname = {"5", "6"}, .grade = {0.0, 0.0, 0.0}},
            {.sname = {"7", "8"}, .grade = {0.0, 0.0, 0.0}}
    };
    getGrade(stu);
    getAverage(stu);
    Print(stu);
    APrint(stu);
    return 0;
}

这条代码可以运行 , 也可以正常输入输出。


//b.c
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
#include <ctype.h>
#include "practiceit.h"
#include <math.h>
int main(void)
{

    //全部初始化,不然不能把整个Student传进去。
    Student student[CSIZE] ={
            {{"a", "A"} ,
                    {0.0,0.0 , 0.0},
                    0.0},
            {{"b", "B"},
                    {0.0,0.0 , 0.0},
                    0.0},
            {{"c", "C"},
                    {0.0,0.0 , 0.0},
                    0.0},
            {{"d", "D"},
                    {0.0,0.0 , 0.0},
                    0.0}
    };

    Getgread(student);
    doave(student);
    showit(student);
    showave(student);

    return 0;
}
//practice.c
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
#include <ctype.h>
#include "practiceit.h"
#include <math.h>
void Getgread(Student arr[])//输入数据
{
    int i;
    int num = 0 ;
    int flag = 1;
    char last[10];
    char first[10];

    while(flag)
    {
        printf("Enter lname and fname!\n");
        scanf_s("%s %s" , last , first);
        for(i = 0 ; i < CSIZE ; i++)
        {
            if(!strcmp(arr[i].idcard.lname , last) &&
                !strcmp(arr[i].idcard.fname , first))//定位
            {
                if(arr[i].grade.Chinese != 0)
                {
                    printf("again!\n");
                    break;
                }

                puts("Enter your grade!\n");
                scanf_s("%f %f %f" ,
                        &arr[i].grade.Chinese,
                        &arr[i].grade.Math,
                        &arr[i].grade.English);
                num++;
                if(num == 4)
                {
                    flag = 0;
                }
                break;
            }
        }
        if(i == CSIZE)//四次没找到
            printf("NO him!\n");
    }
}

void doave(Student arr[])//算平均分
{
    //float total = 0.0 ;
    for (int i = 0; i < CSIZE; ++i)
    {
        arr[i].avergae =
               ( arr[i].grade.Chinese +
                arr[i].grade.Math +
                arr[i].grade.English) / 3;
    }
}

void showit(Student arr[])//展示所有数据
{
    for (int i = 0; i < CSIZE; ++i)
    {
        printf("%s%s : %5.2f %5.2f %5.2f, average = %5.2f\n",
        arr[i].idcard.lname , arr[i].idcard.fname ,
               arr[i].grade.Chinese ,
               arr[i].grade.Math ,
               arr[i].grade.English,
               arr[i].avergae);
    }
}
void showave(Student arr[])//展示总平均分
{
    float total = 0.0;

    for (int i = 0; i < CSIZE; ++i)
    {
        total += arr[i].avergae;
    }
    printf("%5.2f" , total / 4);
}


//practice.h
#define CSIZE 4

typedef struct {
    char lname[10];
    char fname[10];
}Idcard;

typedef struct {
    float Chinese;
    float Math;
    float English;
}Grade;

typedef struct {
    Idcard idcard ;
    Grade grade;
    float avergae;
}Student;

void Getgread(Student arr[]);//输入数据
void doave(Student arr[]);//算平均分

void showit(Student arr[]);//展示所有数据
void showave(Student arr[]);//展示总平均分
#endif //PRACTICE_PRACTICEIT_H

这三条代码是同一个程序的,只不过想把a.c这个程序换成多文件形式。但是多文件那个程序就可以正常运行,但是不能正常输入输出,看了好久都没看出哪里有问题T T。想知道多文件那条程序到底哪里有bug?

  • 写回答

5条回答 默认 最新

  • 四海一叶秋 2022-08-27 09:30
    关注

    practice.c文件19行错了:

    scanf_s("%s %s" , last , first);
    

    应改成

    scanf_s("%s %s", last,10,first,10);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 9月4日
  • 已采纳回答 8月27日
  • 修改了问题 8月27日
  • 赞助了问题酬金5元 8月27日
  • 展开全部

悬赏问题

  • ¥35 VBA-JSON中文乱码报错
  • ¥50 dac adc的检定规程
  • ¥20 MIT控制器能控制不稳定系统吗
  • ¥15 公司代码X对业务伙伴X无效,处理方法?
  • ¥15 微信内链接跳转到浏览器打开怎么实现
  • ¥15 三角波可以直接加施密特电路整形到矩形波吗实物
  • ¥15 html,php,在使用html请求php文件时发生了错误,无法请求到php文件读取数据库并用javascript进行数据显示,刷新
  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错