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

四则运算二之结果

时间:2015-03-19 18:00:48      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

设计思想:

  1. 题目避免重复

     将得到的题目放入数组,并与前面的进行匹配,若相同则跳过且变量减一。

  1. 可定制

     通过一个变量控制输出来确定输出格式。

  1. 控制参数

     是否有乘除法

          If语句当面对除法时,控制被除数不为0。若为零,则跳过且变量减一。

     数值范围

          随机数控制。

     加减有无负数

         If语句检验随机数是否小于0,若小于零则跳过且变量减一。

     除法有无余数

         If语句检验,其余同上。

     是否支持分数

         If语句随机出现分数的运算。

实现代码:

//四则运算二,王昭博,3.13,2015

 

#include<iostream>

using namespace std;

#define N 30

#include<cmath>

 

void main ()

{

         double a,b[N],c[N],d[N],e[N],f;

         char g ;

         cout<<"若允许有负数请输入Y;否则,输入N:"<<endl;

         cin>>g;

         for( int i = 0 ; i < N ; i++ )

         {

                   a = rand()%2;//决定是否出现分数题目            

                   if( a == 0 )//分数题目出现

                   {

                            b[i] = rand()%100;

                            c[i] = rand()%100;

                            d[i] = rand()%100;

                            e[i] = rand()%100;

                            f = rand()%4;

                            if( f == 0)

                            {

                                     if(c[i] == 0 || e[i] == 0)//控制被除数及分子不为零

                                     {i--;break;}

                                     for( int j=0;j<i;j++)//避免重复

                                     {

                                               if(b[i] == b[j] && c[i] == c[j] && d[i] == d[j] && e[i] == e[j])

                                               {i--;break;}

                                     }

                                     //控制有无负数

                                     if(g == ‘Y‘)

                                     {cout<<b[i]<<"/"<<c[i]<<" + "<<d[i]<<"/"<<e[i]<<"="<<endl;}

                                     else

                                     {

                                               if(b[i] < 0 || c[i] < 0 || d[i] < 0 || e[i] < 0)

                                               {i--;break;}

                                               else

                                               {cout<<b[i]<<"/"<<c[i]<<" + "<<d[i]<<"/"<<e[i]<<"="<<endl;}

                                     }

                            }

                            else if( f == 1)

                            {

                                     if(c[i] == 0 || e[i] == 0)//控制被除数及分子不为零

                                     {i--;break;}

                                     for( int j=0;j<i;j++)//避免重复

                                     {

                                               if(b[i] == b[j] && c[i] == c[j] && d[i] == d[j] && e[i] == e[j])

                                               {i--;break;}

                                     }

                                     //控制有无负数

                                     if(g == ‘Y‘)

                                     {cout<<b[i]<<"/"<<c[i]<<" - "<<d[i]<<"/"<<e[i]<<"="<<endl;}

                                     else

                                     {

                                               if(b[i] < 0 || c[i] < 0 || d[i] < 0 || e[i] < 0)

                                               {i--;break;}

                                               else

                                               {cout<<b[i]<<"/"<<c[i]<<" - "<<d[i]<<"/"<<e[i]<<"="<<endl;}

                                     }

                            }

                            else if( f == 2)

                            {

                                     if(c[i] == 0 || e[i] == 0)//控制被除数及分子不为零

                                     {i--;break;}

                                     for( int j=0;j<i;j++)//避免重复

                                     {

                                               if(b[i] == b[j] && c[i] == c[j] && d[i] == d[j] && e[i] == e[j])

                                               {i--;break;}

                                     }

                                     //控制有无负数

                                     if(g == ‘Y‘)

                                     {cout<<b[i]<<"/"<<c[i]<<" * "<<d[i]<<"/"<<e[i]<<"="<<endl;}

                                     else

                                     {

                                               if(b[i] < 0 || c[i] < 0 || d[i] < 0 || e[i] < 0)

                                               {i--;break;}

                                               else

                                               {cout<<b[i]<<"/"<<c[i]<<" * "<<d[i]<<"/"<<e[i]<<"="<<endl;}

                                     }

                            }

                            else

                            {

                                     if(c[i] == 0 || e[i] == 0 || d[i] == 0)//控制被除数及分子不为零

                                     {i--;break;}

                                     for( int j=0;j<i;j++)//避免重复

                                     {

                                               if(b[i] == b[j] && c[i] == c[j] && d[i] == d[j] && e[i] == e[j])

                                               {i--;break;}

                                     }

                                     //控制有无负数

                                     if(g == ‘Y‘)

                                     {cout<<b[i]<<"/"<<c[i]<<" / "<<d[i]<<"/"<<e[i]<<"="<<endl;}

                                     else

                                     {

                                               if(b[i] < 0 || c[i] < 0 || d[i] < 0 || e[i] < 0)

                                               {i--;break;}

                                               else

                                               {cout<<b[i]<<"/"<<c[i]<<" / "<<d[i]<<"/"<<e[i]<<"="<<endl;}

                                     }

                            }

                   }

                   else//非分数题目出现

                   {

                            b[i] = rand()%100;

                            c[i] = rand()%100;

                            f = rand()%4;

                            if( f == 0)

                            {

                                     for( int j=0;j<i;j++)

                                     {

                                               if(b[i] == b[j] && c[i] == c[j])//避免重复

                                               {i--;break;}

                                     }

                                     //控制有无负数

                                     if(g == ‘Y‘)

                                     {cout<<b[i]<<" + "<<c[i]<<"="<<endl;}

                                     else

                                     {

                                               if(b[i] < 0 || c[i] < 0)

                                               {i--;break;}

                                               else

                                               {cout<<b[i]<<" + "<<c[i]<<"="<<endl;}

                                     }

                            }

                            else if( f == 1)

                            {

                                     for( int j=0;j<i;j++)//避免重复

                                     {

                                               if(b[i] == b[j] && c[i] == c[j])

                                               {i--;break;}

                                     }

                                     //控制有无负数

                                     if(g == ‘Y‘)

                                     {cout<<b[i]<<" - "<<c[i]<<"="<<endl;}

                                     else

                                     {

                                               if(b[i] < 0 || c[i] < 0)

                                               {i--;break;}

                                               else

                                               {cout<<b[i]<<" - "<<c[i]<<"="<<endl;}

                                     }

                            }

                            else if( f == 2)

                            {

                                     for( int j=0;j<i;j++)//避免重复

                                     {

                                               if(b[i] == b[j] && c[i] == c[j])

                                               {i--;break;}

                                     }

                                     //控制有无负数

                                     if(g == ‘Y‘)

                                     {cout<<b[i]<<" * "<<c[i]<<"="<<endl;}

                                     else

                                     {

                                               if(b[i] < 0 || c[i] < 0)

                                               {i--;break;}

                                               else

                                               {cout<<b[i]<<" * "<<c[i]<<"="<<endl;}

                                     }

                            }

                            else

                            {

                                     if( c[i] == 0)//控制被除数及分子不为零

                                               {i--;break;}

                                     for( int j=0;j<i;j++)//避免重复

                                     {

                                               if(b[i] == b[j] && c[i] == c[j])

                                               {i--;break;}

                                     }

                                     //控制有无负数

                                     if(g == ‘Y‘)

                                     {cout<<b[i]<<" / "<<c[i]<<"="<<endl;}

                                     else

                                     {

                                               if(b[i] < 0 || c[i] < 0)

                                               {i--;break;}

                                               else

                                               {cout<<b[i]<<" / "<<c[i]<<"="<<endl;}

                                     }

                            }

                   }

         }

}

 

结果截图:

技术分享

心得总结:

一开始,我尝试着不看设计思想去做,发现做着做着就卡壳;后来,我就边看设计思想边写,很快就做完了,虽然我的代码有些过于冗杂了,但是很顺畅(当然代码比较简单也是其中一个原因所在)。

时间记录日志

学生:

王昭博

 

日期:

3/13/2015

 

教师:

王建民

 

课程:

PSP

 

 

 

 

 

 

 

日期

开始时间

结束时间

净时

活动

备注

3月13日

10点10分

12点

100分钟

讲师授课

 

3月13日

13点

13点20分

20分钟

编写程序

 

3月18日

16点

17点30分

90分钟

编写程序

 

3月18日

21点30分

22点

30分钟

制作时间记录日志模板

 

四则运算二之结果

标签:

原文地址:http://www.cnblogs.com/yuntianblog/p/4350889.html

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