Read a rational number (2)

Write a procedure

    bool read_rational(int& num, int& den);

that tries to read a rational number given in the form “numerator”/“denominator”, and stores these two values in @num@ and @den@, respectively. Additionally, you must remove all common factors from @num@ and @den@. Furthermore, if the input had a rational number, you must return true; otherwise you must returna false. For instance, this code

  int num, den;
  while (read_rational(num, den)) cout << num << ' ' << den << endl;

with this input

66/12
100/100

should print

11 2
1 1

Precondition

11 \le @num@ 109\le 10^9 and 11 \le @den@ 109\le 10^9

Observation

You only need to submit the required procedure; your main program will be ignored.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T12:00:59.693Z

© Jutge.org, 2006–2026.
https://jutge.org