码迷,mamicode.com
首页 > 其他好文 > 详细

小学二年级题目的改进

时间:2015-03-15 15:15:00      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

以下程序满足的条件:

     

  1.避免重复

      随机算取一组随机数,将第二道题和第一道题操作数进行对比,如果发现两个操作数都完全一样话,就删除此组随机数,重新进行对边,直至最后没有重复的数组。

  2.可定制

      可控制每行打印题目的个数,以及所有算术题的个数从而控制打印格式

  3.是否有乘除法

     题目随机出现后,将所有运算符号送入一个数组,然后进行检索,查询是否有乘除号

  4.数值范围

     随机数的取值范围在0-99之间,这样结果就不会超过200。

  5.有无括号

      当有乘除法的时候有括号,无乘除法的话,就没有括号

  6.打印中的每行建间隔可以调整

     可以根据需要调整。

 

 

源代码:

#include<iostream.h>

#include<stdlib.h>

#include<time.h>

void main()

{

      int a,i,k,m,q,j,h,v; //定义几个变量

      int *o1,*o2,*o3;     //3个数组指针       

      char *oc1,*oc2;      //两个运算符指针

      char n1,n2;          //n1有无乘除  n2有无括号

      int a1,a2;

      char g[4]={‘+‘,‘-‘,‘*‘,‘/‘};

      srand((unsigned)time(NULL)); //本地时间

b:   cout<<"请问您想输出多少道题目?"<<endl;

      cin>>q;

      cout<<"请输入操作数值的最大范围:"<<endl;

      cin>>j;

      cout<<"请问是否有无乘除法?(Y/N)"<<endl;

      cin>>n1;

      cout<<"请问是否有无括号?(Y/N)"<<endl;

      cin>>n2;

    cout<<"请输入每行运算式的个数:"<<endl;

    cin>>v;

      cout<<"请输入每行运算式之间的间隔:"<<endl;

      cin>>a2;

      o1=new int[q];           //动态生成数组

      o2=new int[q];

      o3=new int[q];

      oc1=new char[q];

      oc2=new char[q];

      cout<<"以下为随机选取的题目:"<<endl;

      for(i=1;i<=q;i++)          //定义一个可以改变题目数量的q

      {

           if(n1==‘Y‘)

           {

                

                 oc1[i]=g[rand()%4];     //获取运算符

                 oc2[i]=g[rand()%4];

                 o1[i]=rand()%j;         //获取随机数

                 o2[i]=rand()%j;

                 o3[i]=rand()%j;

                 if(i==1)  

                 {

                      if(n2==‘N‘)

                      {

                            cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                      }

                      else

                      {

                            if(rand()%2==0)                //判断括号的位置

                            {

                                  if(oc1[i]==‘+‘||oc1[i]==‘-‘)  //括号在前

                                  {

                                       if(oc2[i]==‘*‘||oc2[i]==‘/‘)

                                       {

                                             cout<<‘(‘<<o1[i]<<oc1[i]<<o2[i]<<‘)‘<<oc2[i]<<o3[i]<<"=    ";

                                       }

                                       else

                                       {

                                             cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                       }

                                  }

                                  if(oc1[i]==‘*‘||oc1[i]==‘/‘)

                                  {

                                       cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                  }

                            }

                            else

                            {

                                  if(oc1[i]==‘+‘||oc1[i]==‘-‘)  //括号在后

                                  {

                                       cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                  }

                                  if(oc1[i]==‘*‘||oc1[i]==‘/‘)

                                  {

                                       if(oc2[i]==‘+‘||oc2[i]==‘-‘)

                                       {

                                             cout<<o1[i]<<oc1[i]<<‘(‘<<o2[i]<<oc2[i]<<o3[i]<<‘)‘<<"=    ";

                                       }

                                       else

                                       {

                                             cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                       }

                                  }

                            }

                      }

                 }

                 else

                 {

                      for (k = 0; k < i; k++)                        //避免重复start

                      {

                            if (o1[k] == o1[i] && o2[k] == o2[i] && o3[k] == o3[i] && oc1[k] == oc1[i] && oc2[k] == oc2[i])

                            {

                                  i--;

                                  break;

                            }

                            else

                            {

                                  if (k == i - 1)  //比较到最后一个数时

                                  {

                                       //循环输出/*q*/道运算题

                                       if(n2==‘N‘)

                                       {

                                             cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                       }

                                       else

                                       {

                                             if(rand()%2==0)                //判断括号的位置

                                             {

                                                  if(oc1[i]==‘+‘||oc1[i]==‘-‘)  //括号在前

                                                  {

                                                        cout<<‘(‘<<o1[i]<<oc1[i]<<o2[i]<<‘)‘<<oc2[i]<<o3[i]<<"=    ";

                                                  }

                                                  if(oc1[i]==‘*‘||oc1[i]==‘/‘)

                                                  {

                                                        cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                                  }

                                             }

                                             else

                                             {

                                                  if(oc1[i]==‘+‘||oc1[i]==‘-‘)  //括号在后

                                                  {

                                                        cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                                  }

                                                  if(oc1[i]==‘*‘||oc1[i]==‘/‘)

                                                  {

                                                        if(oc2[i]==‘+‘||oc2[i]==‘-‘)

                                                        {

                                                              cout<<o1[i]<<oc1[i]<<‘(‘<<o2[i]<<oc2[i]<<o3[i]<<‘)‘<<"=    ";

                                                        }

                                                        else

                                                        {

                                                              cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                                        }

                                                  }

                                             }

                                       }

                                  }   

                            }

                      }                                              //避免重复end

                 }

           }

           else if(n1==‘N‘)

           {

                 oc1[i]=g[rand()%2];

                 oc2[i]=g[rand()%2];

                 o1[i]=rand()%j;

                 o2[i]=rand()%j;

                 o3[i]=rand()%j;

                 if(i==1)

                 {

                      cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                 }

                 else

                 {

                      for (k = 0; k < i; k++)                                  //避免重复start

                      {

                            if (o1[k] == o1[i] && o2[k] == o2[i] && o3[k] == o3[i] && oc1[k] == oc1[i] && oc2[k] == oc2[i])

                            {

                                  i--;

                                  break;

                            }

                            else

                            {

                                  if (k == i - 1)  //比较到最后一个数时

                                  {

                                       //循环输出/*q*/道运算题

                                       cout<<o1[i]<<oc1[i]<<o2[i]<<oc2[i]<<o3[i]<<"=    ";

                                  }   

                            }

                      }                                                         //避免  重复end

                 }

           }

           if (i%v==0)

           {

                 cout<<endl ;

                 for(a1=1;a1<=a2;a1++)

                 {

                      cout<<endl;

                 }

           }

          

          

      }

      cout<<endl;

      cout<<endl;

      cout<<"请重新测试:"<<endl ;

      goto b  ;

}

 

截图:

技术分享技术分享

 

   程序感悟:大的程序不好写但是可以按照需求一步一步来,积少成多也许就会更容易一点,同时还因为以前的基础不牢靠,导致很多小的问题还是需要请教别人,以后一定会多努力!!

小学二年级题目的改进

标签:

原文地址:http://www.cnblogs.com/TSbj/p/4339736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!