Question

In: Computer Science

Using STL stack class, implement in C++ a function that converts an infix expression to postfix...

Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,

Solutions

Expert Solution

#include <iostream>
#include <stack>
using namespace std;

// declare necessary functions which are defined below
int priority(char c);
bool isOperator(char c);

int main() {
        string infix, postfix;
        cout << "Enter an infix expression: ";
        cin >> infix; // take input in infix string
        stack<char> s; // instantiate stack of char
        
        /* We iterate a loop from start to end of infix string and for each character we do the following:
         * 1. if the character is an operator or brackets we check if it is opening bracket then we add it to stack and if it is closing bracket then we pop elements from stack until
         * it becomes empty or we found an opening bracket from the stack.
         * 2. if the character is an operator we check its precedence (or priority) using priority function. precedence of operators are as follows : '^' > ( '/' = '*') > ('+' = '-')
         * We keeps on popping elements from stack and adding it in postfix expression until the priority of current character becomes greater than stack's top element
        */
        
        for(int i=0; i<infix.length(); i++) {
                // check if the character is operator or a bracket or not
                if(isOperator(infix[i])) {
                        // if opening bracket just push it in stack
                        if(infix[i] == '(')
                                s.push(infix[i]);
                        // if it is closing bracket then continuously pop elements from stack and add in postfix string until we encounter a opening bracket or stack is empty
                        else if(infix[i] == ')') {
                                while(s.size() > 0 && s.top() != '(') {
                                        postfix += s.top();
                                        s.pop();
                                }
                                // if stack is not empty and top element is an opening bracket pop it
                                if(s.top() == '(') {
                                        s.pop();
                                }
                        }
                        else {
                                // if the character is an operator than we check its precedence and pop element until its precedence becomes greater than the top element from stack or stack is empty
                                while(s.size() > 0 && priority(infix[i]) <= priority(s.top())) {
                                        postfix += s.top();
                                        s.pop();
                                }
                                s.push(infix[i]); // push current characater in stack
                        }
                }
                // if the character is not an operator or a bracket then just add it in postfix expression string
                else {
                        postfix += infix[i];
                }
        }
        // add the remaining elements in postfix string
        while(s.size() > 0) {
                postfix += s.top();
                s.pop();
        }
        // print the postfix expression
        cout << "Postfix expression is: " << postfix << endl;
        return 0;
}

// returns true if the character is an operator or a bracket else return false
bool isOperator(char c) {
        if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c == '(' || c == ')')
                return true;
        return false;
}

// returns the priority of operator
// priority of '+' and '-' is 0
// priority of '*' and '/' is 1
// priority of '^' is 2
// priority of any other char is -1
int priority(char c) {
        if(c == '+' || c == '-')
                return 0;
        else if(c == '*' || c == '/')
                return 1;
        else if(c == '^')
                return 2;
        else
                return -1;
}

Output is like this:


Related Solutions

Using STL stack class, implement in C++ a function that checks for balanced braces { },...
Using STL stack class, implement in C++ a function that checks for balanced braces { }, in a given string / arithmetic expressions.
Write a class PostFix that has one method covert that converts an infix expression (as a...
Write a class PostFix that has one method covert that converts an infix expression (as a string input) to a postfix. Then Test it in a different class. code in java.
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
Using STL stack class, implement in C++ the following pseudocodefunctioncheckBraces(aString: string) that checks for...
Using STL stack class, implement in C++ the following pseudocode functioncheckBraces(aString: string) that checks for balanced braces { } in a given string /arithmetic expressions. Write a main() program to test the function.// Checks the string aString to verify that braces match.// Returns true if aString contains matching braces, false otherwise.checkBraces(aString: string): boolean{aStack = a new empty stackbalancedSoFar = truei =0// Tracks character position in stringwhile (balancedSoFar and i < length of aString) {ch = character at position i in...
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
how do I write a class PostFix that has one method covert that converts an infix...
how do I write a class PostFix that has one method covert that converts an infix expression (as a string input) to postfix in java?
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
Find is the final result of evaluating the following postfix expression using a stack. Show each...
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation. 85 5 / 4 * 5   6 +   10    5 -   * +
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT