测试点3
乘法可能会溢出,用long long
#include<cstdio>
using namespace std;
const int maxn = 110;
long long gcd(long long a, long long b) {
if (b == 0)return a;
else return gcd(b, a%b);
}
struct fs
{
long long fm, fz;
long long in;
}f[maxn];
struct fs yf(struct fs f) {
f.in = f.fz / f.fm;
f.fz = f.fz%f.fm;
long long a=gcd(f.fz, f.fm);
f.fz = f.fz / a;
f.fm = f.fm / a;
return f;
}
void myprint(struct fs f) {
int flag = 0;
if (f.in < 0 && f.fz < 0&&f.fm>0)f.fz = -f.fz;
else if (f.in == 0 && f.fz > 0 && f.fm < 0) {
f.fz = -f.fz; f.fm = -f.fm;
}
else if (f.in < 0 && f.fm < 0 && f.fz>0)f.fm = -f.fm;
if (f.in == 0 && f.fz == 0) { printf("0"); return; }
if (f.in < 0 || f.fz < 0)printf("(");
if (f.in != 0) {
printf("%lld",f.in); flag = 1;
}
if (f.fz != 0 && flag == 1)printf(" %lld/%lld", f.fz, f.fm);
else if (f.fz != 0)
{
printf("%lld/%lld", f.fz, f.fm);
}
if (f.in < 0 || f.fz < 0)printf(")");
}
int main() {
long long minfm=1;
for (int i = 0; i < 2; i++)
{
long long x,y;
scanf("%lld/%lld", &x, &y);
//printf("%d%d", x, y);
f[i].fz = x; f[i].fm = y;
minfm = f[i].fm*minfm / gcd(f[i].fm, minfm);
}
f[2].fz = f[0].fz*minfm /f[0].fm + minfm/f[1].fm*f[1].fz ;
f[2].fm = minfm;
for (int i = 0; i < 2; i++)
{
f[i+4] = yf(f[i]);
}myprint(f[4]); printf(" + ");
myprint(f[5]); printf(" = ");
f[2]=yf(f[2]);
myprint(f[2]);
printf("\n");
f[2].fz = f[0].fz*minfm / f[0].fm - minfm / f[1].fm*f[1].fz;
f[2].fm = minfm;
for (int i = 0; i < 2; i++)
{
f[i + 4] = yf(f[i]);
}myprint(f[4]); printf(" - ");
myprint(f[5]); printf(" = ");
f[2] = yf(f[2]);
myprint(f[2]);
printf("\n");
f[2].fz = f[0].fz*f[1].fz;
f[2].fm = f[0].fm*f[1].fm;
for (int i = 0; i < 2; i++)
{
f[i + 4] = yf(f[i]);
}myprint(f[4]); printf(" * ");
myprint(f[5]); printf(" = ");
f[2] = yf(f[2]);
myprint(f[2]);
printf("\n");
if (f[1].in == 0 && f[1].fz == 0) {
for (int i = 0; i < 2; i++)
{
f[i + 4] = yf(f[i]);
}myprint(f[4]); printf(" / ");
myprint(f[5]); printf(" = ");
printf("Inf");
printf("\n");
}
else {
f[2].fz = f[0].fz*f[1].fm;
f[2].fm = f[0].fm*f[1].fz;
for (int i = 0; i < 2; i++)
{
f[i + 4] = yf(f[i]);
}myprint(f[4]); printf(" / ");
myprint(f[5]); printf(" = ");
f[2] = yf(f[2]);
myprint(f[2]);
printf("\n");
}
return 0;
}