- 论坛徽章:
- 0
|
我又找出一些问题,已更正,程序运行结果正常了。
下面是代码更正:
/* calc mark fileops*/
#include <stdio.h>
#include <stdlib.h>
#define LEN 41
#define SIZE_A 10
#define SIZE_B 5
#define STEP 2
int main(void){
char keys[LEN] = "dadacbcbadfftftttftffttttfftttbdccdabbaa";
char ans[LEN];
int wrng_1[SIZE_A] = {0}, wrng_2[SIZE_A] = {0}, wrng_3[SIZE_A] = {0},
wrng_4[SIZE_B] = {0}, wrng_5[SIZE_B] = {0};
int cnt = 0, sum_1, sum_2, sum_3, sum_4, sum_5;
int i,j;
FILE * fwrt;
if((fwrt = fopen("output.txt","a")) == NULL){
fprintf(stderr,"Error opening file!");
getch();
exit(EXIT_FAILURE);
}
puts("\nGet started!");
fprintf(fwrt,"\nInput the students' answers (Press Enter at the beginning "
"of the next line to quit):\n");
while(gets(ans) != NULL && ans[0] != '\0'){
sum_1 = SIZE_A * STEP;
sum_2 = SIZE_A * STEP;
sum_3 = SIZE_A * STEP;
sum_4 = SIZE_B * STEP;
sum_5 = SIZE_B * STEP;
for(i = 0; i < SIZE_A; i++){ /*这里wrng_1-3的数组内容要归零才不会给下面的记录行为留下上一个行为的残留*/
wrng_1 = 0;
wrng_2 = 0;
wrng_3 = 0;
}
for(j = 0; j < SIZE_B; j++){
wrng_4[j] = 0;
wrng_5[j] = 0;
}
fprintf(fwrt, "\nAnswers of student %d: %s", cnt+1, ans);
for(i = 0; i < LEN-1; i++){
if(ans != keys){
if((i+1) > 0 && (i+1) <= 10){
sum_1 -= STEP;
wrng_1 = i+1;
}
if((i+1) > 10 && (i+1) <= 20){
sum_2 -= STEP;
wrng_2[i - SIZE_A] = (i+1) - SIZE_A;
}
if((i+1) > 20 && (i+1) <= 30){
sum_3 -= STEP;
wrng_3[i - (SIZE_A * 2)] = (i+1) - (SIZE_A * 2);
}
if((i+1) > 30 && (i+1) <= 35){
sum_4 -= STEP;
wrng_4[i - (SIZE_A * 3)] = (i+1) - (SIZE_A * 3);
}
if((i+1) > 35 && (i+1) <= 40){
sum_5 -= STEP;
wrng_5[i - (SIZE_A * 3 + SIZE_B)] =
(i+1) - (SIZE_A * 3 + SIZE_B);
}
}
}
fprintf(fwrt, "\nResults of Student %d:\n",cnt + 1);
fprintf(fwrt, "\nItem One: Mark: %d, Wrong: ",sum_1);
for(i = 0; i < SIZE_A; i++)
if(wrng_1 != 0)
fprintf(fwrt, "%4d", wrng_1);
fprintf(fwrt, "\nItem Two: Mark: %d, Wrong: ",sum_2);
for(i = 0; i < SIZE_A; i++)
if(wrng_2 != 0)
fprintf(fwrt, "%4d", wrng_2);
fprintf(fwrt, "\nItem Three: Mark: %d, Wrong: ",sum_3);
for(i = 0; i < SIZE_A; i++)
if(wrng_3 != 0)
fprintf(fwrt, "%4d", wrng_3);
fprintf(fwrt, "\nItem Four: Mark: %d, Wrong: ",sum_4);
for(i = 0; i < SIZE_B; i++)
if(wrng_4 != 0)
fprintf(fwrt, "%4d", wrng_4);
fprintf(fwrt, "\nItem Five: Mark: %d, Wrong: ",sum_5);
for(i = 0; i < SIZE_B; i++)
if(wrng_5 != 0)
fprintf(fwrt, "%4d", wrng_5);
cnt++;
fprintf(fwrt,"\nInput the students' answers (Press Enter at the beginning "
"of the next line to quit):\n");
}
fprintf(fwrt,"%d students' data treated till now.\n", cnt);
fclose(fwrt);
puts("\nDone!");
getch();
return 0;
}
更正后程序运行情况:
输入:
Get Started!
cabaabccdaftttfttfftftftffftttbcbaacbada
cadbaabdadftttftfttffttttfftttadcaaddbbc
Done!
输出到文件的结果:
Input the students' answers (Press Enter at the beginning of the next line to quit):
Answers of student 1: cabaabccdaftttfttfftftftffftttbcbaacbada
Results of Student 1:
Item One: Mark: 8, Wrong: 1 3 5 8 9 10
Item Two: Mark: 10, Wrong: 2 4 5 9 10
Item Three: Mark: 16, Wrong: 3 5
Item Four: Mark: 2, Wrong: 2 3 4 5
Item Five: Mark: 4, Wrong: 1 3 4
Input the students' answers (Press Enter at the beginning of the next line to quit):
Answers of student 2: cadbaabdadftttftfttffttttfftttadcaaddbbc
Results of Student 2:
Item One: Mark: 8, Wrong: 1 4 5 6 7 8
Item Two: Mark: 10, Wrong: 2 4 5 7 8
Item Three: Mark: 20, Wrong:
Item Four: Mark: 4, Wrong: 1 4 5
Item Five: Mark: 2, Wrong: 1 2 4 5
Input the students' answers (Press Enter at the beginning of the next line to quit):
2 students' data treated till now.
如果还有任何需要更正或改进的地方,扔请各位不吝指教,谢谢! |
|