Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment you will be creating a simple programmer’s stack calculator.

ID: 3628791 • Letter: I

Question

In this assignment you will be creating a simple programmer’s stack calculator. 

You must have a main function, a parse function, a displayValues function, a push function and a pop function. You must use switch case statements. All variables must be declared inside functions…that means no static variables because one of the objectives of this assignment is passing data to functions and returning results and static (i.e. global) variables defeat this.

Main

Declare and initialize needed variables
Loop
Call parse function which returns an operation code or a value (or both)
switch on the operation code
case value:
Push the value onto the stack
case add
Push(Pop from stack + Pop from stack)
endswitch
displayValues
endLoop

displayValues
Print stack array[0]
Print stack array[1]
Print stack array[2]

Print stack array[sp]
Note: if sp = -1 you can display “Stack Empty”

Parse
Read in a string (we suggest using fgets)
Examine first character
if it is a valid operation symbol
return the appropriate operation code
else
Convert string to number (Hint: Check out strtol)
return number as well as operation code indicating a number is being returned
Note: The idea here is that parse must sometimes return two values and we want one to be the normal return value and the other to be returned through a parameter.

Basic Operations
Your calculator must support the following basic operations:
+ add
- subtract
* multiply
/ divide
h hex
o octal
d decimal
c change sign
& bitwise and
| bitwise or
^ bitwise xor
~ complement
> right shift

Note: the above is based on idea of single character operation codes

Negative numbers are entered by entering the number part and then enter a ‘c’ for change sign. If you try and enter a negative number the negative sign should be interpreted as subtract.

Explanation / Answer