0

我几乎完成了我的程序。问题是,我在使用指针进行调试时遇到了一些小问题。我想要一些关于如何修复它的帮助。请注意,我确实需要我所有的函数原型。(如果我能在 main 中做所有事情,分配会容易得多......)

#include <stdio.h>
#include <stdlib.h>

#define STUDENTS 5

int getData(int *student, char *pass, int *hw1, int *hw2, int *hw3, int *hw4,
             int *hw5, int *hw6, int *hw7, int *mid1, int *mid2, int *final,
             int *extra);
float calcAvg(float *total);
float printTable(int student, float total);

int main(void)

{
    int student[STUDENTS];
    char pass[STUDENTS];
    int hw1[STUDENTS], hw2[STUDENTS], hw3[STUDENTS], hw4[STUDENTS], hw5[STUDENTS], hw6[STUDENTS], hw7[STUDENTS];
    int mid1[STUDENTS], mid2[STUDENTS];
    int final[STUDENTS];
    int extra[STUDENTS];
    float total[STUDENTS];

    getData(student, pass, hw1, hw2, hw3, hw4, hw5, hw6, hw7,
            mid1, mid2, final, extra);
    calcAvg(total);
    printTable(student, total);

system("pause");
return 0;
}

int getData(int *student, char *pass, int *hw1, int *hw2, int *hw3, int *hw4,
             int *hw5, int *hw6, int *hw7, int *mid1, int *mid2, int *final,
             int *extra)
{
    FILE *fpIn;
    int i;

    fpIn = fopen("data.txt", "r");
    if (!fpIn)
    {
        printf("File does not exist.");
        system("pause");
        exit (101);
    }

    for (i=0; i<STUDENTS; i++)
    {
        fscanf(fpIn, "%d", &student[i]);
        fscanf(fpIn, "%c", &pass[i]);
        fscanf(fpIn, "%d %d %d %d %d %d %d", hw1[i], hw2[i], hw3[i], hw4[i], hw5[i], hw6[i], hw7[i]);
        fscanf(fpIn, "%d %d", mid1[i], mid2[i]);
        fscanf(fpIn, "%d %d", final[i], extra[i]);
    }

    return;
}

float calcAvg(float *total)
{
    int hwtotal[STUDENTS], midtotal[STUDENTS];
    int i;

    for (i=0; i<STUDENTS; i++)
    {
        hwtotal[i] = hw1[i]+ hw2[i]+hw3[i]+hw4[i]+hw5[i]+hw6[i]+hw7[i];
        midtotal[i] = mid1[i]+mid2[i];
        total[i] = (hwavg[i]+midavg[i]+final[i]+extra[i])/3;
    }

    return total;
}

float printTable(int student, float total)
{
    int i;

    printf("==== ======= =======\n");
    printf(" ID   Score   Grade\n");
    printf("==== ======= =======\n");

    for(i=0; i<STUDENTS; i++)
    {
        printf("%d .1f  ", student[i], total[i]);

        if (total>90)
        printf("A\n");
        else if(total>80);
        printf("B\n");
        else if (total>70);
        printf("C\n");
        else if (total>60);
        printf("D\n");
        else
        printf("F\n");
    }

    return;
}
4

2 回答 2

2

我建议您创建一个结构来封装学生的所有信息,而不是为您存储的每个数据使用不同的数组。

struct{ 
   int studentId;
   char pass;
   int hw[7]
   int mid[2];
   int final;
   int extra;
   float total;
} student;

 student mystudents[STDENTS];

无论您的问题是什么,使用更紧凑的数据结构将更容易检测到,如果必须进行任何更改,这些数据结构也将变得容易。

于 2012-08-08T19:25:46.523 回答
0

好吧,您没有描述您的问题,但看起来这部分可能会导致问题。

int getData(...) {
    ...
    fscanf(fpIn, "%d %d %d %d %d %d %d", hw1[i], hw2[i], hw3[i], hw4[i], hw5[i], hw6[i], hw7[i]);
    fscanf(fpIn, "%d %d", mid1[i], mid2[i]);
    fscanf(fpIn, "%d %d", final[i], extra[i]);
    ...
}

您将ints 传递给fscanf期望int *s 的对象。你早先做了正确的事

fscanf(fpIn, "%d", &student[i]);
fscanf(fpIn, "%c", &pass[i]);
于 2012-08-08T19:38:06.423 回答