# Arithmetic Expression Evaluator

3 Pages976 Words266 Views
|
|
|
The concept of stack is extremely important in computer science and is used in a wide variety ofproblems. This assignment requires you to write a program that can be used to evaluate ordinaryarithmetic expressions that contains any of the five arithmetic operators (+, -, *, /, %). This exercise requires three distinct steps, namely:- 1.Verify that the infix arithmetic expression (the original expression), that may containregular parentheses, is properly formed as far as parentheses are concerned. 2.If the parenthesized expression is properly formed, convert the expression from an infixexpression to its equivalent postfix expression, called Reverse Polish Notation (RPN)named after the Polish Mathematician J. Lukasiewics. 3.Evaluate the postfix expression, and print the result. Step 1 - Verify that the expressionGiven an arithmetic expression, called an infixed expression, to verify that it is properly formed as far as parentheses are concerned, do the following:Create an empty stack to hold left parenthesis ONLY.Scanned the arithmetic expression from left to right, one character at a time.While there are more characters in the arithmetic expression{If the character is a left parenthesis ‘(‘, push it on to the stack. However if the character is a right parenthesis, ‘)’, visit the stack and pop the top element from off the stack.}If the stack contains any element at the end of reading the arithmetic expression, then theexpression was not properly formed.Step 2 - Convert infixed expression to postfixGiven that an arithmetic expression is properly form with respect to parentheses, do the following:Create an empty stack to hold any arithmetic operators and left parenthesis, ONLY.A string to contain the postfix expression – the output from this conversion.Scan the arithmetic expression from left to right.While the are more symbols in the arithmetic expression, {After a symbol is scanned, there are four (4) basic rules to observed and apply accordingly:1.If the symbol is an operand (a number), write it to the output string.2.If the symbol is an operator and if the stack is empty, push the symbol on the stack. Otherwise, if the symbol is either ‘(‘ or ‘)’, check for the following conditions:If the symbol is ‘(‘, push on to the stack,

## End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents

### Support

#### +1 306 205-2269

Chat with our experts. we are online and ready to help.