/* 底云飞 2016.3.19 */
#include <iostream>
#include <time.h>
#include <string>
using
namespace std;
#define MAX 2000
//显示有乘除法的运算题
void show_multiplication()
{
???//随机数dividend1和divisor1对应第一个随机数和第二个随机数,operate是用于四则运算的
???float dividend1[MAX],divisor1[MAX],operate[4]; //分别为第一个随机数,第二个随机数,运算符
???float result=0; //result是结果
???string fu; //运算符号
???int a=0; //a用来给计数的
???int number=0; //要生成多少道题
???cout<<"要生成多少道题呢?";
???cin>>number;
???for(int i=1;;i++)
???{
p: dividend1[i] = rand()%100;
??????divisor1[i] = rand()%100;
??????operate[i%4+1] = rand()%4+1; //生成第一二个随机数和随机运算符的代表数字
??????for(int j=1;j<=i-1;j++)
?????????if(dividend1[i]==dividend1[j])
????????????if(divisor1[i]==divisor1[j])
???????????????if(operate[i%4+1]==operate[j%4+1]) //判断是否重复
??????????????????goto p;
??????if(operate[i%4+1]==1)
??????{
?????????result=dividend1[i]+divisor1[i];
?????????fu = "+";
??????}
??????else
if(operate[i%4+1]==2)
??????{
?????????result=dividend1[i]-divisor1[i];
?????????fu = "-";
??????}
??????else
if(operate[i%4+1]==3)
??????{
?????????result=dividend1[i]*divisor1[i];
?????????fu = "*";
??????}
??????else
if(operate[i%4+1]==4)
??????{
?????????result=dividend1[i]/divisor1[i];
?????????fu = "/";
??????} //直接用result把运算结果算出来,用于后面显示
??????if(fu=="/"&&divisor1[i]==0) //如果除法时分母为0,则重新生成随机数
?????????goto p;
??????cout<<a+1<<". "<<dividend1[i]<<fu<<divisor1[i]<<"="<<result<<endl;
??????a++;
??????if(a==number) //当达到指定数量时,结束
?????????break;
???} //for
}
?
//显示没有乘除法的运算题
void show_nomultiplication()
{
???//随机数dividend1和divisor1对应第一个随机数和第二个随机数,operate是用于四则运算的
???float dividend1[MAX],divisor1[MAX],operate[2]; //分别为第一个随机数,第二个随机数,运算符
???float result=0; //result是结果
???string fu; //运算符号
???int a=0; //a用来给计数的
???int number=0; //要生成多少道题
???cout<<"要生成多少道题呢?";
???cin>>number;
???for(int i=1;;i++)
???{
p: dividend1[i] = rand()%100;
??????divisor1[i] = rand()%100;
??????operate[i%2+1] = rand()%2+1;
??????for(int j=1;j<=i-1;j++)
?????????if(dividend1[i]==dividend1[j])
????????????if(divisor1[i]==divisor1[j])
???????????????if(operate[i%2+1]==operate[j%2+1])
??????????????????goto p;
??????if(operate[i%2+1]==1)
??????{
?????????result=dividend1[i]+divisor1[i];
?????????fu = "+";
??????}
??????else
if(operate[i%2+1]==2)
??????{
?????????result=dividend1[i]-divisor1[i];
?????????fu = "-";
??????} //直接用result把运算结果算出来,用于后面显示
??????cout<<a+1<<". "<<dividend1[i]<<fu<<divisor1[i]<<"="<<result<<endl;
??????a++;
??????if(a==number) //当达到指定数量时,结束
?????????break;
???} //for
}
?
//显示假分数运算题
void showimproperfraction()
{
???//int dividend1[MAX],divisor1[MAX],operate[4]; //分别为被除数,除数和运算符
???float dividend1[MAX], divisor1[MAX], dividend2[MAX], divisor2[MAX], operate[MAX];
???int number,b=0; //number用来输入几个假分数的运算,b用来计数,查看是否到达number个
???float result; //计算之后的结果
???string fu; //计算符号
???cout<<"需要生成几个假分数的四则运算?";
???cin>>number;
???cout<<"下面是生成的"<<number<<"个假分数四则运算:"<<endl;
???cout<<"个数 "<<"随机次数 "<<"题目"<<endl;
???for(int i=1;;i++)
???{
q: dividend1[i] = rand()%100;
??????divisor1[i] = rand()%100;
??????dividend2[i] = rand()%100;
??????divisor2[i] = rand()%100;
??????operate[i%4+1] = rand()%4+1;
??????for(int j=1;j<=i-1;j++)
?????????if(dividend1[i]==dividend1[j])
????????????if(divisor1[i]==divisor1[j])
???????????????if(dividend2[i]==dividend2[j])
??????????????????if(divisor2[i]==divisor2[j])
?????????????????????if(operate[i%4+1]==operate[j%4+1])
????????????????????????goto q;
??????if(divisor1[i]>=dividend1[i]&&divisor2[i]>=dividend2[i]&÷nd1[i]!=0&&divisor1[i]!=0) //判断是真分数并且分母不能为0
??????{
?????????if(operate[i%4+1]==1)
?????????{
????????????result=(divisor1[i]/dividend1[i])+(divisor2[i]/dividend2[i]);
????????????fu = "+";
?????????}
?????????else
if(operate[i%4+1]==2)
?????????{
????????????result=(divisor1[i]/dividend1[i])-(divisor2[i]/dividend2[i]);
????????????fu = "-";
?????????}
?????????else
if(operate[i%4+1]==3)
?????????{
????????????result=(divisor1[i]/dividend1[i])*(divisor2[i]/dividend2[i]);
????????????fu = "*";
?????????}
?????????else
if(operate[i%4+1]==4)
?????????{
????????????result=(divisor1[i]/dividend1[i])/(divisor2[i]/dividend2[i]);
????????????fu = "/";
?????????}
?????????cout<<b+1<<". "<<i<<". "<<"("<<divisor1[i]<<"/"<<dividend1[i]<<")"<<fu<<"("<<divisor2[i]<<"/"<<dividend2[i]<<")="<<result<<endl;
?????????b++;
??????}//判断是两个真分数的if条件语句
??????if(b==number) //当达到真分数数量要求时,停止
?????????break;
???}//for
}
?
void main()
{
???int fenshuflag,chengchuflag;
???srand((unsigned)time(0)); //用时间做随机种子,随机数发生器
???cout<<"需要有乘除法吗?需要输入1,不需要输入0:"<<endl;
???cin>>chengchuflag;
???if(chengchuflag==1)
??????show_multiplication(); //带有乘除法的运算
???else
??????show_nomultiplication(); //不带有乘除法的运算
?
//假分数的四则运算
???cout<<"需要生成假分数的四则运算吗(需要输入1,不需要输入0)?:";
???cin>>fenshuflag;
???if(fenshuflag==1)
??????showimproperfraction();
???else
if(fenshuflag == 0)
??????exit(1);
}