Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Stack Application Infix to Postfix Conversion) Write a program that uses the Stack class template to convert an infix arithmetic expression into a postfix arithmetic

(Stack Application Infix to Postfix Conversion) Write a program that

uses the Stack class template to convert an infix arithmetic expression into a postfix

arithmetic expression.

In infix form, the operator of an arithmetic statement is in-between every pair of operands.

For example:

a + b

a * (b + c)

(a + b) * c

In postfix form, the operands of an arithmetic statement appear followed by the operator.

One of the virtues of postfix form is that expressions can be written without the need for

parentheses. Here are some examples of arithmetic expressions written in postfix form:

a b + // Equivalent to a + b

a b c + * // Equivalent to a * (b + c)

a b + c * // Equivalent to (a + b) * c

Please write a C++ program that uses an operator stack to convert an infix arithmetic

expression that the user enters into a postfix arithmetic expression. Users input the infix

arithmetic expressions which only contains the operands, operators and parenthesis

symbols. The character # marks the end of the expression. The operands are represented

by single lowercase letters, i.e., a, b, c, d, etc, and the operators are +, -, * and /.

Assuming the expressions the users enter are all validated.

You may call the following functions in main function.

// checks if c is an operand

bool is_operand(char c)

{

if(c >= 'a' && c <= 'z')

return true;

else

return false;

}

// checks if c is an operator +, -, *, or /

bool is_operator(char c)

{

if(c == '+' || c == '-' || c == '*' || c == '/')

return true;

else

return false;

}

// return the precedence of an operator

int get_operator_precedence(char op)

{

int prec = 0;

if (op == '+' || op == '-' )

prec = 1;

else if (op == '*' || op == '/' )

prec = 2;

else

prec = -1;

return prec;

}

Three files should be submitted for this program question.

1) the file Stack.h which contains the definition and implementation of Stack

class template,

2) the application file a2q2.cpp containing main() function,

3) a script file a2q2result containing result.

Here are the sample runs:

The infix expression:

a+b-(c+d)*e

The postfix expression:

ab+cd+e*-

The infix expression:

a*(b+c)-e

The postfix expression:

abc+*e

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

Algorithmic Trading Navigating The Digital Frontier

Authors: Alex Thompson

1st Edition

B0CHXR6CXX, 979-8223284987

More Books

Students also viewed these Databases questions