zoukankan      html  css  js  c++  java
  • 程序实现自动生成30道四则运算题目(2)

    继上一次的任务后,本周老师又给自动出题系统提出了新的要求,我感觉我跟二柱子
    一样可怜,本次老师的要求是
    1.题目避免重复
    2.题目可以定制(数量/打印方式)
    3.可以控制一下参数
    (1)是否有乘除法
    (2)是否有括号(最多可以支持十个数参与运算)
    (3)数值范围
    (4)加减有无负数
    (5)除法有无余数

    针对本次任务,我们逐个要求,逐个分析
    对于题目避免重复可以用建立字符串数组,进行字符串之间相互比较来解决,另外题目
    可以定制通过随机数生成题目数并带入循环解决,同时打印方式可以用文件输入输出流
    把数据存到文件里。对于是否有乘除法,可以把+ - * /存到数组里,随机调用
    数值范围可以用随机数进行范围控制,本题的难点在是否能生成括号,
    括号是用来提升优先级,对于这个问题我们可以用循环调用来实现,具体含义是
    四则运算表达式可以看成 表达式1+符号+表达式2的模式,在进入循环开表达式
    可以随机选择放在表达式1或表达式2上,也可以决定有无括号,这样用随机生成
    的运算数的个数决定循环的次数,加减有无负数和除法有无余数,这里只对两个
    运算数的表达式进行了操作。
    下面是本次实验源代码:

    //随机生成四则运算表达式 杨超群 2016.3.12
    #include<iostream>
    #include<string>
    #include<time.h>
    #include <stdio.h>
    #include<fstream>
    using namespace std;
    string str1[4]={"+","-","*","/"};
    int num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,n,m,a,b;
    char str2[25];
    char str3[25];
    string str4[100];
    void Input(int n,int p)
    {
        m=n;
        string str;
        ofstream outfile;
        outfile.open("a.txt",ios::app);                  
        if(!outfile)
        {
            cerr<<"OPEN ERROR!"<<endl;
            exit(0);
        }
        num1=rand()%100+1;
        num2=rand()%100+1;
        num3=rand()%4;
        num8=num1;
        num9=num2;
        num10=num3;
        itoa(num1,str2,10);
        itoa(num2,str3,10);
        str=str2+str1[num3]+str3;
        n=n-2;
        while(n!=0)                 //当n不等于0时,循环生成str,即表达式+符号+表达式的形式
        {    
            num4=rand()%2;
            if(num4==0)             //上一个str放在符号的左边
            {
                num5=rand()%2;
                if(num5==0)            //上一个str不加括号
                {
                    num3=rand()%4;
                    num1=rand()%100+1;
                    itoa(num1,str2,10);
                    str=str+str1[num3]+str2;
                }
                else                           //上一个str加括号
                {
                    num3=rand()%4;
                    num1=rand()%100+1;
                    itoa(num1,str2,10);
                    str="("+str+")"+str1[num3]+str2;
                }
            }
            else                              //上一个str放在符号的右边
            {
                num5=rand()%2;
                if(num5==0)                    // 上一个str不加括号
                {
                    num3=rand()%4;
                    num1=rand()%100+1;
                    itoa(num1,str2,10);
                    str=str2+str1[num3]+str;
                }
                else                         //上一个str加括号
                {
                    num3=rand()%4;
                    num1=rand()%100+1;
                    itoa(num1,str2,10);
                    str=str2+str1[num3]+"("+str+")";
                }
            }
            n--;
        }
        str4[p]=str;                         //把str存入字符串数组str4中
        for(int i=0;i<p;i++)                 //查询四则运算式是否有重复
            if(str4[i]==str4[p])
                 Input(m,p);
        cout<<str4[p]<<"                       ";
        outfile<<str4[p]<<endl;
        if(m==2)
        {
            if(num10==0)                     //加法有无负数
            {
                a=num8+num9;
                if(a>0)
                    cout<<"结果"<<a<<"为正";
                else if(a==0)
                    cout<<"结果"<<a<<"为零";
                else if(a<0)
                    cout<<"结果"<<a<<"为负";
            }
            else if(num10==1)                    //减法有无负数
            {
                a=num8-num9;
                if(a>0)
                    cout<<"结果"<<a<<"为正";
                else if(a==0)
                    cout<<"结果"<<a<<"为零";
                else if(a<0)
                    cout<<"结果"<<a<<"为负";
            }
            else if(num10==2)
            {
                a=num8*num9;
                if(a>0)
                    cout<<"结果"<<a<<"为正";
                else if(a==0)
                    cout<<"结果"<<a<<"为零";
                else if(a<0)
                    cout<<"结果"<<a<<"为负";
            }
            else if(num10==3)                          //除法有无余数
            {
                b=num8%num9;
                if(b==0)
                    cout<<"余数为0";
                else
                    cout<<"余数不为0";
            }
        }
        cout<<endl;
    }
    void changeNum(int n)
    {
        for(int i=0;i<n;i++)                   //随机生成的题目
        {
            num6=rand()%9+2;
            switch(num6)
            {
            case 2:Input(2,i);break;
            case 3:Input(3,i);break;
            case 4:Input(4,i);break;
            case 5:Input(5,i);break;
            case 6:Input(6,i);break;
            case 7:Input(7,i);break;
            case 8:Input(8,i);break;
            case 9:Input(9,i);break;
            case 10:Input(10,i);break;    
            }
        }
    }
    void main()
    {
        ofstream outfile1;
        outfile1.open("a.txt");
        if(!outfile1)
        {
            cerr<<"OPEN ERROR!"<<endl;
            exit(0);
        }
        srand((unsigned)time(NULL));             //随机种子
        cout<<"输入你想做的题目数:";
        cin>>n;
        changeNum(n);
        cout<<"四则表达式没有重复"<<endl;
    }

    如下是不同题目数量的结果截图:

    题目数量为5

    相应的文件输出

    题目数量为30

    相应的文件输出

    总结:本次实验有点仓促,勉勉强强符合,有很多瑕疵的地方,真分数没有弄

    0值判断没有写,大于两个数的运算结果没有计算,以后逐个完善,希望做的更好。

  • 相关阅读:
    Effective Java 19 Use interfaces only to define types
    Effective Java 18 Prefer interfaces to abstract classes
    Effective Java 17 Design and document for inheritance or else prohibit it
    Effective Java 16 Favor composition over inheritance
    Effective Java 15 Minimize mutability
    Effective Java 14 In public classes, use accessor methods, not public fields
    Effective Java 13 Minimize the accessibility of classes and members
    Effective Java 12 Consider implementing Comparable
    sencha touch SortableList 的使用
    sencha touch dataview 中添加 button 等复杂布局并添加监听事件
  • 原文地址:https://www.cnblogs.com/linumy/p/5267870.html
Copyright © 2011-2022 走看看