answersLogoWhite

0

A simple calculator works strictly left to right:

  1. get a number as the result of the last operation (if there is no number make it zero)
  2. make the result of the last operation the current number
  3. get the next operator
  4. if there is no next operator or it is equals (=) go to step 8
  5. get the next number
  6. do the operator on the current number and the next number
  7. go to step 2
  8. print the result of the last operator
  9. stop
You need to:
  • decide what to do if two operators are found together without an intervening number;
  • decide how to detect a unary minus (ie when a '-' is found, is it a binary "number - number", or a unary "negate following number"
  • how to handle the numbers - work with integers, fixed point or floating point, etc.
  • decode the operator and do the relevant operation on the stored numbers.

-----------------------------------

A reverse polish calculator is actually much easier to code (than a simple calculator), but allows the user to specify precedence of operations (like a scientific calculator) or to use it as a simple calculator (left-to-right):

  1. Initialise the stack
  2. get an object
  3. if no more objects go to step 12
  4. if the object is a number go to step 9
  5. if the are not at least 2 numbers on the stack error
  6. do the operation on under and top (eg under ÷ top)
  7. remove the top two numbers and push the result on the stack
  8. repeat from step 2
  9. if stack full error
  10. push the number on the stack
  11. repeat form step 2
  12. If stack empty push 0 onto the stack
  13. print top number on stack
  14. clear the stack
  15. stop

A simple calculator would evaluate: "1 + 3 x 2" as "(1 + 3) x 2" = 4 x 2 = 8

The reverse polish calculator would have the same calculation entered as "1 3 + 2 x"

User Avatar

Wiki User

7y ago

Still curious? Ask our experts.

Chat with our AI personalities

ProfessorProfessor
I will give you the most educated answer.
Chat with Professor
LaoLao
The path is yours to walk; I am only here to hold up a mirror.
Chat with Lao
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
More answers

The answer depends on algorithm for carrying out which operation!

User Avatar

Wiki User

7y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What is the algorithm for simple calculator?
Write your answer...
Submit
Still have questions?
magnify glass
imp