PAT甲级1088

1088. Rational Arithmetic (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

#include
#include
#include
using namespace std;
struct fraction
{
	long long  a, b;
	long long k;
	fraction():k(0){}
};
int gcd(long long a, long long b)
{
	return !b ? a : gcd(b, a%b);
}
fraction reduction(fraction f)
{
	fraction ft;
	if (f.b < 0)
	{
		ft.a = -f.a;
		ft.b = -f.b;
		f.a = -f.a;
		f.b = -f.b;
	}
	 if (f.a == 0)
	{
		ft.a = 0;
		ft.b = 1;
		f.a = 0;
		f.b = 1;
	}
	
	ft.a= f.a/gcd(abs(f.a), f.b);
	ft.b= f.b/gcd(abs(f.a), f.b);
	if (abs(ft.a) >= ft.b)
	{
		if (f.a < 0)
		{
			ft.k = -abs(ft.a) / ft.b;
			ft.a = abs(ft.a) % ft.b;
		}
		else
		{
			ft.k = abs(ft.a) / ft.b;
			ft.a = abs(ft.a) % ft.b;
		}
	}
	return ft;
}
fraction add(fraction f1, fraction f2)
{
	fraction f;
	f.a = f1.a*f2.b + f2.a*f1.b;
	f.b = f1.b*f2.b;
	return reduction(f);
}
fraction substract(fraction f1, fraction f2)
{
	fraction f;
	f.a = f1.a*f2.b - f2.a*f1.b;
	f.b = f1.b*f2.b;
	return reduction(f);
}
fraction multiply(fraction f1, fraction f2)
{
	fraction f;
	f.a = f1.a*f2.a;
	f.b = f1.b*f2.b;
	return reduction(f);
}
fraction divide(fraction f1, fraction f2)
{
	fraction f;
	f.a = f1.a*f2.b;
	f.b = f1.b*f2.a;
	return reduction(f);
}
void printFraction(fraction f)
{
	if (f.k > 0)
	{
		if (f.a != 0)
		{
			cout << f.k << " " << f.a << "/" << f.b;
		}
		else
			cout << f.k;
	}
	else if (f.k == 0)
	{
		if (f.a > 0)
		{
			cout << f.a << "/" << f.b;
		}
		else if (f.a == 0)
			cout << 0;
		else
			cout << "(" << f.a << "/" << f.b << ")";
	}
	else
	{
		if (f.a != 0)
			cout << "(" << f.k << " " << f.a << "/" << f.b<<")";
		else
			cout << "(" << f.k << ")";
	}
}
int main()
{
	fraction f1, f2;
	scanf("%lld/%lld %lld/%lld", &f1.a, &f1.b, &f2.a, &f2.b);
	printFraction(reduction(f1));
	cout << " + ";
	printFraction(reduction(f2));
	cout << " = ";
	printFraction(add(f1, f2));
	cout << endl;
	printFraction(reduction(f1));
	cout << " - ";
	printFraction(reduction(f2));
	cout << " = ";
	printFraction(substract(f1, f2));
	cout << endl;
	printFraction(reduction(f1));
	cout << " * ";
	printFraction(reduction(f2));
	cout << " = ";
	printFraction(multiply(f1, f2));	
	cout << endl;
	printFraction(reduction(f1));
	cout << " / ";
	printFraction(reduction(f2));
	cout << " = ";
	if (f2.a != 0)
		printFraction(divide(f1, f2));
	else
		cout << "Inf";
	cout << endl;
	return 0;
}

你可能感兴趣的:(PAT)