修改代码的错误 void xs(int n) { struct score stu[100]; int i; printf("成绩信息如下:\n"); FILE* fp; fp = fopen("C:\\2023-05\\student.txt", "r"); if (fp == NULL) { printf("打开文件失败,请重试!\n"); _getch(); fclose(fp); exit(0); } printf("学号\t姓名\tC语言\t高数\t大物\t总分\t名次\t平均分\n"); int c = 0; while (!feof(fp)) { fscanf(fp, "%s %s %d %d %d %d %d %f\n", &stu[c].snum, &stu[c].sname, &stu[c].chinese, &stu[c].math, &stu[c].english, &stu[c].sumstudent, &stu[c].rank, &stu[c].avg); c++; } if (n != 0) {//人数不为0 for (i = 0; i < c; i++) { printf("%s\t%s\t%d\t%d\t%d\t%d\t%d\t%.1f\n", stu[i].snum, stu[i].sname, stu[i].chinese, stu[i].math, stu[i].english, stu[i].sumstudent, stu[i].rank, stu[i].avg); } } fclose(fp); printf("显示完毕 ,请按任意键返回\n"); _getch(); return; } void jisuan(int n) { struct score stu[100]; int i; printf("成绩信息如下:\n"); FILE* fp; fp = fopen("C:\\2023-05\\student.txt", "r"); if (fp == NULL) { printf("打开文件失败,请重试!\n"); _getch(); fclose(fp); exit(0); } int c = 0; while (!feof(fp)) { fscanf(fp, "%s %s %d %d %d %d %d %f\n", &stu[c].snum, &stu[c].sname, &stu[c].chinese, &stu[c].math, &stu[c].english, &stu[c].sumstudent, &stu[c].rank, &stu[c].avg); c++; } if (n != 0) {//人数不为0 float sum = 0; for (i = 0; i < c; i++) { sum = sum + stu[i].chinese; } printf("C语言平均分:%.1f\n", sum / c + 1); sum = 0; for (i = 0; i < c; i++) { sum = sum + stu[i].math; } printf("高数平均分:%.1f\n", sum / c + 1); sum = 0; for (i = 0; i < c; i++) { sum = sum + stu[i].english; } printf("大物平均分:%.1f\n", sum / c + 1); } fclose(fp); printf("显示完毕 ,请按任意键返回\n"); _getch(); return; }
时间: 2024-01-30 16:03:13 浏览: 74
go语言用printf打印 Go 结构体(struct)信息:fmt.Printf(“%+v”, 结构变量名)
在这段代码中,有一些错误需要修改:
1. 在fscanf函数中,字符串类型的参数需要使用数组,而不是单个字符的指针,所以需要将&stu[c].snum和&stu[c].sname改为stu[c].snum和stu[c].sname。
2. 在计算平均分时,需要将除数改为c,而不是c+1。因为c已经表示了学生的总数,不需要再加1。
修改后的代码如下:
```
void xs(int n) {
struct score stu[100];
int i;
printf("成绩信息如下:\n");
FILE* fp;
fp = fopen("C:\\2023-05\\student.txt", "r");
if (fp == NULL)
{
printf("打开文件失败,请重试!\n");
_getch();
fclose(fp);
exit(0);
}
printf("学号\t姓名\tC语言\t高数\t大物\t总分\t名次\t平均分\n");
int c = 0;
while (!feof(fp))
{
fscanf(fp, "%s %s %d %d %d %d %d %f\n", stu[c].snum, stu[c].sname, &stu[c].chinese, &stu[c].math, &stu[c].english, &stu[c].sumstudent, &stu[c].rank, &stu[c].avg);
c++;
}
if (n != 0) {//人数不为0
for (i = 0; i < c; i++)
{
printf("%s\t%s\t%d\t%d\t%d\t%d\t%d\t%.1f\n", stu[i].snum, stu[i].sname, stu[i].chinese, stu[i].math, stu[i].english, stu[i].sumstudent, stu[i].rank, stu[i].avg);
}
}
fclose(fp);
printf("显示完毕 ,请按任意键返回\n");
_getch();
return;
}
void jisuan(int n) {
struct score stu[100];
int i;
printf("成绩信息如下:\n");
FILE* fp;
fp = fopen("C:\\2023-05\\student.txt", "r");
if (fp == NULL)
{
printf("打开文件失败,请重试!\n");
_getch();
fclose(fp);
exit(0);
}
int c = 0;
while (!feof(fp))
{
fscanf(fp, "%s %s %d %d %d %d %d %f\n", stu[c].snum, stu[c].sname, &stu[c].chinese, &stu[c].math, &stu[c].english, &stu[c].sumstudent, &stu[c].rank, &stu[c].avg);
c++;
}
if (n != 0)
{//人数不为0
float sum = 0;
for (i = 0; i < c; i++)
{
sum = sum + stu[i].chinese;
}
printf("C语言平均分:%.1f\n", sum / c);
sum = 0;
for (i = 0; i < c; i++)
{
sum = sum + stu[i].math;
}
printf("高数平均分:%.1f\n", sum / c);
sum = 0;
for (i = 0; i < c; i++)
{
sum = sum + stu[i].english;
}
printf("大物平均分:%.1f\n", sum / c);
}
fclose(fp);
printf("显示完毕 ,请按任意键返回\n");
_getch();
return;
}
```
阅读全文