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

  • 相关阅读:
    数据算法之汉诺塔
    Mvc模板页
    mvc局部视图
    Area区域
    mvc之文件下载
    MVC过滤器
    MVC_Ajax请求
    MVC之校验
    Json&Razor&控制器
    抓包分析,tcpdump 和 wireshark 配合使用的简单尝试
  • 原文地址:https://www.cnblogs.com/wangzongze/p/5246750.html
Copyright © 2011-2022 走看看