pat1088 Rational Arithmeti

#include
#include
#include
#include
#include
#include
#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.down)cout<

你可能感兴趣的:(PAT)