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:
Rational(int numerator, int denominator)
:numerator
and denominator
to represent the rational number.Rational Add(Rational const& b) const
:Rational
object b
and returns a new Rational
object that represents the sum of the two rational numbers.Rational Sub(Rational const& b) const
:Rational
object b
and returns a new Rational
object that represents the subtraction of the two rational numbers.Rational Mul(Rational const& b) const
:Rational
object b
and returns a new Rational
object that represents the multiplication of the two rational numbers.Rational Div(Rational const& b) const
:Rational
object b
and returns a new Rational
object that represents the division of the two rational numbers.void print(std::ostream& os) const
: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.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}\)
/
, the second rational number is not 0.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}\)