F008B. Addition of fractions P93090


Statement
 

pdf   zip

thehtml

Your task is to write a program that computes the result of adding a sequence of fractions.

Using the definition

struct Fraction { int num, den; // always strictly positive };

your program must include and use the function

Fraction addition(const Fraction& x, const Fraction& y);

that returns the addition of |x| and |y|, without common factors in the numerator and denominator.

Input

The input is a sequence of one or more simplified fractions separated by plus signs, ended with an equal sign. Each fraction consists of its numerator, a bar, and its denominator. Numerators and denominators are natural strictly positive.

Output

Your program must print the simplified fraction corresponding to the sum of all the given fractions.

Observations

  • In order to avoid overflows, use the function |addition()| to accumulate the partial calculations.
  • Inefficient calculation of the greatest common divisor will be negatively valued.
  • Using vectors is not allowed to solve this problem.
Public test cases
  • Input

    1/2 + 1/2 =
    

    Output

    1/1
    
  • Input

    1/2 + 2/3 + 3/4 + 4/5 + 5/6 =
    

    Output

    71/20
    
  • Input

    1/10125 + 1/8000 + 1000/999 = 
    

    Output

    4801073/4795200
    
  • Input

    9/4 =
    

    Output

    9/4
    
  • Information
    Author
    Professorat de P1
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++