#K64297. Advanced Calculator Simulation
Advanced Calculator Simulation
Advanced Calculator Simulation
You are asked to implement an advanced calculator that supports several arithmetic operations using operator overloading (or equivalent method implementations in languages that do not support operator overloading). The calculator maintains an internal state value
and an operation history. It supports the following operations:
- Addition: updating the state by performing $a+b$.
- Subtraction: updating the state by performing $a-b$.
- Multiplication: updating the state by performing $a\times b$.
- Division: updating the state by performing $a\div b$. Division by zero is invalid and should output an error message.
- Sum: compute the sum of a list of numbers, record the operation in the history (the state remains unchanged).
- Product: compute the product of a list of numbers, record the operation in the history (the state remains unchanged).
The calculator records a history message for each operation performed. For example, after adding 5 to a value of 10, the history should record "Added 5: 15". At the end of the simulation, when a specific command is issued, the final value and the complete history are output.
inputFormat
The input is provided via stdin and consists of multiple lines:
- The first line contains a number representing the initial value of the calculator.
- Each subsequent line represents a command. The commands can be:
+ x
: Add number x to the current value.- x
: Subtract number x from the current value.* x
: Multiply the current value by x./ x
: Divide the current value by x. If x is 0, output the error messageCannot divide by zero
immediately and stop processing further commands.sum n1 n2 ... nk
: Calculate the sum of the list of numbers [n1, n2, ..., nk] and record the operation in the history.product n1 n2 ... nk
: Calculate the product of the list of numbers [n1, n2, ..., nk] and record the operation in the history.history
: This command indicates the end of input. Upon reading this command, your program should output the final value and the complete history of operations.- All numeric values can be integers or floating-point numbers.
outputFormat
If all operations are executed without errors, print the final calculator value on the first line. Then, for each operation performed (in the order they occurred), print the corresponding history record on separate lines.
If a division by zero is attempted at any point, immediately output the exact string Cannot divide by zero
and terminate the program without further output.
10
+ 5
history
15
Added 5: 15
</p>