Ask the user to input a string of expression in RPN form (+ - * / ), use a stack to evaluate the
result and display the result (in decimal) on screen. The numbers and operations in the RPN
expression are assumed to be separated by (one or more) spaces. Assuming no negative
numbers in the input (but intermediate results might be negative).
Calculate single-digit RPN in valid forms. For example:
3 4 – 2 7 + * 4 / = -2 is a valid RPN
Allowing multi-digit numbers, and detecting illegal RPN
expressions. For example:
3 100 105 - + 433 * = -866 is a valid RPN
3 100 105 - + * 433 is invalid
3 abc 105 - + 433 * is invalid
Extra Goal a) (20 pts): achieving the above levels for both RPN and PN. For example:
3 100 105 - + 433 * = -866 is a valid RPN
- 120 / * 3 12 - 42 36 = 114 is a valid PN
- 120 3 100 105 - + 433 * is invalid
b) (20 pts): support of real numbers:
0.3 1.02 1.05 - + 3 * = -0.99 is a valid RPN
:
First, you need to prepare a stack structure that supports push / pop, etc.
a) From left to right, go through every character of the expression, do the following:
i. if the character is a digit (0 – 9):
convert the ASCII to integer and push that number onto stack
ii. if the character is an operation (+ - * / )
1. pop from the stack a number -> A
2. pop from the stack a number -> B
3. calculate B op A ( for /, use quotient as result and ignore remainder).
4. push the result number back onto stack
b) Pop out from stack -> final result