Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please using with C++ Infix_To_Postfix.cpp /** Implementation of Infix_To_Postfix that processes parentheses.*/ #include Infix_To_Postfix.h #include #include using std::string; using std::istringstream; const string Infix_To_Postfix::OPERATORS = +-*/%()[]{};

Please using with C++

image text in transcribed Infix_To_Postfix.cpp

/** Implementation of Infix_To_Postfix that processes parentheses.*/

#include "Infix_To_Postfix.h" #include #include using std::string; using std::istringstream;

const string Infix_To_Postfix::OPERATORS = "+-*/%()[]{}"; const int Infix_To_Postfix::PRECEDENCE[] = { 1, 1, 2, 2, 2, -1, -1, -1, -1, -1, -1 };

/** Extract and process each token in infix and return the equivalent postfix string. @param expression The infix expression @return The equivalent postfix expression @throws Syntax_Error */ string Infix_To_Postfix::convert(const string& expression){ postfix = ""; while (!operator_stack.empty()) operator_stack.pop(); istringstream infix_tokens(expression); string next_token; while(infix_tokens >> next_token) { if (isalnum(next_token[0])) { postfix += next_token; postfix += " "; } else if (is_operator(next_token[0])) { process_operator(next_token[0]); } else { throw Syntax_Error("Unexpected Character Encountered"); } } // End while // Pop any remaining operators and append them to postfix while (!operator_stack.empty()) { char op = operator_stack.top(); operator_stack.pop(); if (op == '(' || op == '[' || op == '}') { throw Syntax_Error("Unmatched open parenthesis"); } postfix += op; postfix += " "; } return postfix; }

/** Function to process operators. @param op The operator @throws Syntax_Error */ void Infix_To_Postfix::process_operator(char op) { if (operator_stack.empty() || (op == '(') || (op == '[') || (op == '{')) { if (op == ')' || op == ']' || op == '}') throw Syntax_Error("Unmatched close parenthesis"); operator_stack.push(op); } else { if (precedence(op) > precedence(operator_stack.top())) { operator_stack.push(op); } else { // Pop all stacked operators with equal // or higher precedence than op. while (!operator_stack.empty() && (operator_stack.top() != '(') && (operator_stack.top() != '[') && (operator_stack.top() != '{') && (precedence(op) top of stack operator // precedence; if (op == ')') { if (!operator_stack.empty() && (operator_stack.top() == '(')) { operator_stack.pop(); } else { throw Syntax_Error("Unmatched close parentheses"); } } else if (op == ']') { if (!operator_stack.empty() && (operator_stack.top() == '[')) { operator_stack.pop(); } else { throw Syntax_Error("Unmatched close parentheses"); } } else if (op == '}') { if (!operator_stack.empty() && (operator_stack.top() == '{')) { operator_stack.pop(); } else { throw Syntax_Error("Unmatched close parentheses"); } } else { operator_stack.push(op); } } } }

(weight: 10%) How would you modify class Infix-To-Postfix to handle the exponentiation operator, indicated by the symbol A. The first operand is raised to the power indicated by the second operand. Assume that a sequence of operators will not occur and that precedence(') is greater than precedence(*)

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

Students also viewed these Databases questions

Question

1-4 How will MIS help my career?

Answered: 1 week ago