| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 360 人关注过本帖
标题:急需解答???
只看楼主 加入收藏
志愿军
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-5-9
结帖率:100%
收藏
 问题点数:0 回复次数:0 
急需解答???
#include<stdio.h>
#include<stdlib.h>/* 应用动态存储分配函数 */
#include<time.h>
# define LEN sizeof(struct question)
struct question
{
    char ask[200];/* 选择题题目 */
    char answer[4][80];/* 选择题选项,每个答案的长度 */
    int right;/* 正确答案 */
    struct question *next;/* next是指针类型的成员, */
    /* 它指向struct question类型数据(即next所在的结构体类型) */
    /* 使用指针类型成员存放下一个结点的地址 */
    /* 此步骤实现了问题的连续输入输入 */
};
    int menu(void);/* 声明菜单选择函数 */
    struct question *seek(struct question *seek,long len,long max);/* 寻找读取答案的位置 */
    struct question *insert(struct question *fst,const struct question *ad);/* 插入试题 */
    void getquestion(struct question *s);/* 获取问题,选项,以及正确答案 */
    void savefile(const struct question *a,FILE *sf);/* 保存最佳答案在文件中 */
    struct question *loadfile(struct question *b,FILE *lf);/* 读取题目,将题目添加到列表中 */
    int getanswer(void);/* 得到答案 */
    int getyouranswer(void);/* 得到考生答案 */
    void explainquestion(const struct question *q,int n);/* 统计答对题目数,显示得分 */
/* 选择菜单 */
int menu(void)
{
    int v;
    printf("1—add question\n2—answer question\n3—exit\n");
    scanf("%d",&v);
    return v;
}
/* seek函数确定一个读取答案的位置,len代表要读取的答案数,max代表列表的长度 */
struct question *seek(struct question *seek,long len,long max)
{
    int i;
    srand(time(NULL));
    while(i=rand()%max+len<max);/* 随机选取一个读题目的位置 */
    while(i--)
        seek=seek->next;/* 找到指定的位置 */
    return seek;
}
/* 向列表中插入试题 */
struct question *insert(struct question *fst,const struct question *ad)
{
    struct question *newptr=(struct question *)malloc(LEN);/* 分配新的内存空间 */
    if (newptr==NULL)
        exit(0);
    *newptr=*ad;
    newptr->next=fst;
    return newptr;
}
/* 获取问题,选项,以及正确答案 */
void getquestion(struct question *s)
{
    int i=0;
    printf("Please input the question:\n");
    fflush(stdin);
    gets(s->ask);/* 指向结构体中的成员 */
    while(i<4)
    {
        printf("Please input option%c`s answer:\n",i+'A');
        scanf("%s",s->answer[i++]);
    }
    s->right=getanswer();
}
/* 试题保存*/
void savefile(const struct question *a,FILE *sf)/* 使用const说明成员函数 */
{
    fclose(sf);
    if((sf=fopen("kstm.dat","w"))==NULL)/* 以写的方式重新打开文件 */
    return;
    while(a)
    {
    fwrite(a,sizeof(struct question),1,sf);
    a=a->next;
    }
}
/* 从文件中读取题目,将题目添加到列表中 */
struct question *loadfile(struct question *b,FILE *lf)
{
    struct question temp;
    while (fread(&temp,sizeof(struct question),1,lf))
        b=insert(b,&temp);
    return b;
}

/* 统计答对题目数,显示得分 */
void explainquestion(const struct question *que,int n)
{
    int i=0,t=0;
    char result[1001],*p=result;
    for(i=0;t<n;que=que->next,t++)
    {
    printf("%s\nA.%s\nB.%s\nC.%s\nD.%s\n\n",que->ask,que->answer[0],que->answer[1],que->answer[2],que->answer[3]);
    if((*p=que->right)==(*(p+1)=getyouranswer()))
    ++i;
    p+=2;
    }
    *p='\0';
    printf("\n%-20s%-20s%s\n","corret answer","your answer","result");
    for(p=result;*p!='\0';p+=2)
        printf("%-20c%-20c%s\n",*p,*(p+1),*p==*(p+1)?"corret":"wrong");
    printf("\nyou answer%dquestion,answer right%dquestion,score:%.2f\n\n",n,i,(float)(i*100.00/n));
}
/* 得到选择题的答案 */
int getanswer(void)
{   
    static int i=1;
    int c=0;/* 必须进行初始化,避免出现偶然性的错误 */
    while(c<'A'||c>'D')/* 确保输入的答案是ABCD中的一个 */
    {
    printf("Please input question%d`s correct answer:",i);
    scanf("%c",&c);
    printf("\n");
    if(c>96)
        c=c-32;/* 实现小写向大写的转换 */
    }i++;
    return c;
}
int getyouranswer(void)
{   
    int c=0;/* 必须进行初始化,避免出现偶然性的错误 */
    while(c<'A'||c>'D')/* 确保输入的答案是ABCD中的一个 */
    {
    printf("Please input your answer:");
    scanf("%c",&c);
    if(c>96)
        c=c-32;/* 实现小写向大写的转换*/
    }
    return c;
}
main()
{
    struct question *start=NULL,temp;
    long int choice,line=0,c;
    FILE *fp=fopen("kstm.dat","a+");/* 用'a+'方式打开文件名为'kstm.dat'文件,可添可读 */
    start=loadfile(start,fp);
    printf("*****please select the number you want to operate*****\n");
    while((choice=menu())!=3)/* 如果考生不选3-退出 */
       if(choice==1)
        {
            getquestion(&temp);
            start=insert(start,&temp);
            ++line;/* 统计列表的长度 */
        }
        else if(choice==2)
        {
            printf("Please input the question quantity you want to answer:");
            scanf("%d",&c);
            start=seek(start,c,line);
            explainquestion(start,c);
    }
    savefile(start,fp);
    fclose(fp);
    return 0;
}



里面的:
/* seek函数确定一个读取答案的位置,len代表要读取的答案数,max代表列表的长度 */
struct question *seek(struct question *seek,long len,long max)
{
    int i;
    srand(time(NULL));
    while(i=rand()%max+len<max);/* 随机选取一个读题目的位置 */
    while(i--)
        seek=seek->next;/* 找到指定的位置 */
    return seek;


是如何随机读取题目的,这个函数是如何运行的,我看不明白,望各位大侠指点~
搜索更多相关主题的帖子: 解答 
2010-09-15 10:30
快速回复:急需解答???
数据加载中...
 
   



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

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