标题:读取有问题,挑题目的算法不随机,请大家帮忙看看
只看楼主
孤鹄
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2018-1-10
结帖率:100%
已结贴  问题点数:20 回复次数:1 
读取有问题,挑题目的算法不随机,请大家帮忙看看
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct test)
struct test
{
    char que[81];
    char answer1[81];
    char answer2[81];
    char answer3[81];
    char answer4[81];
    char ture;
    struct test *next;
};
struct test *head;//全局变量储存第一个结构体的指针
int zts(int m)//将第m道题装入内存,输入-1时将返回总题数
{
    FILE *fp;
    struct test *p1=NULL;
    char ch,ch1[5];
    int i,n=0,wz,py;
    char temp1[81];
    char temp2[81];
    if((fp=fopen("test.txt","r"))==NULL)//只读方式打开文件
    {
        printf("文件打开失败请检查test.txt文件。\n");
        exit(0);
    }
    do
    {
        py=1;
        ch=fgetc(fp);
        for(i=0;ch!='\n'&&ch!=-1;i++)//循环判断回车符个数
        {
            py++;
            ch=fgetc(fp);
        }
        n++;                         //储存行数的变量
        if(n==m)//如果当前行数与传入参数相等
        {
            if(p1==NULL)//判断是否是第一次开辟内存空间
            {
                p1=(struct test *)malloc(LEN);
                head=p1;
                p1->next=NULL;
            }
            else
            {
                p1->next=p1;
                p1=(struct test *)malloc(LEN);
                p1->next=NULL;
            }
            wz=ftell(fp);//记录指针当前位置
            fseek(fp,(wz-py-1),0);//调整指针位置到行首
            fscanf(fp,"%s%s%s%s%s%s%s%s",temp1,temp2,&p1->que,&p1->answer1,&p1->answer2,&p1->answer3,&p1->answer4,&ch1);
            fseek(fp,wz+1,0);//调整指针位置到行末
            p1->ture=ch1[0];
            break;//将数据装入内存后跳出循环
        }
    }while(!feof(fp));//文件结束跳出循环                                   
    fclose(fp);
    return n;//返回题目总数n
}
void exe1()
{
  
    int n,m=0,tm,i,t=0,f=0;
    char ans[2];
    struct test *p1;
    system("cls");
    printf("请输入要答题数:");
    scanf("%d",&n);
    tm=zts(-1);
    if(0>n||n>tm)
    {
        printf("答题数应小于总数!");
        exe1();
    }
    for(i=0;i<n;i++)//n为用户要答题数
    {
        do
        {
            m=rand();//置随机数
        }while(!(0<m&&m<=tm));//判断产生的随机数是否符合要求
            zts(m);
        p1=head;//将zts开辟的结构体首地址赋给p1
        printf("\n第%d题:\n",i+1);
        printf("%s\n%s\n%s\n%s\n%s\n请输入答案:",p1->que,p1->answer1,p1->answer2,p1->answer3,p1->answer4);
        scanf("%s",&ans);
        if(toupper(ans[0])==p1->ture)//判断用户输入答案是否正确
        {
            printf("恭喜你答对了!\n\n");
            t++;//回答正确统计
        }
        else
        {
            printf("对不起,你答错了。。。\n正确答案是:%c\n\n",p1->ture);
            f++;//回答错误统计
        }
    }
    p1=head;
    do
    {
        head=p1->next;
        free(p1);
        p1=head;
    }while(p1!=NULL);//释放结构体占用内存
    printf("*******************************\n");
    printf("\n已完成测试!\n共做%d道题:%d错误,%d正确。\n正确率:%5.2f%%\n\n",n,f,t,(float)t/n*100);
    printf("*******************************\n");
    printf("\n\n1.继续答题;2.添加题目;3.返回主菜单;\n请选择:");
    i=0;
    scanf("%d",&i);
    if(i==1)
        exe1();//继续答题
    else
        if(i==2)
            exe2();//添加题目
        else
            main();//返回主菜单
}
void exe2()//实现用户自主输入题目
{
    void main();
    system("cls");
    int n;
    FILE *fp;
    if((fp=fopen("test.txt","a"))==NULL)
    {
        printf("文件打开失败请检查test.txt文件。\n");
        exit(0);
    }
    struct test *p1=NULL;
    p1=(struct test *)malloc(LEN);//开辟内存空间
    do
    {
        printf("请输入题目:");
        scanf("%s",&p1->que);
    }while(p1->que[0]=='\0');
    do
    {
        printf("请输入选项A:");
        scanf("%s",&p1->answer1);
    }while(p1->answer1=='\0');
    do
    {
        printf("请输入选项B:");
        scanf("%s",&p1->answer2);
    }while(p1->answer2=='\0');
    do
    {
        printf("请输入选项C:");
        scanf("%s",&p1->answer3);
    }while(p1->answer3=='\0');
    do
    {
        printf("请输入选项D:");
        scanf("%s",&p1->answer4);
    }while(p1->answer4=='\0');
    printf("请输入答案:");
    do
    {
        scanf("%c",&p1->ture);
        p1->ture=toupper(p1->ture);
    }while(p1->ture!='A'&&p1->ture!='B'&&p1->ture!='C'&&p1->ture!='D');
    printf("\n确认输入题目?\n1.是。 2.否。\n");
    scanf("%d",&n);
    if(n==1)
    {
        fputc('\n',fp);
        fputs(p1->que,fp);
        fputc(' ',fp);
        fputs("A.",fp);
        fputs(p1->answer1,fp);
        fputc(' ',fp);
        fputs("B.",fp);
        fputs(p1->answer2,fp);
        fputc(' ',fp);
        fputs("C.",fp);
        fputs(p1->answer3,fp);
        fputc(' ',fp);
        fputs("D.",fp);
        fputs(p1->answer4,fp);
        fputc(' ',fp);
        fputc(p1->ture,fp);
        fclose(fp);
        printf("保存成功!");
        system("cls");
        free(p1);
        printf("是否继续添加?\n1.继续添加  2.返回主菜单\n");
        scanf("%d",&n);
        if(n==1)
            exe2();
        else
            main();
    }
    else
    {
        free(p1);
        exe2();
    }
}
void main()
{
    system("cls");
    int n;
    printf("********************************\n");
    printf("欢迎使用单项选择题标准化考试系统\n");
    printf("********************************\n");
    printf("\t1.开始答题。\n");
    printf("\t2.添加试题。\n");
    printf("\t3.退出系统。\n");
    printf("\n请选择:");
    scanf("%d",&n);
    switch(n)
    {
        case 1:exe1();break;
        case 2:exe2();break;
        case 3:printf("\ngoodbye!\n");
    }while(n!=3);
}
test.zip (3.26 KB)
搜索更多相关主题的帖子: test 输入 printf scanf while 
2018-01-12 01:01
wp231957
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:422
帖 子:13681
专家分:53296
注 册:2012-10-18
得分:14 
没有看到对随机数种子进行初始化的代码

DO IT YOURSELF !
2018-01-12 07:39



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




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

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