View Code of Problem 3494

#include<stdio.h>
#include<iostream>
using namespace std;
int minP(int x,int y)
{
	y = max(x,y);
	for(int i = y;i <= x * y;i ++)
	{
		if(i%x==0&&i%y==0) 
		{
			return i;
		}
	}
	return 0;
}
int maxP(int x,int y)
{
	for(int i = min(x,y);i >= 1;i --)
	{
		if(x%i==0&&y%i==0) return i;
	}
	return 0;
}
int main()
{
	int a, b;
	char c;
	int d, e;
	while(scanf("%d/%d%c%d/%d",&a,&b,&c,&d,&e) != EOF)
	{
		int k = minP(b,e);
		a = a * k / b;
		d = d * k / e;
		int n;
		if(c == '+')
		{
			n = a + d;
		}
		else
		{
			n = a - d;
		}
		//cout << n << " " << k << endl;
		if(n > 0)
		{
			int q = maxP(n,k);
			cout << n/q << "/" << k/q << endl;
		}
		else if(n == 0) cout << 0 << endl;
		else
		{
			n = -n;
			int q = maxP(n,k);
			cout << "-" << n/q << "/" << k/q << endl;
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3494