Question: Lab Exercise Below are codes that convert from infix to postfix and infix to prefix. CODE infix to postfix: //Function to return precedence of operators
Lab Exercise Below are codes that convert from infix to postfix and infix to prefix. CODE infix to postfix: //Function to return precedence of operators int prec(char) { if (c == "") return 3; else if (* 11-7) return 2; else if (c == I1 C =) return 1; else return -1; } void infixToPostfix(string :) std::stack char> st; st.push('N'); int 1. s.length(); string ns; for (int i = 0; i 'a' && s[1] = 'A' && s[i] while (st.top() = N*$8 st.top() != "") { char = st.top(); st.pop(); ns ; } if (st.top() == 0) char = st.top(); st.pop(); > > //If an operator is scanned else while (st.top() I-*** prec($[1]) st.push([1]); 1/ Pop all the remaining elements from the stack while (st.top() ! N') char c. st.top(); st.pop(); ns += c; > cout CODE infix=>*z)>
CA) return 3; return; > string infixToPostfix(string Inf.x) Infix ( + infix + '); int 1 Infix.size(); stackechar> char_stack; string output; for (int i . 8; 1 while (char_stack.top() () output + char_stack.top(); char_stack.pop(); } // Renove '' from the stack char_stack.pop(); > // Operator found else { if (isOperator(char_stack.top()) { while (getPriority(infix[1]) =>
Replace with ) and vice versa Get Postfix Reverse Postfix Intl. Infix.size(); // Reverse infix reverse( infix.begin(), infix.end()); // Replace with ) and vice versa for (int i 0; i else if (infix[i] == ')) { infix[i] = ''; > > string prefix = infixToPostfix(infix); // Reverse postfix reverse(prefix.begin(), prefix.end()); return prefix V/ Driver code int main() string s . ("(-b/c)(a/k-1)"); cout Question: Try to rerun the code by changing the infix expression and analyze the result.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
