免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1942 | 回复: 4
打印 上一主题 下一主题

[C] 改客观题给分的程序有错,请教! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-31 23:43 |只看该作者 |倒序浏览
10可用积分
我要改客观题(ABCD单选那种),情况如下:
一共有五个大题,前三题都是十个小题(题号1-10),后两题都是五小题(题号1-5),所有小题都是2分每个。
要求:
一次性输入学生所有的答题结果,并记录入文件,输出结果是学生的每个大题的得分,其中哪几个题错误,这些也都写入前面的那个文件。

我做了一个程序,如下:

/* 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";   /*所有1-40题的正确答案*/
    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;              /*记录已处理完学生资料数目cnt和为每一题算分数的变量sum_1-5*/
    int 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;                                          /*每一次都将sum_1-5的满分还原以便之后扣减*/
        fprintf(fwrt, "\nAnswers of student %d: %s", cnt+1, ans); /*记录下学生答题的输入序号以及答题情况*/
        for(j = 0;  j < LEN-1; j++){                             /*将学生答题与答案一个个比较*/
            if(ans[j] != keys[j]){
                if((j+1) > 0 && (j+1) <= 10){               
                    sum_1 -= STEP;
                    wrng_1[j] = j+1;
                }
                if((j+1) > 10 && (j+1) <= 20){
                    sum_2 -= STEP;
                    wrng_2[j] = (j+1) - SIZE_A;     /*因为每一大题的题号是1-10,所以做了这样的处理,下同*/
                }
                if((j+1) > 20 && (j+1) <= 30){
                    sum_3 -= STEP;
                    wrng_3[j] = (j+1) - (SIZE_A * 2);
                }
                if((j+1) > 30 && (j+1) <= 35){
                    sum_4 -= STEP;
                    wrng_4[j] = (j+1) - (SIZE_A * 3);
                }
                if((j+1) > 35 && (j+1) <= 40){
                    sum_5 -= STEP;
                    wrng_5[j] = (j+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(j = 0; j < SIZE_A; j++)
            if(wrng_1[j] != 0)
                fprintf(fwrt, "%4d", wrng_1[j]);
        fprintf(fwrt, "\nItem Two: Mark: %d, Wrong: ",sum_2);
        for(j = 0; j < SIZE_A; j++)
            if(wrng_2[j] != 0)
                fprintf(fwrt, "%4d", wrng_2[j]);
        fprintf(fwrt, "\nItem Three: Mark: %d, Wrong: ",sum_3);
        for(j = 0; j < SIZE_A; j++)
            if(wrng_3[j] != 0)
                fprintf(fwrt, "%4d", wrng_3[j]);
        fprintf(fwrt, "\nItem Four: Mark: %d, Wrong: ",sum_4);
        for(j = 0; j < SIZE_B; j++)
            if(wrng_4[j] != 0)
                fprintf(fwrt, "%4d", wrng_4[j]);
        fprintf(fwrt, "\nItem Five: Mark: %d, Wrong: ",sum_5);
        for(j = 0; j < SIZE_B; j++)
            if(wrng_5[j] != 0)
                fprintf(fwrt, "%4d", wrng_5[j]);
        fprintf(fwrt,"\nInput the students' answers (Press Enter at the beginning "
                 "of the next line to quit):\n");
        cnt++;
    }
     
    fprintf(fwrt, "\n%d students' data treated till now.",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:    3   4   5   5   9  10   9  10                 /*这里有题号的重复*/
Item Two: Mark: 10, Wrong:    1   3
Item Three: Mark: 16, Wrong:                                                        /*这里有错题和扣分却无错题题号记录,下同*/
Item Four: Mark: 2, Wrong:
Item Five: Mark: 4, Wrong:
Input the students' answers (Press Enter at the beginning of the next line to quit):

Answers of student 2: cadbaabdadftttftfttffttttfftttadcaaddbbc
Results of Student 2:                             /*学生2的结果中也有同学生1一样的错误出现*/

Item One: Mark: 8, Wrong:    1   4   5   4   7   8   7   8   9  10
Item Two: Mark: 10, Wrong:    1   2   3
Item Three: Mark: 20, Wrong:
Item Four: Mark: 4, Wrong:
Item Five: Mark: 2, Wrong:
Input the students' answers (Press Enter at the beginning of the next line to quit):
2 students' data treated till now.

结果中一看就知道有问题了,似乎给分和答错的题目题号的记录都不正确,我也看不出有什么地方出了问题,请各位指教,谢谢!

论坛徽章:
0
2 [报告]
发表于 2009-04-01 00:33 |只看该作者

回复 #1 mcmay 的帖子

我经过检查发现了一个错误,更正如下:

/* 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;
    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;
        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; /*这里错误题号记录数组wrng_2下标应保持在1-10之内,下同*/
                }
                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   3   4   5   6   7   8   9  10     /*第二轮输出结果很明显错误重重,却不知为何*/
Item Two: Mark: 10, Wrong:    2   4   5   7   8   9  10
Item Three: Mark: 20, Wrong:    3   5
Item Four: Mark: 4, Wrong:    1   2   3   4   5
Item Five: Mark: 2, Wrong:    1   2   3   4   5
Input the students' answers (Press Enter at the beginning of the next line to quit):
2 students' data treated till now.

请各位继续不吝赐教!

论坛徽章:
0
3 [报告]
发表于 2009-04-01 01:14 |只看该作者

我又找出一些问题,已更正,程序运行结果正常了。

下面是代码更正:
/* 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.

如果还有任何需要更正或改进的地方,扔请各位不吝指教,谢谢!

论坛徽章:
0
4 [报告]
发表于 2009-04-01 08:31 |只看该作者
贴代码的时候请用代码模式。
  1. test
复制代码

论坛徽章:
1
射手座
日期:2013-08-21 13:11:46
5 [报告]
发表于 2009-04-01 10:34 |只看该作者
感觉是简单的问题复杂化.
char keys[LEN]是正确答案,
char ans[LEN] 是做出来的答案,
bit state[LEN] 就是答案的状态,然后再统计一下不就行了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP