四则运算2

1. 题目避免重复:

将每一个随机的算术题的两个随机数、运算符和计算结果存储在不同数组之中,在出下一道题时先比较计算结果是否相同,若是相同再比较运算符是否相同,以此来避免重复;

2. 定制数量,打印方式:增加两个变量,用for循环来控制;

3.用rand产生0~3来控制运算符,只需要让其产生0~1,将乘除法去除就可以;数值范围同样用rand控制就可以;用(rand()%2?-1:1)*(随机数)就可以产生正负数;产生两个随机数后进行求余运算,若余数为0,输出运算,若余数不为0,跳过就可以了;

源代码:

#include<iostream>

#include<ctime>

 

using namespace std;

 

int main()

{

     int w,i,j,k;

     int s,p;

     char q,o,m;

 

     srand((unsigned) time(NULL));

 

     cout<<"请输入出题数:"<<endl;

     cin>>s;

     cout<<"请输入每行输出的个数:"<<endl;

     cin>>p;

     cout<<"请输入数值范围:"<<endl;

     cin>>w;

     cout<<"是否需要乘除法?(Y/N):"<<endl;

     cin>>q;

     cout<<"是否需要负数?(Y/N):"<<endl;

     cin>>o;

     cout<<"除法是否有余数?(Y/N):"<<endl;

     cin>>m;

    

     for(int a=1;a<=s;a++)

     {

         if (q=='y'||q=='Y')

              k=rand()%4;

         else

              k=rand()%2;

 

         if (o=='y'||o=='Y')

         {

              i=(rand()%2?-1:1)*rand()%w;

              j=(rand()%2?-1:1)*rand()%w;

 

              if (k==3)

              {

                   while(j==0)

                       j=(rand()%2?-1:1)*rand()%w;

              }

 

         }

         else

         {

              i=rand()%w;

              j=rand()%w;

 

              if (k==3)

              {

                   while(j==0)

                       j=(rand()%2?-1:1)*rand()%w;

              }

          }

 

        

        

         if (j<0)

         {

              if (k==0)

                   cout<<i<<"+("<<j<<")="<<"   ";

              if (k==1)

                   cout<<i<<"-("<<j<<")="<<"   ";

              if (k==2)

                   cout<<i<<"*("<<j<<")="<<"   ";

              if (k==3)

              {

                   if ((m!='y'||m!='Y')&&i%j!=0)

                   {

                       a--;

                       continue;

                   }

                   cout<<i<<"/("<<j<<")="<<"   ";

              }

         }

         else

         {

              if (k==0)

                   cout<<i<<"+"<<j<<"="<<"   ";

              if (k==1)

                   cout<<i<<"-"<<j<<"="<<"   ";

              if (k==2)

                   cout<<i<<"*"<<j<<"="<<"   ";

              if (k==3)

              {

                   if ((m!='y'||m!='Y')&&i%j!=0)

                   {

                       a--;

                       continue;

                   }

                   cout<<i<<"/"<<j<<"="<<"   ";

              }

         }

 

         if (a%p==0)

              cout<<endl;

     }

     return 0;

}

 

程序截图:

编程总结:

这个程序可以自己控制出题数目、每一行的个数、算法的数值范围、有无乘除法、有无负数、除法有无余数这几个条件;通过这个实验,我深刻的体会到一个复杂的,功能尽可能齐全的软件不是一两天可以完成的,而且设计的思路是重中之重,只要思路清晰,代码的编写很简单

时间日志:

posted on 2015-03-19 17:45  温柔的心  阅读(121)  评论(0编辑  收藏  举报

导航