我的程序快完成了。问题是,我在使用指针进行调试时遇到了一些小问题。我想要一些关于如何修复它的帮助。请注意,遗憾的是我确实需要所有函数原型(prototype)。 (如果我可以在 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;
}
最佳答案
我建议您创建一个结构体来封装学生的所有信息,而不是为要存储的每个数据使用不同的数组。
struct{
int studentId;
char pass;
int hw[7]
int mid[2];
int final;
int extra;
float total;
} student;
student mystudents[STDENTS];
无论您的问题是什么,使用更紧凑的数据结构都会更容易检测到,如果必须进行任何更改也将变得容易。
关于c - 调试程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11871435/