- 论坛徽章:
- 0
|
5可用积分
期末了我想写一个简单的程序,目标是将学生考试的客观题答案事先在Windows下输入文本文档保存起来,每输入完毕一个学生的所有答题结果(用大写字母表示)就换行输入下一个学生的答题结果直到全部答题输入完毕。然后用程序打开刚才的文本文档逐行读入学生的成绩,然后将每一行学生的成绩与答案字符串(用答鞋子目表示)进行比较之后计算该学生成绩与错误题号。以下是我的源码,但有一个问题,比方来说,我明明在保存学生答题结果的文本文档中输入的是四个学生的成绩,但在输出学生成绩情况的文本文档中确出现了八个输出结果。不知这是何原因所致,请教各位,谢谢!
/* File-based paper grading */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define SIZE 81
#define LEN 10
void grader(char * line,FILE * spew);
int main(void){
char filnam[SIZE],filnam_1[SIZE],temp[LEN];
FILE * feed,* spew;
printf("\nEnter the name of the input file: ");
gets(filnam);
printf("\nThen enter the name of the output file: ");
gets(filnam_1);
if((feed = fopen(filnam,"r")) == NULL){
fprintf(stderr,"\nUnable to open file %s.\n",filnam);
system("PAUSE");
exit(EXIT_FAILURE);
}
if((spew = fopen(filnam_1,"a")) == NULL){
fprintf(stderr,"\nUnable to create file %s.\n",filnam_1);
system("PAUSE");
exit(EXIT_FAILURE);
}
while(fgets(temp,LEN, feed) != NULL)
grader(temp,spew);
if(fclose(feed) != 0)
fprintf(stderr,"\nUable to close file %s.\n",filnam);
if(fclose(spew) != 0)
fprintf(stderr,"\nUnable to close file %s.\n",filnam_1);
puts("\nDone!");
system("PAUSE");
return 0;
}
void grader(char * line, FILE * spew){
char key[LEN] = "CBDBADBBBC";
int i;
int mark = 10;
int wrngnum[LEN] = {0};
for(i = 0; i < LEN; i++)
if(line != key){
wrngnum = i + 1;
mark--;
}
fprintf(spew,"\nThe mark for the test item is %d.\n"
"The sequence numbers of the wrong answer are: \n",mark);
for(i = 0; i < sizeof(wrngnum)/sizeof(wrngnum[0]); i++)
if(wrngnum != 0)
fprintf(spew,"%3d",wrngnum);
}
保存学生答题结果的文本文档中的内容是:
CDADBDBCCD
CDABBDBCCA
CBDBADBDBC
BCDBAABCBC
输出学生成绩情况的文本文档中却显示出以下结果:
The mark for the test item is 3.
The sequence numbers of the wrong answer are:
2 3 4 5 8 9 10
The mark for the test item is 2.
The sequence numbers of the wrong answer are:
1 2 3 4 5 8 9 10
The mark for the test item is 4.
The sequence numbers of the wrong answer are:
2 3 5 8 9 10
The mark for the test item is 3.
The sequence numbers of the wrong answer are:
1 2 3 5 8 9 10
The mark for the test item is 8.
The sequence numbers of the wrong answer are:
8 10
The mark for the test item is 6.
The sequence numbers of the wrong answer are:
2 3 8 10
The mark for the test item is 5.
The sequence numbers of the wrong answer are:
1 2 6 8 10
The mark for the test item is 5.
The sequence numbers of the wrong answer are:
2 3 6 8 10 |
|