#include <iostream>
#include <string>
using namespace std;
int gcd(long long a, long long b){
return b == 0 ? a : gcd(b, a % b);
}
void print(long long a, long long b){
int yue, shang, yu, flag=0;
string ans;
if(b == 0) printf("Inf");
else if(a == 0) printf("0");
else{
if((a < 0 && b > 0) || (a > 0 && b < 0)) flag = 1; //本来用 a * b < 0, 错点
yue = gcd(a, b);
a = abs(a /yue); b = abs(b / yue);
shang = a / b;
yu = a % b;
if(flag) printf("(-");
if(shang != 0 && yu != 0) printf("%d %d/%d", shang, yu, b);
else if(yu == 0) printf("%d", shang);
else if(shang == 0) printf("%d/%d", yu, b);
if(flag) printf(")");
}
}
int main(){
long long a1, b1, a2, b2;
scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
print(a1, b1); cout << " + "; print(a2, b2); cout << " = "; print(a1*b2 + a2*b1, b1*b2); cout << endl;
print(a1, b1); cout << " - "; print(a2, b2); cout << " = "; print(a1*b2 - a2*b1, b1*b2); cout << endl;
print(a1, b1); cout << " * "; print(a2, b2); cout << " = "; print(a1*a2, b1*b2); cout << endl;
print(a1, b1); cout << " / "; print(a2, b2); cout << " = "; print(a1*b2, b1*a2); cout << endl;
return 0;
}
3好测试点是关于int 和 long 的,虽然都是整型数据,但是相乘后用int会溢出,所以用 long long。
而且用辗转相除法求最大公约数,对于负数的处理也是隐藏点。
2号测试点是关于正负号的,本来我用 a * b < 0,但是涉及到大数相乘再相乘就溢出了,所以没想到啊。