The concept of stack is extremely important in computer science

Added on - 18 Sep 2019

  • 3

    pages

  • 976

    words

  • 140

    views

  • 0

    downloads

Showing pages 1 to 1 of 3 pages
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 asfar as parentheses are concerned, do the following:Create an empty stack to hold left parenthesisONLY.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 characteris 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 applyaccordingly: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,
desklib-logo
You’re reading a preview
card-image

To View Complete Document

Become a Desklib Library Member.
Subscribe to our plans

Download This Document