随机生成30道四则运算题目

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

2.设计思路

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

(2)利用switch\case函数分别对每种情况进行表示,利用除以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.程序运行结果:

 

 

posted @ 2016-03-06 11:02  二十划生  阅读(504)  评论(2编辑  收藏  举报