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. Since he prefer the class to be over-rational to use, you're asked to help him implement the following functions in an overloaded operator style.
Rational(int numerator, int denominator)
:numerator
and denominator
to represent the rational number.Rational operator+(Rational const& b) const
:+
that takes another Rational
object b
and returns a new Rational
object that represents the sum of the two rational numbers.Rational operator-(Rational const& b) const
:-
that takes another Rational
object b
and returns a new Rational
object that represents the subtraction of the two rational numbers.Rational operator*(Rational const& b) const
:*
that takes another Rational
object b
and returns a new Rational
object that represents the multiplication of the two rational numbers.Rational operator/(Rational const& b) const
:/
that takes another Rational
object b
and returns a new Rational
object that represents the division of the two rational numbers.std::ostream& operator<<(std::ostream& os, Rational const& r)
:<<
operator of std::ostream
to output the rational number in the form numerator/denominator
.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}$