IT142546355 2015-11-03 06:29 采纳率: 50%
浏览 2203
已采纳

求大神帮我把这机构化的C++程序改成C++面向对象的程序,有酬谢(单项选择题标准化考试系统)

 #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—添加选择题\n2—回答选择题\n3—退出\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("请输入选择题题目:\n");
    scanf("%s",s->ask);//指向结构体中的成员//
    while(i<4)
    {
        printf("请输入选项%c的答案:\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","标准答案","你的答案","评价");
    for(p=result;*p!='\0';p+=2)
        printf("%-20c%-20c%s\n",*p,*(p+1),*p==*(p+1)?"正确":"错误");
    printf("\n你回答了%d道题,答对%d道题目,得分:%.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("请输第%d题的正确答案:",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("请输入你的答案:");
    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("           *****欢迎使用此考试系统,请输入你要执行的步骤的编号*****\n");
    while((choice=menu())!=3)//如果考生不选3-退出//
        if(choice==1)
        {
            getquestion(&temp);
            start=insert(start,&temp);
            ++line;//统计列表的长度//
        }
        else if(choice==2)
        {
            printf("请输入要回答的问题数量:");
            scanf("%d",&c);
            start=seek(start,c,line);
            explainquestion(start,c);
    }
    savefile(start,fp);
    fclose(fp);
    return 0;
}
  • 写回答

4条回答 默认 最新

  • Z2269811161 2015-11-04 19:31
    关注

    大体的实现我没改你的

    #include
    #include//应用动态存储分配函数//
    #include

    define LEN sizeof(struct question)

    struct question
    {
    char ask[200];//选择题题目//
    char answer[4][80];//选择题选项,每个答案的长度//
    int right;//正确答案//
    struct question *next;//next是指针类型的成员,
    //它指向struct question类型数据(即next所在的结构体类型)
    //使用指针类型成员存放下一个结点的地址
    //此步骤实现了问题的连续输入输入
    };

    class Question
    {
    private:
    struct question *head;
    public:
    Question():head(NULL){}
    int menu(void);//声明菜单选择函数
    struct question *seek(long len,long max);//寻找读取答案的位置
    struct question *insert(const struct question *ad);//插入试题
    void getquestion(struct question *s);//获取问题,选项,以及正确答案
    void savefile(FILE *sf);//保存最佳答案在文件中//
    void loadfile(FILE *lf);//读取题目,将题目添加到列表中
    int getanswer(void);//得到答案
    int getyouranswer(void);//得到考生答案
    void explainquestion(int n);//统计答对题目数,显示得分
    };

    //选择菜单//
    int Question::menu(void)
    {
    int v;
    printf("1—添加选择题\n2—回答选择题\n3—退出\n");
    scanf("%d",&v);
    return v;
    }
    //seek函数确定一个读取答案的位置,len代表要读取的答案数,max代表列表的长度//
    struct question * Question::seek(long len,long max)
    {
    int i;
    struct question* seek=head;
    srand(time(NULL));
    while(i=rand()%max+len while(i--)
    seek=seek->next;//找到指定的位置//
    return seek;
    }
    //向列表中插入试题,使用头插法//
    struct question * Question::insert(const struct question *ad)
    {
    struct question *newptr=(struct question *)malloc(LEN);//分配新的内存空间//
    if (newptr==NULL)
    exit(0);
    *newptr=*ad;
    newptr->next=head;
    head=newptr;
    return newptr;
    }
    //获取问题,选项,以及正确答案//
    void Question::getquestion(struct question *s)
    {
    int i=0;
    printf("请输入选择题题目:\n");
    scanf("%s",s->ask);//指向结构体中的成员//
    while(i {
    printf("请输入选项%c的答案:",i+'A');
    scanf("%s",s->answer[i++]);
    }
    s->right=getanswer();
    }
    //试题保存//
    void Question::savefile(FILE *sf)//使用const说明成员函数//
    {
    const struct question *a=head;
    fclose(sf);
    if((sf=fopen("kstm.dat","w"))==NULL)//以写的方式重新打开文件//
    return;
    while(a)
    {
    fwrite(a,sizeof(struct question),1,sf);
    a=a->next;
    }
    }
    //从文件中读取题目,将题目添加到列表中//
    void Question::loadfile(FILE *lf)
    {
    struct question temp;
    while (fread(&temp,sizeof(struct question),1,lf))
    insert(&temp);
    }

    //统计答对题目数,显示得分//
    void Question::explainquestion(int n)
    {
    int i=0;
    struct question que=head;
    char result[1001],*p=result;
    for(;que!=NULL;que=que->next)
    {
    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","标准答案","你的答案","评价");
    for(p=result;*p!='\0';p+=2)
    printf("%-20c%-20c%s\n",*p,
    (p+1),*p==*(p+1)?"正确":"错误");
    printf("\n你回答了%d道题,答对%d道题目,得分:%.2f\n\n",n,i,(float)(i*100.00/n));
    }
    //得到选择题的答案//
    int Question::getanswer(void)
    {
    static int i=1;
    int c=0;//必须进行初始化,避免出现偶然性的错误//
    while(c<'A'||c>'D')//确保输入的答案是ABCD中的一个//
    {
    printf("请输第%d题的正确答案:",i);
    scanf("%c",&c);
    printf("\n");
    if(c>96)
    c=c-32;//实现小写向大写的转换//
    }i++;
    return c;
    }
    int Question::getyouranswer(void)
    {
    int c=0;//必须进行初始化,避免出现偶然性的错误//
    while(c<'A'||c>'D')//确保输入的答案是ABCD中的一个//
    {
    printf("请输入你的答案:");
    scanf("%c",&c);
    if(c>96)
    c=c-32;//实现小写向大写的转换//
    }
    return c;
    }
    main()
    {
    Question start;
    struct question temp;
    long int choice,line=0,c;
    FILE fp=fopen("kstm.dat","a+");//用'a+'方式打开文件名为'kstm.dat'文件,可添可读//
    start.loadfile(fp);
    printf(" *
    ***欢迎使用此考试系统,请输入你要执行的步骤的编号*****\n");
    while((choice=start.menu())!=3)//如果考生不选3-退出//
    if(choice==1)
    {
    start.getquestion(&temp);
    start.insert(&temp);
    ++line;//统计列表的长度//
    }
    else if(choice==2)
    {
    printf("请输入要回答的问题数量:");
    scanf("%d",&c);
    start.seek(c,line);
    start.explainquestion(c);
    }
    start.savefile(fp);
    fclose(fp);
    return 0;
    }

    我不得不说你的代码写得不太好啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作