Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please using with C++ Postfix_Evaluator.h #ifndef POSTFIX_EVALUATOR_H_ #define POSTFIX_EVALUATOR_H_ #include #include #include Syntax_Error.h class Postfix_Evaluator { // Public member functions public: /** Evaluates a postfix

Please using with C++

image text in transcribed

Postfix_Evaluator.h

#ifndef POSTFIX_EVALUATOR_H_ #define POSTFIX_EVALUATOR_H_

#include #include #include "Syntax_Error.h"

class Postfix_Evaluator { // Public member functions public: /** Evaluates a postfix expression. @param expression The expression to be evaluated @return The value of the expression @throws Syntax_Error if a syntax error is detected */ int eval(const std::string& expression);

// Private member functions private: /** Evaluates the current operator. This function pops the two operands off the operand stack and applies the operator. @param op A character representing the operator @throws Syntax_Error if top is attempted on an empty stack */ int eval_op(char op);

/** Determines whether a character is an operator. @param ch The character to be tested @return true if the character is an operator */ bool is_operator(char ch) const { return OPERATORS.find(ch) != std::string::npos; }

// Data fields static const std::string OPERATORS; std::stack operand_stack; };

#endif

Postfix_Evaluator.cpp

/** Implementation of the postfix_evaluator. */

#include "Postfix_Evaluator.h" #include #include using std::stack; using std::string; using std::istringstream; using std::isdigit;

const std::string Postfix_Evaluator::OPERATORS = "+-*/%";

/** Evaluates a postfix expression. @param expression The expression to be evaluated @return The value of the expression @throws Syntax_Error if a syntax error is detected */ int Postfix_Evaluator::eval(const std::string& expression) { // Be sure the stack is empty while (!operand_stack.empty()) operand_stack.pop();

// Process each token istringstream tokens(expression); char next_char; while (tokens >> next_char) { if (isdigit(next_char)) { tokens.putback(next_char); int value; tokens >> value; operand_stack.push(value); } else if (is_operator(next_char)) { int result = eval_op(next_char); operand_stack.push(result); } else { throw Syntax_Error("Invalid character encountered"); } } if (!operand_stack.empty()) { int answer = operand_stack.top(); operand_stack.pop(); if (operand_stack.empty()) { return answer; } else { throw Syntax_Error("Stack should be empty"); } } else { throw Syntax_Error("Stack is empty"); } }

/** Evaluates the current operator. This function pops the two operands off the operand stack and applies the operator. @param op A character representing the operator @throws Syntax_Error if top is attempted on an empty stack */ int Postfix_Evaluator::eval_op(char op) { if (operand_stack.empty()) throw Syntax_Error("Stack is empty"); int rhs = operand_stack.top(); operand_stack.pop(); if (operand_stack.empty()) throw Syntax_Error("Stack is empty"); int lhs = operand_stack.top(); operand_stack.pop(); int result = 0; switch(op) { case '+' : result = lhs + rhs; break; case '-' : result = lhs - rhs; break; case '*' : result = lhs * rhs; break; case '/' : result = lhs / rhs; break; case '%' : result = lhs % rhs; break; } return result; }

(Weight: 1096) Trace the evaluation of the following expression using class Postfix_Evaluator. Show the operand stack each time it is modified (Use Postfix_Evaluator on Blackboard) 10 2 5/ 625 * + . Here is an example of tracing what happens when the first operand is parsed: Expression ActionStack 10 2*5/625Push 10 10 (Weight: 1096) Trace the evaluation of the following expression using class Postfix_Evaluator. Show the operand stack each time it is modified (Use Postfix_Evaluator on Blackboard) 10 2 5/ 625 * + . Here is an example of tracing what happens when the first operand is parsed: Expression ActionStack 10 2*5/625Push 10 10

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_2

Step: 3

blur-text-image_3

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

What do we know about thinking in other species?

Answered: 1 week ago

Question

WHAT IS AUTOMATION TESTING?

Answered: 1 week ago

Question

What is Selenium? What are the advantages of Selenium?

Answered: 1 week ago

Question

Explain the various collection policies in receivables management.

Answered: 1 week ago

Question

4. Explain the strengths and weaknesses of each approach.

Answered: 1 week ago