Fractions (2) X77323


Statement
 

pdf   zip

Write a program that reads a fraction (as two integers) followed by a sequence of artimethic operations on fractions, and prints the final resulting fraction simplified.

The input consists of two integers (numerator and denominator of initial fraction) followed by a sequence of zero or more triplets (str, int, int). The string is one of ’+’, ’-’, ’*’, ’/’, and the int’s correspond to the numerator and denominator of the fraction to be operated. Denominators are always strictly positive.

The program will accumulate each operation on the result of the previous. For instance:

3 4   # initial value is 3/4
+ 1 2  # add 1/2 to 3/4 (accumulated is 5/4)
* 3 2  # multiply 5/4 by 3/2 (accumulated is 15/8)
- 1 4  # substract 1/4 from 15/8 (accumulated is 13/8)

when the triplet sequence ends, the program prints the final simplified fraction (13/8 in this example).

Input

The input consists of two integers, followed by a sequence of zero or more triplets (operation, numerator, denominator). Denominators are always strictly positive.

Output

The output is the final simplified fraction resulting from accumulating all operations in the input sequence. Denominator must be strictly positive.

Public test cases
  • Input

    3 4
    + 1 2  
    * 3 2  
    - 1 4 

    Output

    13/8
    
  • Input

    50 125

    Output

    2/5
    
  • Input

    5 7
    * 4 3
    - 12 26
    / -2 31
    * 1 2
    + -17 10
    + 3418 1365

    Output

    -3/1
    
  • Input

    3 4
    + 2 6
    / 8 12
    - 13 8

    Output

    0/1
    
  • Information
    Author
    Lluís Padró
    Language
    English
    Official solutions
    Python
    User solutions
    Python