PROBLEM STATEMENT
You are to write a class Calculator that simulates a real calculator supporting
the four most common arithmetic operations. The specific model that you are
simulating has the following 15 buttons: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ., +, -,
*, /
Notice that this calculator does not have a '=' button! If you want to perform
a calculation you need to terminate it with an operation. For example, "1+1+",
instead of, "1+1=". The last '+' can be also '-', '*' or '/' without having any
impact on the computation. See the examples section below.
Create a class Calculator that contains a method getResult. getResult takes a
String that is a key sequence and returns the contents of the display after the
last key was pressed.
DEFINITION
Class name: Calculator
Method name: getResult
Parameters: String
Returns: String
Method signature (make sure your method is public):
String getResult (String s);
NOTES
1. The LCD display of the calculator can hold up to 8 digits and a decimal
point. As in most real calculators the decimal point is always shown (123 shows
as "123."). In case the integer part of a result cannot fit in the screen,
return "-ERROR-". If the entire fractional part of a result cannot fit on the
display, show as many digits as you can, without doing any sort of rounding
(e.g. 0.123456789 show as 0.1234567). Note that this applies to the results of
all computations, even intermediate ones, as they need to be shown on the
screen (see example 8 below).
2. If an input number is too big to fit on the screen, "-ERROR-" should be
returned. Two such cases are "1234567890" and "1234.567890". Keep in mind that
while processing "000000000123" the calculator will discard the leading zeros
(see 6b below), so no more than 3 digits will be on the screen at any given
time, thus no "-ERROR-" is returned. Rather, the display should read "123."
after this sequence is keyed in. On the other extreme, if we try to input
"1.00000000" the number will not be able to fit onscreen after the 7th zero, so
"-ERROR-" should be returned.
3. This specific model does not support negative numbers. In case you need to
show a negative number on the display at any point of the key sequence, you
should return "-ERROR-", discarding the rest of the sequence.
4. Typing a number is done using the digit keys and the '.'. When an operation
key is pressed ('+', '-', '*' or '/') the number is sealed, meaning you can't
continue typing it.
Example: "123.456+789"
Meaning: The first 7 keys type in the number 123.456. The '+' seals it.
After
that 789 does not continue that first number but rather starts a new
one, leaving "789." on the screen at the end.
5. The calculator has 3 dedicated registers:
a) D - the display contents (a number), initially 0 (zero) and sealed
b) R - the last result (a number), initially 0 (zero)
c) Op - the last operation requested ('+', '-', '*' or '/'), initially '+'
Note that because the display is sealed initially, although it shows "0.",
pressing any number key (say 5) will throw the "0" out and show "5."
6. Here is what happens when a key K is pressed:
a) K == '+', '-', '*', '/'
if (previous key pressed is not '+', '-', '*' or '/')
{
D = R Op D; // apply Op to arguments R, D; show result on display
R = D; // copy display to R
seal the number on display, on error show "-ERROR-" and abort
}
Op = K; // update last operation requested
b) K == '0' ... '9', '.'
The only thing to consider would be whether the number on display is
sealed or not. If so, just start a new number. Note that you can start
a number with '.', which is equivalent to "0." (see examples 3, 8,...).
Also a number on display should never have leading zeros in the integer
part (e.g. "00123" shows as "123.").
Further, '.' can be pressed multiple times while a single number is
typed, but only the first time matters (e.g. "12.34.56" is "12.3456").
Note: As you can see from (a) above, pressing multiple operations has the
same effect as if only the last one was pressed (see first example
below).
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- s is between 0 and 50 characters, inclusive
- s consists only of the 15 characters "0123456789.+-*/" (quotes are for
clarity and are not a valid character)
EXAMPLES
Sequence: "2-/+*3+"
- When '2' is pressed the display (D) updates to "2.".
- The '-' that follows performs D = R Op D = 0 + 2 = 2 and then R = 2.
The "2." is sealed on the display. Op gets updated to '-'.
- The following '/', '+' and '*' just update Op, leaving it with a value of '*'
at the end.
- Pressing '3' now updates D to "3.".
- The following '+' performs D = R Op D = 2 * 3 = 6 and then R = 6.
The "6." is sealed and Op is updated to '+'.
So your method should return "6." as a result.
Here are some more examples:
Sequence: Display: Sequence: Display:
1. "1234" "1234." 6. "2+2" "2."
2. "1.23" "1.23" 7. "2+2+" "4."
3. ".123" "0.123" 8. "1000/3+0.000008+" "333.33333"
4. "0.00" "0.00" 9. ".1*.1+" "0.01"
5. "0.00+" "0." 10. "4/3-1+" "0.3333333"
11. ".1+.01+" "0.11" 16. "1*-/+1*" "2."
12. ".1-.01*" "0.09" 17. "888888881" "-ERROR-"
13. ".1*.1+0.99-" "1." 18. "000000123" "123."
14. ".0001*.001+" "0.0000001" 19. "99999*99999*" "-ERROR-"
15. ".0001/1000+" "0.0000001" 20. "1-2+3*" "-ERROR-"
21. "*4+1-" "1." 26. "12.0000000" "-ERROR-"
22. "/-//8+2" "2." 27. "0012.000001" "12.000001"
23. "33+++33*" "66." 28. "012.0000001" "-ERROR-"
24. "3/*++5--8+" "0." 29. "000000000.1" "0.1"
25. "+3+4+" "7." 30. "192.168.0.1" "192.16801"