zoukankan      html  css  js  c++  java
  • 实现四则运算的功能添加版01

    这一次的程序设计,相比上一次难了许多,好多功能因为自身编程能力的不足而无法实现,但是在上个程序上添加了许多功能

    一、题目避免重复

    二、定制(数量/打印方式)

    三、控制参数

    1.是否有乘除法

    2.是否有负数

    3是否有分数,分数为真分数还是假分数

    4.是否有余数

    5.是否有小数,小数点后控制多少位

    6.是否有括号

    四、控制数字范围

    题目避免重复可以根据位数和里面的随机数相比较来控制

    是否有乘除、是否有负数、是否有分数、是否有小数、是否有括号可以根据一个函数来定义所有情况

    其中定义一个标志符号

    控制数字范围可以用算式%rand(max-min+1)+min来实现

    但是因为自己能力问题,括号、余数、还有小数并没有实现。

      1  #include<iostream>
      2  #include<cmath>
      3  #include<cstdlib>
      4  using namespace std;
      5  void cond(int p[5],int &z);
      6  void judge(int p[5],int &z);
      7   
      8  void main()
      9 {
     10     int num,min,max;
     11     cout<<"题目数量 请输入:";// 输入题目数量
     12     cin>>num;
     13     cout<<endl;
     14     cout<<" 数值范围:(n-m   n为最小值,m为最大值):"<<endl;
     15     cin>>min;
     16     cin>>max;
     17     int a,b,p[5],number;
     18     char c[4];                    //c[4]为+ - * /数组;p[5]为条件判断数组
     19     int d;
     20     int e,f, z,k;        //z为判断变量
     21     c[0] = '+';
     22     c[1] = '-';
     23     c[2] = '*';
     24     c[3] = '/';
     25     d = 0;
     26     cond(p,z);                //条件支持函数
     27     for (int i = 0; i < num; i++)
     28     {
     29         if (z == 00)                            //只有加减无负数
     30         {
     31             number=rand()%10;
     32             while(number==1||number==0)
     33             {
     34                 number=rand()%10;
     35             }
     36             a=rand()%(max-min+1)+min;
     37             cout<<a;
     38             for(int j=0;j<number-1;j++)
     39             {
     40                 d=rand()%2;
     41                 a=rand()%(max-min+1)+min;
     42                 cout<<c[d]<<a;
     43             }
     44             cout<<"="<<endl;
     45         }
     46         if(z == 01)                                //
     47         {
     48             int o;
     49             number=rand()%10;
     50             while(number==1||number==0)
     51             {
     52                 number=rand()%10;
     53             }
     54             o=rand()%2;
     55             d=rand()%2;
     56             a=rand()%(max-min+1)+min;
     57             if(o==0)
     58             {
     59                 cout<<a;
     60                 for(int j=0;j<number-1;j++)
     61                 {    
     62                     o=rand()%2;
     63                     if(o==0)
     64                     {
     65                         o=rand()%2;
     66                         d=rand()%2;
     67                         a=rand()%(max-min+1)+min;
     68                         cout<<c[d]<<a;
     69                     }
     70                     else
     71                     {
     72                         o=rand()%2;
     73                         d=rand()%2;
     74                         a=rand()%(max-min+1)+min;
     75                         cout<<c[d]<<-a;
     76                     }
     77                 }
     78             }
     79             else if(o==1)
     80             {
     81                 cout<<-a;
     82                 for(int j=0;j<number-1;j++)
     83                 {
     84                     if(o==0)
     85                     {
     86                         o=rand()%2;
     87                         d=rand()%2;
     88                         a=rand()%(max-min+1)+min;
     89                         cout<<c[d]<<a;
     90                     }
     91                     else
     92                     {
     93                         o=rand()%2;
     94                         d=rand()%2;
     95                         a=rand()%(max-min+1)+min;
     96                         cout<<c[d]<<-a;
     97                     }
     98                 }
     99             }
    100             cout<<"="<<endl;
    101         }
    102         if(z==100000)                            //
    103         {
    104             number=rand()%10;
    105             while(number==1||number==0)
    106             {
    107                 number=rand()%10;
    108             }
    109             d=rand()%4;
    110             a=rand()%(max-min+1)+min;
    111             cout<<a;
    112             for(int j=0;j<number-1;j++)
    113             {
    114                 d=rand()%4;
    115                 a=rand()%(max-min+1)+min;
    116                 cout<<c[d]<<a;
    117             }
    118             cout<<"="<<endl;
    119         }        
    120         if(z==100100)                            //
    121         {
    122             number=rand()%10;
    123             while(number==1||number==0)
    124             {
    125                 number=rand()%10;
    126             }
    127             d=rand()%4;
    128             a=rand()%(max-min+1)+min;
    129             cout<<a;
    130             for(int j=0;j<number-1;j++)
    131             {
    132                 int symbol=a;
    133                 d=rand()%4;
    134                 a=rand()%(max-min+1)+min;
    135                 while(d==3&&symbol>a)
    136                 {
    137                     a=rand()%(max-min+1)+min;
    138                 }
    139                 cout<<c[d]<<a;
    140             }
    141             cout<<"="<<endl;
    142         }
    143 
    144         if(z==100110)                            //
    145         {
    146             number=rand()%10;
    147             while(number==1||number==0)
    148             {
    149                 number=rand()%10;
    150             }
    151             d=rand()%4;
    152             a=rand()%(max-min+1)+min;
    153             cout<<a;
    154             for(int j=0;j<number-1;j++)
    155             {
    156                 int symbol=a;
    157                 d=rand()%4;
    158                 a=rand()%(max-min+1)+min;
    159                 cout<<c[d]<<a;
    160             }
    161             cout<<"="<<endl;
    162         }
    163         if(z==110100)                            //
    164         {
    165             int o;
    166             number=rand()%10;
    167             while(number==1||number==0)
    168             {
    169                 number=rand()%10;
    170             }
    171             o=rand()%2;
    172             d=rand()%4;
    173             a=rand()%(max-min+1)+min;
    174             if(o==0)
    175             {
    176                 cout<<a;
    177                 for(int j=0;j<number-1;j++)
    178                 {    
    179                     int symbol=a;
    180                     if(o==0)
    181                     {
    182                         o=rand()%2;
    183                         d=rand()%4;
    184                         a=rand()%(max-min+1)+min;
    185                         while(d==3&&symbol>a)
    186                         {
    187                             a=rand()%(max-min+1)+min;
    188                         }
    189                         cout<<c[d]<<a;
    190                     }
    191                     else
    192                     {
    193                         o=rand()%2;
    194                         d=rand()%4;
    195                         a=rand()%(max-min+1)+min;
    196                         while(d==3&&symbol>a)
    197                         {
    198                             a=rand()%(max-min+1)+min;
    199                         }
    200                         cout<<c[d]<<-a;
    201                     }
    202                 }
    203             }
    204             else if(o==1)
    205             {
    206                 cout<<-a;
    207                 for(int j=0;j<number-1;j++)
    208                 {
    209                     int symbol=a;
    210                     if(o==0)
    211                     {
    212                         o=rand()%2;
    213                         d=rand()%4;
    214                         a=rand()%(max-min+1)+min;
    215                         while(d==3&&symbol>a)
    216                         {
    217                             a=rand()%(max-min+1)+min;
    218                         }
    219                         cout<<c[d]<<a;
    220                     }
    221                     else
    222                     {
    223                         o=rand()%2;
    224                         d=rand()%4;
    225                         a=rand()%(max-min+1)+min;
    226                         while(d==3&&symbol>a)
    227                         {
    228                             a=rand()%(max-min+1)+min;
    229                         }
    230                         cout<<c[d]<<-a;
    231                     }
    232                 }
    233             }
    234             cout<<"="<<endl;
    235         }
    236         if(z==110110)                        //
    237         {
    238             int o;
    239             number=rand()%10;
    240             while(number==1||number==0)
    241             {
    242                 number=rand()%10;
    243             }
    244             o=rand()%2;
    245             d=rand()%2;
    246             a=rand()%(max-min+1)+min;
    247             if(o==0)
    248             {
    249                 cout<<a;
    250                 for(int j=0;j<number-1;j++)
    251                 {    
    252                     if(o==0)
    253                     {
    254                         o=rand()%2;
    255                         d=rand()%2;
    256                         a=rand()%(max-min+1)+min;
    257                         cout<<c[d]<<a;
    258                     }
    259                     else
    260                     {
    261                         o=rand()%2;
    262                         d=rand()%2;
    263                         a=rand()%(max-min+1)+min;
    264                         cout<<c[d]<<-a;
    265                     }
    266                 }
    267             }
    268             else if(o==1)
    269             {
    270                 cout<<-a;
    271                 for(int j=0;j<number-1;j++)
    272                 {
    273                     if(o==0)
    274                     {
    275                         o=rand()%2;
    276                         d=rand()%4;
    277                         a=rand()%(max-min+1)+min;
    278                         cout<<c[d]<<a;
    279                     }
    280                     else
    281                     {
    282                         o=rand()%2;
    283                         d=rand()%4;
    284                         a=rand()%(max-min+1)+min;
    285                         cout<<c[d]<<-a;
    286                     }
    287                 }
    288             }
    289             cout<<"="<<endl;
    290         }
    291         if(z==110000)
    292         {
    293             int o;
    294             number=rand()%10;
    295             while(number==1||number==0)
    296             {
    297                 number=rand()%10;
    298             }
    299             o=rand()%2;
    300             d=rand()%2;
    301             a=rand()%(max-min+1)+min;
    302             if(o==0)
    303             {
    304                 cout<<a;
    305                 for(int j=0;j<number-1;j++)
    306                 {    
    307                     if(o==0)
    308                     {
    309                         o=rand()%2;
    310                         d=rand()%2;
    311                         a=rand()%(max-min+1)+min;
    312                         cout<<c[d]<<a;
    313                     }
    314                     else
    315                     {
    316                         o=rand()%2;
    317                         d=rand()%2;
    318                         a=rand()%(max-min+1)+min;
    319                         cout<<c[d]<<-a;
    320                     }
    321                 }
    322             }
    323             else if(o==1)
    324             {
    325                 cout<<-a;
    326                 for(int j=0;j<number-1;j++)
    327                 {
    328                     if(o==0)
    329                     {
    330                         o=rand()%2;
    331                         d=rand()%4;
    332                         a=rand()%(max-min+1)+min;
    333                         cout<<c[d]<<a;
    334                     }
    335                     else
    336                     {
    337                         o=rand()%2;
    338                         d=rand()%4;
    339                         a=rand()%(max-min+1)+min;
    340                         cout<<c[d]<<-a;
    341                     }
    342                 }
    343             }
    344             cout<<"="<<endl;
    345         }
    346     }                                        //循环结束!
    347 
    348     system("pause");
    349 } 
    350 void cond(int p[5],int &z)
    351 {
    352     cout<<"是否有乘除?   “有 1/没有 0”        :";                    //条件支持函数
    353     cin>>p[0];
    354     cout<<endl;
    355     cout<<"加减是否有负数?   “有 1/没有 0”         :";
    356     cin>>p[1];
    357     cout<<endl;
    358     if(p[0]==0)
    359     {
    360         judge(p,z);
    361     }
    362     else
    363     {
    364         cout<<"除法是否有余数?   “有 1/没有 0”         :";
    365         cin>>p[2];
    366         cout<<endl;
    367         cout<<"是否支持分数?   “假分数11 /真分数10/不支持00”        :";
    368         cin>>p[3];
    369         cout<<endl;
    370         cout<<"是否支持小数,支持几位  “支持 1/不支持 0”        :";
    371         cin>>p[4];
    372         cout<<endl;
    373         judge(p,z);
    374     }
    375  }
    376    void judge(int p[5],int &z)
    377  {                         
    378      if(p[0]==0&&p[1]==0)                //只有加减且无负数
    379      {
    380          z=00;
    381      }
    382      if(p[0]==0&&p[1]==1)                //只有加减有负数
    383      {
    384          z=01;
    385      }
    386      if(p[0]==1&&p[1]==0&&p[2]==0&&p[3]==00&&p[4]==0)     //不支持负数,余数,分数,小数的四则运算 
    387      {
    388          z=100000;
    389      }
    390      if(p[0]==1&&p[1]==0&&p[2]==0&&p[3]==10&&p[4]==0)     //不支持负数,余数,小数的四则运算 ,但支持真分数
    391      {
    392          z=100100;
    393      }
    394      if(p[0]==1&&p[1]==0&&p[2]==0&&p[3]==11&&p[4]==0)     //不支持负数,余数,小数的四则运算,但支持假分数 
    395      {
    396          z=100110;
    397      }
    398      if(p[0]==1&&p[1]==0&&p[2]==1&&p[3]==00&&p[4]==0)     //不支持负数,分数,小数的四则运算 ,但支持余数
    399      {
    400          z=101000;
    401      }
    402      if(p[0]==1&&p[1]==0&&p[2]==1&&p[3]==10&&p[4]==0)     //不支持负数,小数的四则运算 ,但支持余数,真分数
    403      {
    404          z=101100;
    405      }
    406       if(p[0]==1&&p[1]==0&&p[2]==1&&p[3]==10&&p[4]==0)     //不支持负数,小数的四则运算 ,但支持余数,假分数
    407      {
    408          z=101110;
    409      }
    410       if(p[0]==1&&p[1]==1&&p[2]==1&&p[3]==10&&p[4]==0)     //不支持小数的四则运算 ,但支持余数,真分数,负数
    411      {
    412          z=111100;
    413      }
    414       if(p[0]==1&&p[1]==1&&p[2]==1&&p[3]==00&&p[4]==0)     //不支持分数,小数的四则运算 ,但支持余数,负数
    415      {
    416          z=111000;
    417      }
    418       if(p[0]==1&&p[1]==1&&p[2]==0&&p[3]==00&&p[4]==0)     //不支持余数,分数,小数的四则运算 ,但支持负数
    419      {
    420          z=110000;
    421      }
    422       if(p[0]==1&&p[1]==1&&p[2]==0&&p[3]==10&&p[4]==0)     //不支持余数,小数的四则运算 ,但支持真分数,负数
    423      {
    424          z=110100;
    425      }
    426       if(p[0]==1&&p[1]==1&&p[2]==0&&p[3]==11&&p[4]==0)     //不支持余数,小数的四则运算 ,但支持假分数,负数
    427      {
    428          z=110110;
    429      }
    430       if(p[0]==1&&p[1]==1&&p[2]==1&&p[3]==11&&p[4]==0)     //不支持小数的四则运算 ,但支持假分数,负数,余数
    431      {
    432          z=111110;
    433       }
    434      
    435  }

     

  • 相关阅读:
    TensorFlowMNIST数据集逻辑回归处理
    TensorFlow多元线性回归实现
    TensorFlow简单线性回归
    mysql ON DUPLICATE KEY UPDATE
    20200908 morning diary
    LambdaUtil
    数据库SQL自我书写册
    getMyFriendsListFromWeChat()
    MySQL8.*的JSON语法知多少
    Exception in thread "main" java.lang.IllegalArgumentException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token at [Source: UNKNOWN; line: -1, c
  • 原文地址:https://www.cnblogs.com/zjy666/p/5269478.html
Copyright © 2011-2022 走看看