Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2.(30) Write a program to convert an infix expression to a postfix expression using a stack. Hint: In a postfix expression operators appear after their

2.(30) Write a program to convert an infix expression to a postfix expression using a stack. Hint: In a postfix expression operators appear after their operands, whereas in an infix expression they appear between their operands. Process the symbols in the postfix expression one-by-one. Output operands immediately, but save the operators in a stack until they are needed. Pay special attention to the precedence of the operators. (basic operators: +, -, x, /, ( ) and exponentiation operator ^^^)

The algorithm for converting from infix to postfix notation is given as follows. You don't have to use it if you find your own algorithm to do. This is for your reference only. You need to think how to add the exponentiation operator into the algorithm. I leave it to you to figure out. Algorithm:

initialize an operator stack, s

while not at the end of the infix expression do the following:

read the next symbol in case the symbol is

an operand : write the operand

'(' : push '(' onto s

')' : pop and write all operators until encountering '(', then pop '('

'*'or '/' : pop and write all '*' and '/' operators from the top down to, but not including, the top-most '(', '+' or '-' or to the bottom of the stack

push the new symbol,'*' or '/'

'+'or'-' : pop and write all operators from the top down to, but not including, the topmost '(' or to the bottom of the stack

push the new symbol, '+' or '-'

end-of-expression: pop and write all operators

Show the testing of your program and your program should be well documented.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions