#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
struct node
{
long long up;
long long down;
};
long long gcd(long a,long b)
{
a=abs(a);
b=abs(b);
while(a%b!=0)
{
long long t=a%b;
a=b;
b=t;
}
return b;
}
node reduct(node a)
{
long long t=gcd(a.up,a.down);
a.up/=t;
a.down/=t;
return a;
}
node add(node a,node b)
{
node tnode;
tnode.up=b.down*a.up+a.down*b.up;
tnode.down=a.down*b.down;
tnode=reduct(tnode);//化简
return tnode;
}
node dif(node a,node b)
{
node tnode;
tnode.up=a.up*b.down-b.up*a.down;
tnode.down=a.down*b.down;
tnode=reduct(tnode);
return tnode;
}
node pro(node a,node b)
{
node tnode;
tnode.up=a.up*b.up;
tnode.down=a.down*b.down;
tnode=reduct(tnode);
return tnode;
}
node qua(node a,node b)
{
node tnode;
tnode.up=a.up*b.down;
tnode.down=a.down*b.up;
tnode=reduct(tnode);
if(tnode.up<0&&tnode.down<0)
{
tnode.up=-tnode.up;
tnode.down=-tnode.down;
}
else if(tnode.up>0 && tnode.down<0)
{
tnode.up=-tnode.up;
tnode.down=-tnode.down;
}
return tnode;
}
void show(node a)
{
a=reduct(a);
if(a.up<0)
printf("(");
if(a.up==0)
printf("0");
else
if(abs(a.up)<a.down)
{
printf("%lld/%lld",a.up,a.down);
}
else if(abs(a.up)==a.down)
{
printf("%lld",a.up/a.down);
}
else//假分数
{
if(a.up%a.down==0)
printf("%lld",a.up/a.down);
else
printf("%lld %lld/%lld",a.up/a.down,abs(a.up)%a.down,a.down);//***abs?
}
if(a.up<0)
printf(")");
}
int main()
{
// freopen("in.txt","r" ,stdin);
node node1,node2;
scanf("%lld/%lld %lld/%lld",&node1.up,&node1.down,&node2.up,&node2.down);
show(node1); printf(" + "); show(node2); printf(" = "); show(add(node1,node2)); printf("\n");
show(node1); printf(" - "); show(node2); printf(" = "); show(dif(node1,node2)); printf("\n");
show(node1); printf(" * "); show(node2); printf(" = "); show(pro(node1,node2)); printf("\n");
if(node2.up==0)
{
show(node1); printf(" / "); show(node2); printf(" = "); printf("Inf");
}
else
{
show(node1); printf(" / "); show(node2); printf(" = "); show(qua(node1,node2));
}
return 0;
}
1088. Rational Arithmetic (20)
最新推荐文章于 2021-05-30 17:43:52 发布