Machine Problem 3
22C:18, Fall 1996
Due Thursday Oct. 31, 1995, at 5:00 PM
Douglas W. Jones
Write a SMAL Hawk program to emulate an RPN calculator.
Each character on the line of input has meaning as an instruction to the
calculator. Your main program should repeatedly read lines of input from
the keyboard and perform the operations indicated by that line, starting with
an empty stack.
Characters on each input line are scanned left to right; each character
has meaning, either requesting a computation or requesting output, as follows:
- 0-9 -- the digits:
- The item on the stack top is multiplied by 10 and then the corresponding
integer is added to the stack top. If the top item is initially zero,
the result of reading a string of digits is to enter the corresponding
number.
- +-*/ -- the operators:
- The two topmost items on the stack are popped, combined as indicated by
the operator symbol, and then the result is pushed. In the case of
subtraction and division, the top item on the stack is subtracted from
or divided into the item below it.
- E -- the enter operation:
- A zero is pushed onto the stack.
- P -- the print operation:
- The top item on the stack is popped off the stack and printed, as
a decimal number.
- B -- the odd-base print operation:
- The top item on the stack is used as the number base for printing the
item below it on the stack. Both are then popped. An error message should
be printed if the number base cannot be supported.
- _ -- space
- Spaces in the input are ignored.
Here is an example of the operation of this calculator for one line of input:
Input: E12E6-P E12E7E6-*E12B E160E2*E10/P
Output: 6
Output: 10
Output: 32
Your program should detect stack underflow and print an error
message in that case. Your program need not process input lines longer
than 80 characters. Your program should use a procedure that prints output
in any number base, possibly a procedure based on your solution to MP1.
Until terminal input is fully supported (there are a few problems), please
work with a simplified main program that uses a line of input coded into
an ASCII directive, for example:
LINE: ASCII "E12E6-P E12E7E6-*E12B E160E2*E10/P",0
E-mail will be sent out when a good input routine is available.