Starting from:
$30

$24

Assignment #5 SOlution

(20 points) In the Reverse Polish notation, each operator follows its operand(s). An infix expression (that we are used to) like

(1.2 – 2)*($+-0.5)









is entered like




2 – 4 -0.5 + *



Parentheses are not needed. The notation is unambiguous as long as we know how many operands each operator expects. Reverse Polish is used by calculator on iPhones and in languages like Forth and Postscript.




In this project, you are asked to write a calculator program that supports at least the following six operators +, -, *, /, sin, and log (natural logarithm). You are encouraged to implement other operators though, the more the better. List all supported operators in comments at the beginning of your program and display them at the prompt for user input when your program runs.




Your program should keep producing correct results upon receipt of user inputs and quit on EOF. For this program, you may assume all user inputs are valid.




(12 points) In the real world, many times, the inputs you get from the user might be invalid. In this question, you are asked to enhance the robustness of the program you wrote above such that it is able to produce error messages upon receipt of three out of four types of invalid inputs as listed below and move on to prompt for the next user input. If your program is able to handle all four types, you will receive 4 bonus points.



Unrecognizable operator(s), e.g. 3.01 4 ^
Illegal operand(s), e.g. 4.45 0 /
Insufficient operand(s), e.g. 2.2 0.68 + *
Leftover operand(s) on stack, 18.2 3 4.5 /



Hints

You may find two example files in the attachment:




teststack.c: an implementation of a stack of doubles
testsin.c: an example of using math functions in your program
You should also find the following examples that we went through in class very helpful:

ch1/tryfgets.c: for reading an infix expression from the command line
ch2/testtok.c: for tokenizing a string
ch2/testtod.c: for converting a string to a double












What to turn in?

Create a tarball file by the name of cs3335_a5_yourlastname.tar that includes




The completed source code file reversepolish.c for question 1.
The completed source code file reversepolish_pro.c for question 2.



Submit the tarball file through BlazeVIEW by the due time.

More products