CENGAGE I MINDTAP Programming Exercise 17-10 CO I Instructions
<I>
co
Instructions
(Infix to Postfix) Write a program that converts an infix expression into an equivalent postfix expression. The rules to convert an infix expression into an equivalent postfix expression are as follows:
Suppose inf x represents the infix expression and pf x represents the postfix expression. The rules to convert inf x into pf x are as follows:
I. Initialize pf x to an empty expression and also initialize the stack.
2. Get the next symbol, sym • from inf x . O If sym is an operand. append sym to pf x .
o If sym is (. push sym into the stack.
o If sym is ). pop and append all of the symbols from the stack until the most recent left parentheses. Pop and discard the left parentheses.
o If sym is an operator: ■ Pop and append all of the operators from the stack to pf x that are above the most recent left parentheses and have precedence greater than or equal to sym
■ Push sym onto the stack.
infixToPostFixlmp…. I infixToPostfith I main.cpp
1 //Header file: stackADT.h 2 3 #ifndef H_StackADT 4 #define H_StackADT 6 template <class Type> 7 class stackADT 8 { 9 public: 10 11 12 13 14 virtual bool isEmptyStack() const = 0; //Function to determine whether the stack is empty. //Postcondition: Returns true if the stack is empty, // otherwise returns false.
I myStack.h stackADT.h
virtual void initializeStack() = 0; //Method to initialize the stack to an empty state. //Postcondition: Stack is empty
15 16 17 II 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
virtual bool isFullStack() const = 0; //Function to determine whether the stack is full. //Postcondition: Returns true if the stack is full, // otherwise returns false.
virtual void push(const Type& newltem) = 0; //Function to add newltem to the stack. //Precondition: The stack exists and is not full. //Postcondition: The stack is changed and newltem // is added to the top of the stack.
virtual Type top() const = 0; //Function to return the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: If the stack is empty, the program // terminates; otherwise, the top element // of the stack is returned.
>_ Terminal
x
Q Search this course
• )1
- Phone+65 93543588
- Emailadmin@solvemyproject.com
- Open Hours24x7
- Phone+65 93543588
- Emailadmin@solvemyproject.com
- Open Hours24x7