PAT1088

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值