• 随机生成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.程序运行结果:

  • 相关阅读:
    28、vSocket模型详解及select应用详解
    27、通过visual s'tudio 验证 SOCKET编程:搭建一个TCP服务器
    26、TCP服务器原理
    8、字符串操作
    9、内存操作
    ESP32作为接入点AP
    ·通过wifi_scan学习esp32wifi程序编写
    10、指针变量基础
    关于wifi网络基本原理了解
    开发团队中命名规范的重要性
  • 原文地址:https://www.cnblogs.com/wangzongze/p/5246750.html
Copyright © 2020-2023  润新知