1 #include<iostream> 2 #include<cstdlib> 3 #include<ctime> 4 using namespace std; 5 #define random(x) (rand()%x) 6 int gcd(int a,int b){ 7 if(b==0)return a; 8 else return gcd(b,a%b); 9 } 10 int main(){ 11 int n=30,a,b,i,d,e,s,f,a1,b1,d1,e1; 12 char syb; 13 srand(time(NULL)); 14 while(n--){ 15 16 s=random(2); 17 a=random(99);a++; 18 b=random(99);b++; 19 i=random(4); 20 if(s){ //处理分数 21 d=random(99);d++; 22 e=random(99);e++; 23 a%=b,d%=e; //分母大于分子 24 f=gcd(a,b); //真分数分子分母去除最大公约数 25 a/=f,b/=f; 26 f=gcd(d,e); 27 d/=f,e/=f; 28 a1=a*e,b1=b*e,d1=b*d; 29 if(i==0){ //进行加减乘除运算 30 a1+=d1;syb='+'; 31 }else if(i==1){ 32 a1-=d1;syb='-'; 33 }else if(i==2){ 34 a1=a*d;syb='*'; 35 }else if(i==3){ 36 a1=a*e; 37 b1=b*d;syb='/'; 38 } 39 f=gcd(abs(a1),b1); 40 a1/=f,b1/=f; 41 cout<<a<<"/"<<b<<" "<<syb<<" "<<d<<"/"<<e<<" = "<<a1<<"/"<<b1<<endl; 42 }else{ 43 b1=0; 44 if(i==0){ 45 a1=a+b;syb='+'; 46 }else if(i==1){ 47 a1=a-b;syb='-'; 48 }else if(i==2){ 49 a1=a*b;syb='*'; 50 }else if(i==3){ 51 a1=a/b; 52 b1=a%b; 53 syb='/'; 54 } 55 cout<<a<<" "<<syb<<" "<<b<<" = "<<a1; 56 if(b1)cout<<"+"<<b1<<"/"<<b; 57 cout<<endl; 58 } 59 } 60 return 0; 61 }