题目:含有分数的四则运算
int a, b, c, d, op;
a = rand() % 100;
while (1) {
b = rand() % 100;
if (b != 0) {
break;
}
}
c = rand() % 100;
while (1) {
d = rand() % 100;
if (d != 0) {
break;
}
}
op = rand() % 4;
int m, n;
if(op == 0){
m = a * d + b * c;
n = b * d;
}
else if (op == 1) {
m = a * d - b * c;
n = b * d ;
}
else if (op == 2) {
m = a * c;
n = b * d;
}
else {
m = a * d;
n = b * c;
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int tmp = abs(gcd(a, b));
a /= tmp;
b /= tmp;
tmp = abs(gcd(c, d));
c /= tmp;
d /= tmp;
return 0;
}
结果: