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.程序运行结果:

  • 相关阅读:
    【Hive】HiveQL:数据定义
    【Hive】Hive安装与配置
    【Hadoop】Hadoop 2.7.6安装_伪分布式集群
    【RMAN】单实例环境RMAN备份和还原
    【SQL开发】Oracle 11g 分区技术
    【SQL开发】使用绑定变量 VS 不使用绑定变量
    MySQL索引最左前缀原则导致系统瘫痪
    阿里云RDS在线DDL工具gh-ost
    centos7下MongoDB3.4安装并解决告警
    ibtmp1文件过大
  • 原文地址:https://www.cnblogs.com/wangzongze/p/5246750.html
Copyright © 2011-2022 走看看