PAT Advanced 1088 Rational Arithmetic: GCD and Simulation

Overview

1088 Rational Arithmetic problem from PAT advanced is to test how to get the greatest common divisor (gcd) and simulate the process of the rational arithmetics.

PAT Advanced 1088 Rational Arithmetic

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)

Solution Analysis: GCD and Simulation

Well, the problem is not a difficult one, to test points:

  1. use Euclidean algorithm to get the gcd between the numerators and the denominators.
  2. better use long long in your c++ program, otherwise, you get WA every time. And I explain the issue about long long type which is used in the following code rather than the long int type which is mentioned in this problem in this post for your reference
  3. Simulate the +, -, *, / between rational numbers.

The following source code is accepted by the PAT OJ to pass this 1088 Rational Arithmetic problem:

#include <cstdio>
#include <string>
#include <algorithm>

class RationalNumber {
public:
	RationalNumber() : negative(false), numerator(0), denoenator(1) {}
	RationalNumber(long long a, long long b) : negative(false), numerator(a), denoenator(b) {
		if (a < 0) { negative = true; numerator = -numerator; }
		simplify();
	}
	static long long gcd(long long a, long long b) {
		if (a < b) std::swap(a, b);
		if (0 == b) return a;
		return gcd(b, a % b);
	}
	void toString() {
		long long intPart = numerator / denoenator;
		long long floPart = numerator % denoenator;

		if (!intPart && !floPart) { printf("0"); return; }
		if (negative) printf("(-");
		if (intPart)  printf("%lld", intPart);
		if (floPart)  {
			if (intPart) printf(" ");
			printf("%lld/%lld", floPart, denoenator);
		}
		if (negative) printf(")");
	}
private:
	void simplify() {
		long long gcdValue = RationalNumber::gcd(numerator, denoenator);
		numerator  /= gcdValue;
		denoenator /= gcdValue;
	}
public:
	long long numerator;
	long long denoenator;
	bool negative;
};

RationalNumber getSum(const RationalNumber &lhs, const RationalNumber &rhs) {
	long long deno = lhs.denoenator * rhs.denoenator;
	long long nume = lhs.numerator * rhs.denoenator + rhs.numerator * lhs.denoenator;
	RationalNumber ret(nume, deno);
	return ret;
}

RationalNumber getDifferece(const RationalNumber &lhs, const RationalNumber &rhs) {
	long long deno = lhs.denoenator * rhs.denoenator;
	long long nume = lhs.numerator * rhs.denoenator - rhs.numerator * lhs.denoenator;
	RationalNumber ret(nume, deno);
	return ret;
}

RationalNumber operator+(const RationalNumber &lhs, const RationalNumber &rhs) {
	if ( (lhs.negative && rhs.negative) || (!lhs.negative && !rhs.negative) ) {
		RationalNumber ret = getSum(lhs, rhs);
		ret.negative = lhs.negative;
		return ret;
	}
	if (rhs.negative && !lhs.negative) {
		RationalNumber ret = getDifferece(lhs, rhs);
		return ret;
	}
	RationalNumber ret = getDifferece(rhs, lhs);
	return ret;
}

RationalNumber operator-(const RationalNumber &lhs, const RationalNumber &rhs) {
	if ( lhs.negative && !rhs.negative ) {
		RationalNumber ret = getSum(lhs, rhs);
		ret.negative = lhs.negative;
		return ret;
	}
	if ( !lhs.negative && rhs.negative ) {
		RationalNumber ret = getSum(lhs, rhs);
		return ret;
	}
	if (lhs.negative && rhs.negative) {
		RationalNumber ret = getDifferece(rhs, lhs);
		return ret;
	}
	RationalNumber ret = getDifferece(lhs, rhs);
	return ret;
}

RationalNumber operator*(const RationalNumber &lhs, const RationalNumber &rhs) {
	RationalNumber ret(lhs.numerator * rhs.numerator, lhs.denoenator * rhs.denoenator);
	if ( (lhs.negative && rhs.negative) || (!lhs.negative && !rhs.negative) )
		return ret;
	ret.negative = true;
	return ret;
}

RationalNumber operator/(const RationalNumber &lhs, const RationalNumber &rhs) {
	RationalNumber ret(lhs.numerator * rhs.denoenator, lhs.denoenator * rhs.numerator);
	if ( (lhs.negative && rhs.negative) || (!lhs.negative && !rhs.negative) )
		return ret;
	ret.negative = true;
	return ret;
}
int main() {
	long long n1, d1, n2, d2;
	scanf("%lld/%lld %lld/%lld", &n1, &d1, &n2, &d2);
	RationalNumber l(n1, d1);
	RationalNumber r(n2, d2);

	RationalNumber result = l + r;
	l.toString(), printf(" + "), r.toString(), printf(" = "), result.toString(), printf("\n");
	result = l - r;
	l.toString(), printf(" - "), r.toString(), printf(" = "), result.toString(), printf("\n");
	result = l * r;
	l.toString(), printf(" * "), r.toString(), printf(" = "), result.toString(), printf("\n");

	l.toString(), printf(" / "), r.toString(), printf(" = ");
	if (n2 == 0)
		printf("Inf\n");
	else {
		result = l / r;
		result.toString(), printf("\n");
	}
	return 0;
}

Summary

1088 Rational Arithmetic problem from PAT advanced is to test how to get the greatest common divisor (gcd) and simulate the process of the rational arithmetics.

Written on December 10, 2014