P1572 Compute Fractions
Description
Csh was locked at home by his mom to do fraction exercises, but he clearly does not want to do so many complicated calculations. Besides, Xxq is waiting for him at the door to go watch a movie together. To join Xxq for the movie as soon as possible, he hands the remaining exercises to you. Can you help him solve the problem?
Input Format
The input consists of a single line: a fraction expression.
The expression contains only digits, `+`, `-`, and `/`. Here `/` is the fraction bar: the numerator is on the left and the denominator is on the right. The input is guaranteed to contain no complex (nested) fractions. If the first term of the expression is positive, there is no leading `+`; if it is negative, there is a leading `-`.
All integers appear in fraction form.
Output Format
Output one line: the final result. If the result is an integer, print it as an integer; otherwise, print it as a fraction in lowest terms.
It is guaranteed that all numbers in the answer (if the answer is a fraction, that is, the numerator and denominator) are within the range of $32$-bit signed integers.
Explanation/Hint
### Constraints and Conventions
For all test points, the length of the input expression is within 100, and the numerator and denominator are within 1000. It is also guaranteed that if you evaluate the sum or difference of the fractions from left to right and reduce immediately, the intermediate results will not exceed the range of int.
Note that the input fractions are not necessarily in lowest terms.
------------
2024/2/13: Added 2 sets of hack testdata.
Translated by ChatGPT 5