zoukankan      html  css  js  c++  java
  • 随机生成30道四则运算题目

    1.题目要求:随机生成30到四则运算题,其中不能超过2位运算,包含真分数。

    2.设计思路

      (1)先随机产生四个数,前两个数用作整数的四则运算,然后前两个数再作为第一个分数,后两个数作为第二个分数。

    (2)利用switchcase函数分别对每种情况进行表示,利用除以7的余数表示8中不同的情况,前四种表示整数的加减乘除运算,后四种表示分数的加减乘除运算。

    (3)利用for循环产生30个运算式。

    3.程序代码:

    //2016/3/3    王宗泽
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    int main()
    {
    	srand((unsigned)time(NULL));
    	int i;
    	for(i=0;i<30;i++)
    	{
    		int firstnum=rand()%100;
    		int secondnum=rand()%100;
    		int thirdnum=rand()%100;
    		int forthnum=rand()%100;
    		switch(int(firstnum%7))
    		{
    		case 0:cout<<firstnum<<"+"<<secondnum<<"="<<endl; break;
    		case 1:cout<<firstnum<<"-"<<secondnum<<"="<<endl; break;
    		case 2:cout<<firstnum<<"*"<<secondnum<<"="<<endl; break;
    		case 3:cout<<firstnum<<"/"<<secondnum<<"="<<endl; break;
    		case 4:if((firstnum>secondnum)||(thirdnum>forthnum))
    				{
    				  i=i-1;
    			 }
    			else cout<<"("<<firstnum<<"/"<<secondnum<<")"<<"+"<<"("<<thirdnum<<"/"<<forthnum<<")"<<"="<<endl; break;
    		case 5:if((firstnum>secondnum)||(thirdnum>forthnum))
    				{
    				 i=i-1;
    			}
    			else cout<<"("<<firstnum<<"/"<<secondnum<<")"<<"-"<<"("<<thirdnum<<"/"<<forthnum<<")"<<"="<<endl; break;
    		case 6:if((firstnum>secondnum)||(thirdnum>forthnum))
    			{
    				i=i-1;
    			}
    			   
    			else cout<<"("<<firstnum<<"/"<<secondnum<<")"<<"*"<<"("<<thirdnum<<"/"<<forthnum<<")"<<"="<<endl; break;
    		case 7:if((firstnum>secondnum)||(thirdnum>forthnum))
    				{
    				 i=i-1;
    			  }
    			else cout<<"("<<firstnum<<"/"<<secondnum<<")"<<"/"<<"("<<thirdnum<<"/"<<forthnum<<")"<<"="<<endl; break;
            }
    		}
    }
    

     4.程序运行结果:

  • 相关阅读:
    hdu 1823 Luck and Love 二维线段树
    UVA 12299 RMQ with Shifts 线段树
    HDU 4578 Transformation 线段树
    FZU 2105 Digits Count 线段树
    UVA 1513 Movie collection 树状数组
    UVA 1292 Strategic game 树形DP
    【ACM】hdu_zs2_1003_Problem C_201308031012
    qsort快速排序
    【ACM】nyoj_7_街区最短路径问题_201308051737
    【ACM】nyoj_540_奇怪的排序_201308050951
  • 原文地址:https://www.cnblogs.com/wangzongze/p/5246750.html
Copyright © 2011-2022 走看看