POCALC1 - Ancient Pocket Calculator

no tags 

Adam likes pocket calculators, especially the early ones. As one of his favorite calculators is about 40 years old, he is not sure how long he will be able to use it. So he had the ingenious idea to develop a simulator that behaves exactly like his calculator. This simulator must be able to read a sequence of keystrokes from the calculator's keypad, process the appropriate calculations and print the calculator's output. As Adam needs only basic arithmetic, the following keys will be sufficient: digits 0 to 9, decimal point, operators +, -, x and : (for division), the equal sign for calculating and displaying a result and the [C] key for reseting the calculator and clearing the display, i.e. the display is set to "0.". Calculations are done from left to right without any operator precedence.

Calculator Display

You may call Adam's calculator a headstrong comtemporary, because of its special behaviour:
There is no invalid sequence of keystrokes. You can press arbitrary keys one after another, the calculator always knows how to handle it. If more than one operator key (including [=]) is pressed directly after another, only the last of these operators will be processed - all previous ones (in that continuous sequence) are ignored.
If more than 8 digit keys are pressed for the input of a single number, only the first 8 digits will be processed - all following digits are ignored. If the actual display value is zero, the typing of the zero key will have no effect, it's just ignored like successive keystrokes of the same operator. If a floating point value is typed in, a leading zero directly before the decimal point may be left out, but will be displayed just the same. If the decimal point key is pressed within a number that already has a decimal point typed in or if the input of a number (as a sequence of digit keys) is terminated by a decimal point, that has no effect.

Input

Input starts with a positive integer t (t<1000) in a single line, the number of testcases. Then t lines follow, each line giving the description of an arbitrary sequence of keystrokes on the calculator's keypad. Every key is enclosed by square brackets, all keystrokes are separated by a single space. The number of keystrokes per sequence is less than 500 and every sequence will be terminated by [=].

Output

For each sequence of keystrokes print the result the calculator will show on the display after the complete sequence of keystrokes has been processed. The size of the display is 8 digits plus an optional "-"-sign in front of the leftmost digit and a decimal point that will always appear, even if the result is an integer value. If a value has more than 8 decimal digits, it has to be rounded to fit into 8 digits. As the calculator's display is filled from right to left, the output has to be adjusted to the right.
If the absolute value of a number rounded to an integer needs more than 8 digits, scientific notation is used. Same case, if the absolute value of a number is larger than 10-100, but rounding to 8 digits would result in displaying zeros only. If the absolute value is not larger than 10-100, it results to zero.
In scientific notation a number is expressed as a product of a decimal part and a power of 10. The decimal part has always exactly one non-zero digit before the decimal point, an optional "-" sign in front of the leftmost digit and upto 4 digits after the decimal point, rounded if necessary. If the exponent is negative, a "-" follows, otherwise a space. Then follow two digits representing the exponent; a leading zero is shown in the exponent, if necessary.

Notice that there are two cases, where the calculator will display "Error." instead of showing a result. If a (final or interim) result has a rounded absolute value of at least 10100 or if you divide by zero. After an error has occured, all following keystrokes are ignored unless [C] is pressed.

For more clarity of the calculator's behaviour and the required input and output format please look at the examples below.

Example

Input:
12
[3] [+] [4] [x] [5] [=]
[1] [:] [6] [=]
[4] [.] [8] [-] [x] [+] [-] [+] [x] [-] [.] [=]
[4] [.] [8] [-] [x] [+] [-] [+] [x] [.] [=]
[+] [+] [+] [+] [+] [+] [1] [=] [=] [=]
[9] [8] [C] [-] [7] [6] [5] [4] [3] [2] [1] [0] [1] [2] [3] [4] [=]
[1] [=] [2] [=] [3] [=]
[5] [:] [9] [8] [7] [8] [9] [8] [9] [8] [7] [8] [8] [:] [4] [5] [6] [7] [8] [9] [=]
[-] [9] [9] [9] [9] [9] [9] [9] [9] [-] [-] [-] [-] [8] [8] [8] [8] [8] [8] [=]
[2] [.] [3] [.] [4] [.] [5] [=]
[0] [0] [0] [0] [0] [=]
[.] [:] [.] [=]
 
Output:
       35.
 0.1666667
       4.8
        0.
        1.
-76543210.
        3.
  1.108-13
-1.0089 08
     2.345
        0.
    Error.

hide comments
numerix: 2012-02-25 10:40:15

There shouldn't be any precision issues. Your code fails e.g. for this input:
[-] [5] [x] [9] [9] [9] [9] [9] [9] [9] [9] [x] [9] [9] [9] [9] [9] [9] [9] [9] [=]
According to your TLE Python submissions: You can get a Python solution AC even without psyco in less than 0.5 s.

Mitch Schwartz: 2012-02-25 10:40:10

How much should we care about rules of rounding when there is a "tie"? I've been using format strings with double precision, which might cause errors for tie-breaking, for example 0.00000005 will get rounded down to zero in both C and Python: http://ideone.com/oyIWS http://ideone.com/I6GmA. Is this critical for any test data? Should result of [.] [0] [0] [0] [0] [0] [0] [5] [x] [.] [1] [=] be " 0.0000001" or " 5.-08" or it doesn't matter?

@numerix Thanks for the replies! Obviously I'm a noob at Python optimisation, but it finally worked out. That's interesting about psyco.

Even with AC, I'm a little confused on constraint of 10^-100. I display value of 2e-100 as 0 but do not round internally, which seems strange.

Last edit: 2011-10-08 21:31:11
:D: 2012-02-25 10:40:20

If the interim absolute value is smaller or eq than 10^-100, should it be rounded to 0? Or is it just for printing values?

Also "(final or interim) result", does it mean any values after computation or just after [=] operator?

Re(numerix): "interim result" means value after every computation. And, yes, it has to be rounded to 0 according to the given constraints, not only for the printed result.

Last edit: 2011-03-17 14:26:23
:D: 2012-02-25 10:40:24

Ok, after trying this problems for a while a have few questions. What is the difference between [C] and [=]? I assume that [C] would erase the Error state, but [=] would preserve it, is that correct?

Re(numerix): Yes, that's what is says in the problem description.

Also about "rounded absolute value of at least 10^100". What is the tolerance here, does it mean values that would overflow when trying to print them? Is it the same for 10^-100?

Re(numerix): Whenever an operand (inclucing [=]) is typed in, the actual value at this time is evaluated and must hold the constraints. There shouldn't be any precision issues if you use doubles.

Last edit: 2011-03-15 16:30:31
:D: 2012-02-25 10:40:03

How do we treat a number that has 8 digits and a decimal point?

Re (numerix): You treat it as a 8 digit number with a decimal point ...
Maybe I'm missing something, but problem description and example seems clear to me according to that.

Re: Re: About ignoring digits above 8. Do we take '.' char into account when counting digits in number?

Re (numerix): 8 digits plus 1 decimal point plus an (optional) minus sign. This case is also part of the example.

Last edit: 2011-03-11 12:25:53

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