A Simple Expression Evaluation Problem
Programming: Individual Project -- not to be done in groups.
This assignment involves the use of classes, class templates, recursion, and the use of objects in constructing a higher level solution to a programming problem. You are responsible for the implementation of only a portion of the assignment (as described below).
Write a program (The Expression Evaluator) to read in an expression involving integer constants, parentheses, and the six operators +, -, *, /, %, and @ (for exponentiation) and compute the value of the expres- sion. If you have questions or problems, make an appointment to see me or the Lab Assistant. ALSO -- bring questions to class or lab. If you have them, others probably do too.
Turn In: YOU ARE EXPECTED TO CODE THE add_term AND mul_term FUNCTIONS OF THE EXPRESSION EVALUATOR. You will also need to convert the stack class to a class template (with the parameter T used to represent the type of the base elements of the stack) and then use that template as the basis for defining the stack class of integers that you need to solve this problem. EVERYTHING ELSE IS GIVEN. BUT YOU ARE RESPONSIBLE FOR COMPLETE- LY UNDERSTANDING ALL CODE HANDED OUT. SOME EXPLANATIONS OF THIS CODE CAN BE FOUND IN THIS DOCUMENT AND SOME EXPLANATIONS WILL BE GIVEN IN CLASS.
You should turn in your final working program and the output from no more than a half-dozen carefully selected test cases. We will later test your program on an input string given by the lab assistant in the lab. The only thing I will promise you is that once all illegal characters are eliminated, you will have left a string that represents a legal integer computation in C. -- i.e.,
67*((5-7*8)@342%(10-1)
The only errors you need to check for are illegal characters in the string; you may assume that the string is otherwise syntactically correct. All output should be nicely labeled and clearly understandable.
Finally: The syntax diagrams used in the design of the expression evaluator are attached. Except for the code for which you are responsible, the rest of the code for this project may be found on the web site for CIS 223.
Due Date: To be determined.
GENERAL DESIGN ISSUES:
There are four major components in the programming system for solving this problem:
If these components are implemented correctly, the evaluator component will be able to call member functions of the stack class when it needs to push or pop expression operands on or off the stack. The evaluator should be able to call the get_token member function of the lexical analyzer class whenever it needs to know what the next token is in the string of characters. ALL OF THE PROCESSING RELATED TO THE INPUT STRING AND THE STACK SHOULD BE COMPLETELY HIDDEN FROM THE EVALUATOR.
All other characters and even the blank are to be eliminated before you begin processing (either during reading, or after reading the string).
char legal_characters [] = "0123456789+-*/%@()"and then use one of the standard C string.h functions to test whether or not a particular character ch is a member of this string. NOTE: This declaration may not be adequately implemented on all compilers.
enum symbol {lpar, rpar, plus, minus, mod, div, mult, power, number};In the case of a number, you will need to compute the value and return it as well. Again, perhaps another library function can help you with this conversion. NOTE THAT EXCEPT FOR NUMBERS, THE ORIGINAL TOKENS THEMSELVES ARE NEVER RETURNED. THE EXPRESSION EVALUATOR HAS NO NEED TO KNOW ANYTHING ABOUT THEM.