#C3707. Simplify Fractional Expressions
Simplify Fractional Expressions
Simplify Fractional Expressions
In this problem, you are given a mathematical expression that contains fractions and basic arithmetic operators such as addition (+), subtraction (-), multiplication (*), and division (/). Your task is to evaluate the expression and simplify the result to its irreducible fraction form. If there is any division by zero during the evaluation, output the string "undefined".
For example, one of the sample expressions is (\frac{3}{4} + \frac{2}{3}), and the correct simplified result is (\frac{17}{12}). Note that an expression like "1/2 + 3" should be interpreted as (\frac{1}{2} + \frac{3}{1}) and simplified accordingly. The evaluation must follow the standard operator precedence: multiplication and division have higher precedence than addition and subtraction.
The input consists of several lines, each containing one expression. The input terminates with a line containing a single zero ("0"), which should not be processed.
inputFormat
The input is read from standard input (stdin) and consists of one or more lines. Each line contains a valid arithmetic expression involving fractions (in the form a/b, where a and b are integers) and/or integers, with operators +, -, *, and /. The last line of input will be a single zero ("0") which indicates the end of the test cases. You should process all lines before the terminating zero.
outputFormat
For each expression (except the terminating line), output the simplified result on a separate line. The result should be in the form of an irreducible fraction (e.g. 17/12) or an integer if the denominator is 1. If a division by zero occurs in the evaluation, output "undefined".## sample
3/4 + 2/3
5/6 * 3/4
1/2 + 3
0
17/12
5/8
7/2
</p>