Description Implement a simple calculator. All numbers used in calculations are integers. Operands given as input can be

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Description Implement a simple calculator. All numbers used in calculations are integers. Operands given as input can be

Post by answerhappygod »

Description
Implement a simple calculator. All numbers used in calculations are
integers.
Operands given as input can be added (add), subtracted (sub), or
multiplied (mult) to the current result.
The number of operands can range from 0 to 3, and it is assumed
that there are no more inputs.
For example, if result = 2 and add 1 2 is input, result = 2 + 1 + 2
= 5.
When the program starts, result = 0 is displayed.
Before inputting a command, it always shows the current
result.
The command to end the program is "exit", "bye" is printed and the
program ends.
If an incorrect command is entered, "wrong operation: {input
command}" is printed, and the operands entered after that are
ignored.
All operands are assumed to be valid integers. (For example, there
is no such input as add a seoul)
Input
A command in the form below is entered in one line.
operator [operand1] [operand2] [operand3]
Output
The current result value is output in the following format.
result={current_result}
Sample output:
result=0
add 3 5 -1 // input
result=7
sub -2 +5 7 // input
result=-3
mult 2 -1 5 // input
result=30
add 1 2 //
input
result=33
add
// input
result=33
sub 5
// input
result=28
mult -2 //
input
result=-56
adb 2 -1 //
input
wrong operation: adb
result=-56
sub -5
// input
result=-51
mult 2 2 5 // input
result=-1020
exit
// input
bye
plz use java
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply