Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// An algorithm for infix to postfix expression conversion. // For example, a + b - c translates to a b + c - //

// An algorithm for infix to postfix expression conversion. // For example, a + b - c translates to a b + c - // a + b * c translates to a b c * + // (a + 2) / (5 - d) goes to a 2 + 5 d - / // a + ((b - c) * d) / e to a b c - d * e / + // Valid operands are single digits and characters: 0-9 a-z A-Z // Valid operators are: + - * / ( ) // Highest precedence: * / // Lowest precedence: + - // ( has lowest precedence on the stack and highest precedence outside of stack. // ) never goes on stack. // Bottom of the stack has the lowest precedence than any operator. // Use a prec() function to compare the precedence of the operators based on the above rules. // Note there is little error checking in the algorithm! while there is more input if input is an operand print input else if input is '(' // '(' has lowest precedence in the stack, highest outside push input on stack else if input is ')' while stack is not empty and top of stack is not '(' print top of stack pop stack if stack is not empty pop stack else error // no matching '(' else if input is '*' or '/' or '+' or '-' if stack is empty or prec(top of stack) < prec(input) // bottom of stack has lower precedence than everything push input on stack else // prec(top of stack) >= prec(input) while stack is not empty and prec(top of stack) >= prec(input) print top of stack pop stack push input on stack else check for errors while stack is not empty print top of stack pop stack 

have this but does not work, any help will be appriciated.

#include

#include

using namespace std;

int prec(char ch) { if (ch == '*' or ch == '/') return 2; else if (ch == '+' or ch == '-') return 1; else if (ch == '(' or ch == ')') return 0; }

int main() {

char ch; stack oper;

cin.get(ch); while (!cin.eof()) { if((ch>='0' and ch<='9')or (ch >='a' and ch <'z')or (ch >'A' and ch <='Z')) // checking for operand cout << ch; else { if (ch == '(') { oper.push(ch); } else if (ch==')'){ while(!oper.empty() && (oper.top() !='(')) { cout << oper.top(); oper.pop(); } if (!oper.empty()) { oper.pop(); } else cout << "Error no matching ("; } else if (ch == '*' or ch =='/' or ch == '+' or ch == '-') { if (oper.empty() or prec(oper.top()) < prec(ch)){ // lower stack has lowest precedence oper.push(ch); } else { while (!oper.empty() && prec(oper.top()) >= prec(ch)) { cout << oper.top(); oper.pop(); } oper.push(ch); } } else { cout << " illegal character"; } cin.get(ch); } while (!oper.empty()) { cout << oper.top(); oper.pop(); } } }

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

More Books

Students also viewed these Databases questions

Question

LO1 Explain how the workforce is changing in unpredicted ways.

Answered: 1 week ago

Question

LO6 List the components of job descriptions.

Answered: 1 week ago