#include<algorithm>
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
struct node
{
ll up,down;
}a,b;
ll gcd(ll x,ll y)
{
return y == 0?x:gcd(y,x%y);
}
node reduction(node x)
{
if(x.down<0)
{
x.up = -x.up;
x.down = -x.down;
}
if(x.up == 0)x.down = 1;
else
{
int tmp = gcd(abs(x.up),abs(x.down));
x.up/=tmp;
x.down/=tmp;
}
return x;
}
node add(node x,node y)
{
node ret;
ret.up = x.up*y.down+x.down*y.up;
ret.down = x.down*y.down;
return reduction(ret);
}
node sub(node x,node y)
{
node ret;
ret.up = x.up*y.down-x.down*y.up;
ret.down = x.down*y.down;
return reduction(ret);
}
node mul(node x,node y)
{
node ret;
ret.up = x.up*y.up;
ret.down = x.down*y.down;
return reduction(ret);
}
node div(node x,node y)
{
node ret;
ret.up = x.up*y.down;
ret.down = x.down*y.up;
return reduction(ret);
}
void out(node x)
{
if(x.down == 0)
{
cout<<"Inf";
return;
}
int f = 0;
if(x.up<0)
{
f = 1;
x.up = -x.up;
}
if(x.down == 0)while(1);
if(f)cout<<"(-";
if(x.down == 1)cout<<x.up;
else if(x.up>x.down)cout<<x.up/x.down<<" "<<x.up%x.down<<"/"<<x.down;
else cout<<x.up<<"/"<<x.down;
if(f)cout<<")";
}
;int main()
{
while(scanf("%lld/%lld %lld/%lld",&a.up,&a.down,&b.up,&b.down)!=EOF)
{
a = reduction(a);
b = reduction(b);
out(a);
cout<<" + ";
out(b);
cout<<" = ";
out(add(a,b));
cout<<endl;
out(a);
cout<<" - ";
out(b);
cout<<" = ";
out(sub(a,b));
cout<<endl;
out(a);
cout<<" * ";
out(b);
cout<<" = ";
out(mul(a,b));
cout<<endl;
out(a);
cout<<" / ";
out(b);
cout<<" = ";
out(div(a,b));
cout<<endl;
}
return 0;
}
pat1088 Rational Arithmeti
最新推荐文章于 2022-02-26 20:56:17 发布