spoj#POCALC2. Modern Pocket Calculator
Modern Pocket Calculator
当前没有测试数据。
Adam likes pocket calculators, not only the early ones, but also the modern ones with a two line LCD-display and mathematically correct operator precedence. The upper (input-)line shows the expression you typed in, the (output-)line below shows the result immediately after the [=] key has been pressed. Given the calculator's input-line, the program's task is to produce its output-line. Using the calculator's [S-D] key, the display switches between fractional and decimal representation of the result, so output must contain both representations.
Input
Input starts with a positive integer t (t<1000) in a single line, then t testcases follow. Every testcase consists of either three lines or a single line, representing the expression in the calculator's input-line, followed by a blank line. An expression contains n numbers (0 < n < 10) with exactly one operator (+, -, x, :) between any two numbers and exactly one space to separate number and operator. There will be no invalid expression and no undefined operation.
A number is given as a decimal, a fraction or a mixed number and will be non-negative. If a number is positive, its decimal value is between 10-9 and 109, numerator and denominator are also non-negative and not larger than 109 each, given decimals have at most 9 digits overall. These constraints also hold for all calculations, if done properly. The length of the fraction bar depends on the maximum length of the numerator or the denominator respectively. If lengths of numerator and denominator are different, the shorter one will be centered based on the fraction bar. If centering isn't possible, it is set one digit to the right.
Output
For each testcase print the result the calculator will display in its output-line: first fractional, then decimal, both representations separated by " [S-D] ". If a result is negative, the negative sign has to be printed directly (i.e. no space) in front of the integer part or the fraction bar.
The fractional representation has to be printed in lowest terms either as a proper fraction or mixed number. If the number has an integer representation, that has to be printed instead. Numerator and denominator have to be placed as described in the input section.
After " [S-D] " the exact(!) decimal represenation has to be printed. As the screenshot shows, the calculator is even able to display a repeating decimal in its decimal representation using a vinculum, so that's what the program has to do as well, using underscores in the line above. You can assume that no decimal expansion is longer than 100 digits (it's a calculator with XXL-display).
The number of lines for every output depends on the result. It may be three lines (if fractions appear) or a single line (if result is an integer). Print a blank line after every testcase except the last one. Be careful not to print any trailing spaces.
For more clarity of input and output format please look at the examples below.
Example
Input:
5
9 50
2-- + 0.26 x --
11 15
4 9
5.88 - -- : -
18 5
3 - 5.125
9 + 14
1
--- x 0.5
100
Output:
113 __
3--- [S-D] 3.684
165
1532 _________
5---- [S-D] 5.75654320987
2025
1
-2- [S-D] -2.125
8
23 [S-D] 23
1
--- [S-D] 0.005
200