POCALC2 - 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.

Display left Display right

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

hide comments
Simes: 2019-11-04 20:02:54

Here are a couple of test cases with 88 and 96 repeating digits
475 : 617
1 : 97

Last edit: 2019-12-31 08:23:34
Petar Bosnjak: 2015-05-28 00:13:59

I don't understand , do real numbers have at most 9 or 100 decimal digits?

Nikola Samardzic: 2015-05-26 18:26:39

1 -1
if the result is -2--- should we print it like that or like 2---- ?
2 2

Bartek Dudek: 2015-05-22 23:24:26

@redmoon: maybe that will help:
" If a number is positive, its decimal value is between 10^-9 and 10^9, numerator and denominator are also non-negative and not larger than 10^9 each, given decimals have at most 9 digits overall. These constraints also hold for all calculations, if done properly"

redmoon: 2015-05-22 11:49:58

Can the final result or mid calculation exceed 10^18?

Last edit: 2015-05-22 11:52:20
jkelava6: 2015-05-17 12:27:34

jkelava6: (edit) 2015-05-17 12:27:34
Cathced some bugs!

Message from numerix: Your AC submission is identical to a former AC submission by another user. So, I disqualified your AC submission. If you have a good explanation for that submission, let me know by PM via SPOJ forum.

EDIT(jkelava6): Exactly. We are in the same Bubble Cup team, and we have worked together on this problem.
EDIT(jkelava6): Also, past simple of verb catch is "caught" :)

Reply (numerix): Feel free to resubmit your solution again. Your submission lists show, that both of you worked on that problem.
BUT (as I see it): If you have solved a problem as a team, you should also submit it as a team, i.e. have a team account and submit it only once.

Last edit: 2015-05-19 21:24:19
Przemysław Perkowski: 2015-05-05 00:34:34

if the result is 1/63, should the program print 0.(015873) or 0.0(158730)?

Reply (numerix): First representation is correct.

Last edit: 2015-05-15 17:52:56
Nikola Samardzic: 2015-05-02 10:22:55

Are the numerators and denominators of fractions in input necessarily integers?

Reply (numerix): Of course.

Last edit: 2015-05-15 17:53:10

Added by:numerix
Date:2011-03-12
Time limit:0.200s
Source limit:10000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:own problem