Class for working with fractions

PHP Medium classes

Task «Class for working with fractions»

Create a class Fraction for working with ordinary fractions. The class must have methods for adding, subtracting, multiplying, and dividing fractions. A fraction is defined by a numerator and a denominator (integers, denominator is not zero). The result of each operation must be reduced to its simplest form. If the denominator is negative, move the minus sign to the numerator. Output the fraction in the format 'numerator/denominator'.

Input Format

First line: integer N — the number of operations. Each of the next N lines contains an operation in the format: 'number1/denominator1 operation number2/denominator2', where the operation is: +, -, *, /.

Output Format

For each operation, output the result as an irreducible fraction 'numerator/denominator'.

Examples

Example 1

INPUT
3 1/2 + 1/3 5/6 - 1/2 2/3 * 3/4
OUTPUT
5/6 1/3 1/2

Example 2

INPUT
2 2/5 + 3/10 7/8 - 1/4
OUTPUT
7/10 5/8

Example 3

INPUT
5 1 2 3 4 5
OUTPUT
2 4 4 16 6
main.php