Read a rational number (2) P93157


Statement
 

pdf   zip   main.cc

thehtml

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

1 ≤ @num@ ≤ 109 and 1 ≤ @den@ ≤ 109

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

Information
Author
Salvador Roura
Language
English
Translator
Salvador Roura
Original language
Catalan
Other languages
Catalan
Official solutions
C++
User solutions
C++