程序思想:
1.将随机数生成、加减符号生成、乘除符号生成、括号生成、储存分别用函数封装
2.判断是否有乘除,是否有负数,是否有余数、是否有括号、位数。
3.通过判断依次引用函数。
代码
using namespace std;
string *o;
int j=0,js=0;
int Rnum(int min,int max ) //randon number (digit)
{
int random;
srand(time(NULL));
random=(rand()%(max-min+1))+min;
return random;
}
char Ras(int judgemd )// add subtraction multiplication division operator
{
int random;
char operation;
if(judgemd=2)random=Rnum(1,2);
else random=Rnum(3,4);
switch(random)
{
case(1):{operation=‘+‘;break;}
case(2):{operation=‘-‘;break;}
case(3):{operation=‘*‘;break;}
case(4):{operation=‘/‘;break;}
}
return operation;
}
bool Compare(string str)
{
bool b=true;
for(int i=0;i<j;i++)
{
if(str==o[i])
b=false;
}
return b;
}
void Storage(string str)//string or 邻ⅷ?接ó表括?
{
o[j]=str;
j++;
}
void Asout (int judgen,int min,int max)// if here is not negaive it is true,else false
{
int num1,num2,num;
char ope;
string o="";
num1= Rnum(min,max);
num2= Rnum(min,max);
ope=Ras(2);
if(judgen==2)
{
if(ope==‘-‘)
{
if(num1<num2)
{
num=num1;
num1=num2;
num2=num;
}
}
}
o=num1+ope+num2;
if(Compare(o)==true)
{
Storage(o);
js++;
}
//比括?较? 和í储洹?存?
cout<<num1<<ope<<num2<<"="<<endl;
}
void Mdout(int judger,int min,int max)//
{
int num1,num2,num;
char ope;
string o;
num1= Rnum(min,max);
num2= Rnum(min,max);
ope=Ras(1);
if(judger==2)
{
if(ope==‘/‘)
{
num=num1*num2;
num1=num;
}
}
o=num1+ope+num2;
if(Compare(o)==true)
{
Storage(o);
js++;
}
//比括?较? 和í储洹?存?
cout<<num1<<ope<<num2<<"="<<endl;
}//没?写′完?
void Asmdout(int judgen,int judger,int min,int max)
{
Asout ( judgen, min, max);
Mdout(judger, min, max);
}
void Bracket(int judgemd,int min,int max)//it has braket, randem digit.
{
int num;
string o="",o1;
num=Rnum(3,10);
int *bnum;
char *bope;
bnum= new int[num];
bope=new char[num-1];
o="(";
for(int i=0;i<num;i++)
{
bnum[i]=Rnum(min,max);
}
for(int i=0;i<num-1;i++).0
{
bope[i]=Ras(judgemd);
}
for(int i=0;i<num;i++)
{
cout<<bnum[i];
o1=bnum[i];
o= o +o1;
if(i=2)o=o+")";
if(i=num-1)break;
cout<<bope[i];
o=o+bope[i];
}
if(Compare(o)==true)
{
Storage(o);
js++;
}
}
void main()
{
int judgeb,judgemd,judgen,judger=2,min,max,n;
cout<<"Do you want multiplication and division? 1.Yes 2.No"<<endl;
cin>>judgemd;
cout<<"Do you want bracket? 1.Yes 2.No"<<endl;
cin>>judgeb;
if(judgeb==2)
{
cout<<"Do you want negative? 1.Yes 2.No"<<endl;
cin>>judgen;
if(judgemd==1)
{
cout<<"Do you want remainder? 1.Yes 2.No"<<endl;
cin>>judger;
}
}
cout<<"which is the scope of the number? input the min and max"<<endl;
cin>>min>>max;
cout<<"数簓量?"<<endl;
cin>>n;
o=new string[n];
while(js<n)
{
if(judgeb==1){ Bracket(judgemd, min, max);};
if(judgeb==2){Asmdout( judgen, judger, min, max);};
}
}
原文:http://www.cnblogs.com/zuhaoran/p/5295594.html