zoukankan      html  css  js  c++  java
  • 辅助判卷程序项目的扩展自动出题

    既完成了主模块---计算题目的设计后,我就开始了自动出题程序的设计,这个程序的思路比较简单,并不是很完美

    下面是程序截图和生成的算式

    题目中最多包含一对括号,此程序唯一的遗憾就是有时候计算结果会很大例如7736/4这样的结果

    下面是这个程序的全部代码

    #include <iostream>
    using namespace std;
    #include <string>
    #include <fstream>
    #include<time.h>
    #include<stdlib.h>
    
    #define random(x) (rand()%x)
    
    bool isoperator(char c){
        if(c=='+'||c=='-'||c=='*'||c=='/')
            return true;
        return false;
    }
    
    int main(int argc, char** argv) {
        srand((int)time(0));
        string equation;
        char temp[100];
        ofstream outf("equation.txt");
        int i=0;
        cout<<"输入生成算式的数量:";
        cin>>i;
        while(i--){
            equation.clear();
            if(i%2){                                            //分数运算 
                char lastop='+';                                //上一个运算符 
                int num=random(7)+4;                            //算式包含的操作数个数-1 
                sprintf(temp,"%d",random(20)+1);                 //第一个操作数 
                equation.append(temp);
                while(num--){
                    int b;                        
                    if(lastop=='/')                            //防止连续除法的出现 
                        b=random(2);
                    else
                        b=random(12);
                    switch(b){
                    case 0:
                    case 4:
                    case 8:
                        lastop=temp[0]='+';
                        break;
                    case 1:
                    case 5:
                    case 9:
                        lastop=temp[0]='-';
                        break;
                    case 2:
                    case 6:
                    case 10:
                        lastop=temp[0]='*';
                        break;
                    case 3:
                    case 7:
                    case 11:
                        lastop=temp[0]='/';
                        break;
                    } 
                    temp[1]=0;
                    equation.append(temp);
                    sprintf(temp,"%d",random(20)+1);                 
                    equation.append(temp);
                }
                int k,a=0;
                for(int j=0;j<equation.size();j++){                    //添加括号 
                    if((equation[j]=='+'||equation[j]=='-')&&a==0){
                        a++;
                    } 
                    else if((equation[j]=='+'||equation[j]=='-')&&a==1){
                        k=j-1;                                            //添加左括号 
                        while(!isoperator(equation[k-1])&&k!=0) k--;
                        if(equation[k-1]=='/'){
                            k--;
                            while(!isoperator(equation[k-1])&&k!=0) k--;
                        }
                        equation.insert(k,"(");
                        
                        k=j+2;                                            //添加右括号 
                        while(!isoperator(equation[k+1])&&k!=equation.size()-1) k++;
                        equation.insert(k+1,")");
                        
                        break;
                    }
                }
                //cout<<equation<<endl;
            }
            else{                                                        //小数运算 
                char lastop='+';                                //上一个运算符 
                int num=random(7)+4;                            //算式包含的操作数个数-1 
                int temp1=random(200)+1;
                sprintf(temp,"%g",temp1/10.0);                 //第一个操作数 
                equation.append(temp);
                while(num--){
                    int b;                        
                    if(lastop=='/')                            //防止连续除法的出现 
                        b=random(2);
                    else
                        b=random(12);
                    switch(b){
                    case 0:
                    case 4:
                    case 8:
                        lastop=temp[0]='+';
                        break;
                    case 1:
                    case 5:
                    case 9:
                        lastop=temp[0]='-';
                        break;
                    case 2:
                    case 6:
                    case 10:
                        lastop=temp[0]='*';
                        break;
                    case 3:
                    case 7:
                    case 11:
                        lastop=temp[0]='/';
                        break;
                    } 
                    temp[1]=0;
                    equation.append(temp);
                    int temp2=random(200)+1;
                    if(equation[equation.size()-1]=='/'&&(temp1%temp2)!=0){
                        temp2=temp1/5+1;
                        while(temp1%temp2){
                            temp2++;
                        }
                    }
                    temp1=temp2;
                    sprintf(temp,"%g",temp2/10.0);                
                    equation.append(temp);
                }
                //cout<<equation<<endl;
            }
            outf<<equation<<endl; 
        }
        cout<<"生成算式成功"<<endl; 
        outf.close();
        return 0;
    }
  • 相关阅读:
    分治
    #include<algorithm>
    c++标准模板库的使用
    mysql_day03
    mysql_day02
    mysql_day01
    mongodb的安装
    迭代器和生成器简单介绍
    File文件操作
    数据类型
  • 原文地址:https://www.cnblogs.com/chengyu404/p/5295935.html
Copyright © 2011-2022 走看看