标题:本人新手求解答。《有关单项选择系统》
取消只看楼主
qq520520
Rank: 1
等 级:新手上路
帖 子:3
专家分:3
注 册:2012-9-9
 问题点数:0 回复次数:1 
本人新手求解答。《有关单项选择系统》
运行到答案输入后出错
要用的文件在下楼
程序如下:




#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
struct single_problem
{
    char content[100];
    char selectA[100];
    char selectB[100];
    char selectC[100];
    char selectD[100];
}problem;
    FILE *fp_read;
    FILE *fp_write;   
    FILE *fp_answer;
    FILE *fp_check;
    int tishu=0;
    int zongtishu=95;
void main()
{
    void printquestion();
    void answer();
    int num=0;
    int count=0;


   
    printf("****************************************************\n");
    printf("**                                                **\n");
    printf("**                   1 题库维护                   **\n");
    printf("**                                                **\n");
    printf("**                     2 答题                     **\n");
    printf("**                                                **\n");
    printf("**                  3 查看全部题目                **\n");
    printf("**                                                **\n");
    printf("**                     4 退出                     **\n");
    printf("**                                                **\n");
    printf("**                     5 帮助                     **\n");
    printf("**                                                **\n");
    printf("****************************************************\n");

    if((fp_read=fopen("16-input_single.txt","r"))==NULL)
    {
        printf("文件16-input_single.txt打开错误!\n");
        return;
    }
    if((fp_answer=fopen("16-input-answer.txt","r"))==NULL)
    {
        printf("文件16-input-answer.txt打开错误!\n");
        return;
    }
    if((fp_check=fopen("答案.txt","w+"))==NULL)
    {
        printf("文件16-input-answer.txt打开错误!\n");
        return;
    }
    if((fp_write=fopen("题库.txt","w+"))==NULL)
    {
        printf("文件题库.txt打开错误!\n");
        return;
    }

    while(!feof(fp_read))
    {
        fscanf(fp_read,"%s",problem.content);
        fscanf(fp_read,"%s",problem.selectA);
        fscanf(fp_read,"%s",problem.selectB);
        fscanf(fp_read,"%s",problem.selectC);
        fscanf(fp_read,"%s",problem.selectD);
   
        fwrite(&problem,sizeof(struct single_problem),1,fp_write);

    }   
    while(!feof(fp_answer))
    {

        fscanf(fp_answer,"%s",problem.selectA);
        fscanf(fp_answer,"%s",problem.selectB);
        fscanf(fp_answer,"%s",problem.selectC);
        fscanf(fp_answer,"%s",problem.selectD);
   
        fwrite(&problem,sizeof(struct single_problem),1,fp_check);

    }   
    if((fp_read=fopen("题库.txt","r"))==NULL)
    {
        printf("题库.txt打开错误!\n");
        return;
    }

    if((fp_check=fopen("答案.txt","r"))==NULL)
    {
        printf("答案.txt打开错误!\n");
        return;
    }

    scanf("%d",&num);
    switch(num)
    {
    case 1:amend();break;
    case 2:answer();break;
    case 3:printquestion();break;
    case 4:;break;
    case 5:help();break;
    }
   
    fclose(fp_read);
    fclose(fp_write);
    fclose(fp_answer);
    fclose(fp_check);

   
}

void printquestion()
{
    while(!feof(fp_read))
    {
   
        fread(&problem,sizeof(struct single_problem),1,fp_read);
        printf("%s\n",problem.content);
        printf("%s\n",problem.selectA);
        printf("%s\n",problem.selectB);
        printf("%s\n",problem.selectC);
        printf("%s\n",problem.selectD);

    }
}


void answer()
{
    void checkanswer(int,char);
    struct temp_problem
    {
        char timu[100];
        char A[100];
        char B[100];
        char C[100];
        char D[100];
    }problem1[200]={"0","0","0","0","0"};
   
    int n;
    int i,a[200];
    char answer;
    int t;   
    printf("请选择要答题的数量。(1至%d)\n",zongtishu);
    scanf("%d",&n);
    while(n>zongtishu||n<1)
    {
        printf("请重新输入1至%d的数量。\n",zongtishu);
        scanf("%d",&n);
    }
    printf("\n");
    srand(time(NULL));
    for(i=0;i<n;i++)
               a[i]=rand()%(zongtishu-1+1)+1;
    for(i=0;i<n;i++)
    {   
        fseek(fp_write,a[i]*sizeof(struct single_problem),0);
        fread(&problem1[i],sizeof(struct single_problem),1,fp_write);
        printf("%s\n%s\n%s\n%s\n%s\n",problem1[i].timu,problem1[i].A,problem1[i].B,problem1[i].C,problem1[i].D);
        printf("请输入答案:");
        for(t=0;t<1;t++)
        {
            getchar();
            scanf("%c",&answer);
            if((answer<64&&answer>69)||(answer<96&&answer>101))
            {
                t=0;
                printf("请重新输入正确答案序号。\n");
            }
        }

        checkanswer(a[i],answer);

    }

}


void checkanswer(int b,char a)
{
    struct ch
    {
        char an[20];
    }che[200]={"\0"};

    int i=0,j=0,n;
    char bn[200]="\0";
    char anw[200][20]={"0"};

    rewind(fp_check);
    while(!feof(fp_check))
    {
        fscanf(fp_check,"%s",che[i].an[20]);
        fwrite(anw[i],sizeof(struct single_problem),1,fp_check);
        i++;
    }
    n=i;
    for(i=0;i<n;i++)
    {
        for(j=0;anw[i][j]!="\0";j++)
        {
            if(anw[i][j]==".")
                bn[i]=che[i].an[j+1];
            break;
        }
    }
//    for(i=0;i<n;i++)
//        printf("答案正确!\n");
    if(a==bn[b])
        printf("答案正确!\n");
    else printf("答案错误!\n");

}





[ 本帖最后由 qq520520 于 2012-9-9 20:11 编辑 ]
搜索更多相关主题的帖子: content include problem count 
2012-09-09 20:07
qq520520
Rank: 1
等 级:新手上路
帖 子:3
专家分:3
注 册:2012-9-9
得分:0 
123.rar (5.96 KB)

这需要用的文件
2012-09-09 20:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-381044-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.413448 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved