1. 程式人生 > >PAT 1088 Rational Arithmetic (20 分)

PAT 1088 Rational Arithmetic (20 分)

1088 Rational Arithmetic (20 分)

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




解析

可以去看看演算法筆記中怎麼處理分數的。PAT & MATH
這題程式碼量有點大,但是不算很難。
要用long long.因為題目都瘋狂暗示了:It is guaranteed that all the output integers are in the range of long int.





Code:

#include<cstdio>
#include<cstdlib>
using namespace std;
using INT64 = long long;
typedef struct {
	INT64 up, down;
}Fraction;
INT64 gcd(INT64 a, INT64 b) {
	if (b == 0)
		return a;
	else
		return gcd(b, a%b);
}
Fraction reduction(Fraction a) {
	if (a.down < 0) {
		a.up = -a.up;
		a.down = -a.down;
	}
	if (a.up == 0)
		a.down = 1;
	else {
		int d = gcd(abs(a.up), abs(a.down));
		a.up /= d;
		a.down /= d;
	}
	return a;
}
Fraction add(Fraction a, Fraction b) {
	Fraction c;
	c.up = a.up*b.down + b.up*a.down;
	c.down = a.down*b.down;
	return reduction(c);
}
Fraction sub(Fraction a, Fraction b) {
	Fraction c;
	c.up = a.up*b.down - a.down*b.up;
	c.down = a.down*b.down;
	return reduction(c);
}
Fraction muiti(Fraction a, Fraction b) {
	Fraction c;
	c.up = a.up*b.up;
	c.down = a.down*b.down;
	return reduction(c);
}
Fraction divide(Fraction a, Fraction b) {
	Fraction c;
	c.up = a.up*b.down;
	c.down = b.up*a.down;
	return reduction(c);
}
using F=Fraction(*)(Fraction a, Fraction b);
void show(Fraction a) {
	if (a.up < 0)
		printf("(");
	if (a.down == 1)
		printf("%lld", a.up);
	else if (abs(a.up) > a.down)
		printf("%lld %lld/%lld", a.up / a.down, abs(a.up) % a.down, a.down);
	else
		printf("%lld/%lld", a.up, a.down);
	if (a.up < 0)
		printf(")");
}
int main()
{
	Fraction f1, f2;
	scanf("%lld/%lld %lld/%lld", &(f1.up), &(f1.down), &(f2.up), &(f2.down));
	f1 = reduction(f1);
	f2 = reduction(f2);
	F functiontable[4]{ add,sub,muiti,divide };
	char SignTable[4]{ '+','-','*','/' };
	for (int i = 0; i < 4; i++) {
		show(f1);
		printf(" %c ", SignTable[i]);
		show(f2);
		printf(" = ");
		if (i == 3 && f2.up == 0)
			printf("Inf");
		else {
			Fraction result = functiontable[i](f1, f2);
			show(result);
		}
		printf("\n");
	}
	return 0;
}