14246 - I Like to be Rational   

Description

Dogeon (鴿子) likes everything to be rational, just like himself. He thinks that it is irrational that floating-point numbers in C/C++ are not accurate. So he decides to implement a class Rational to represent rational numbers. The class Rational should have the following member functions:

  1. Rational(int numerator, int denominator):
    a constructor that takes two integers numerator and denominator to represent the rational number.
  2. Rational Add(Rational const& b) const:
    a member function that takes another Rational object b and returns a new Rational object that represents the sum of the two rational numbers.
  3. Rational Sub(Rational const& b) const:
    a member function that takes another Rational object b and returns a new Rational object that represents the subtraction of the two rational numbers.
  4. Rational Mul(Rational const& b) const:
    a member function that takes another Rational object b and returns a new Rational object that represents the multiplication of the two rational numbers.
  5. Rational Div(Rational const& b) const:
    a member function that takes another Rational object b and returns a new Rational object that represents the division of the two rational numbers.
  6. void print(std::ostream& os) const:
    a member function that prints the rational number in the simplest form numerator/denominator to the output stream os. The simplest form means that the numerator and the denominator have no common divisor other than 1. Also, the denominator should be positive.
  • This is a partial judge problem. All you have to do is to implement the functions mentioned above.
  • This is a work of fiction. Any resemblance to actual persons, living or dead, or actual events is purely coincidental.

Input

The first line of the input contains an integer \(N\), the number of expressions to be evaluated.

The next \(N\) lines each contain the expression to be evaluated. Each expression contains two rational numbers \(\frac{n_{i,1}}{d_{i,1}}, \frac{n_{i,2}}{d_{i,2}}\) and an operator \(op_i\), separated by a space. The rational numbers are in the form numerator/denominator and the operator is one of +, -, *, /.

\(N\)
\(n_{1,1}/d_{1,1} \quad op_1 \quad n_{1,2}/d_{1,2}\)
\(n_{2,1}/d_{2,1} \quad op_2 \quad n_{2,2}/d_{2,2}\)
\(\vdots\)
\(n_{N,1}/d_{N,1} \quad op_N \quad n_{N,2}/d_{N,2}\)

Constraints

  • \(1 \leq N \leq 10^5\)
  • \(-10^9 \leq n_{i,1}, d_{i,1}, n_{i,2}, d_{i,2} \leq 10^9\)
  • It is guaranteed that the denominator of the rational numbers is not 0.
  • It is guaranteed that if operator is /, the second rational number is not 0.
  • It is guaranteed that the simplest form of the result of each expression has a numerator and a denominator that are both within \([-10^9, 10^9]\).

Output

Each line of the output should contain the result of the corresponding expression in the simplest form \(n_{i,3}/d_{i,3}\).

$\frac{n_{i,3}}{d_{i,3}} = (\frac{n_{i,1}}{d_{i,1}} \, \mathrm{op_i} \, \frac{n_{i,2}}{d_{i,2}})$, $d_{i,3} > 0$ and $\mathrm{gcd}(n_{i,3}, d_{i,3}) = 1$.

\(n_{1,3}/d_{1,3}\)
\(n_{2,3}/d_{2,3}\)
\(\vdots\)
\(n_{N,3}/d_{N,3}\)

Sample Input  Download

Sample Output  Download

Partial Judge Code

14246.cpp

Partial Judge Header

14246.h

Tags

Dogeon



Discuss