{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

If the token is an operand we push the value

Info icon This preview shows pages 18–23. Sign up to view the full content.

View Full Document Right Arrow Icon
If the token is an operand, we push the value associated with it onto the stack. If it is a binary operator, we pop two values from the stack, apply the operator to them, and push the result back into the stack. (If it is a unary operator, we pop only one value, apply the operator to it, and push the result back into the stack.) We assume that first we convert an expression from infix form to postfix form. Then we use the expression in postfix form to evaluate the expression by calling the function getValue() . c circlecopyrt Teresa Leyk Slide 18 Parsing & Evaluating Expressions
Image of page 18

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CPSC 221 Data Structures & Algorithms We need also to initialize the object of type Evaluator using an object of type Parser (see main() below). These are additional functions used in the class Evaluator : 1. valStack is a stack keeping values of operands. 2. parser is a pointer to a parser object we want to evaluate. 3. isOperator() receives a token as an argument and returns true if that token is a valid operator. 4. eval() —a function that applies a binary operator (third argument) to two numeric operands of type int (first and second arguments), and returns the result of this operation. (A similar function with two arguments exists for unary operators.) c circlecopyrt Teresa Leyk Slide 19 Parsing & Evaluating Expressions
Image of page 19
CPSC 221 Data Structures & Algorithms The implementation of the class Evaluator presented here is based on an integer stack and queue. Therefore, only character tokens are accepted (that is, only digits from 0 to 9 can be used). Also, division of two integers gives an integer (for example, 2 / 3 is 0, or 6 / 4 is 1). This restrictions will be removed in your next assignment where characters will be replaced by strings, and division of two numbers will return a number of type double . This is an example how to run the evaluator. Enter expression: (1-2) * 3 Expression: (1-2) * 3 12-3 * Value = -3 Enter expression: 2 * 3/4 Expression: 2 * 3/4 23 * 4/ Value = 1 Enter expression: quit c circlecopyrt Teresa Leyk Slide 20 Parsing & Evaluating Expressions
Image of page 20

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CPSC 221 Data Structures & Algorithms The Class Evaluator class Evaluator { private : // exception class DivisionByZero : public RuntimeException { public : DivisionByZero() : RuntimeException("Division by zero") {} }; Parser * parser; // pointer to the current parser LinkedStack valStack; // value stack int eval( int v1, int v2, Parser::EnumTokens token) throw (DivisionByZero); // two-argument evaluator int eval( int v1, Parser::EnumTokens token); // one-arg eval bool isOperator(Parser::EnumTokens token); public : //constructor Evaluator (Parser * par) : parser(par), valStack() { } int getValue(); // get a value of the expression }; c circlecopyrt Teresa Leyk Slide 21 Parsing & Evaluating Expressions
Image of page 21
CPSC 221 Data Structures & Algorithms Implementation of the Class Evaluator #include <cmath> // used for pow #include <cctype> #include "Evaluator.h" #include "RuntimeException.h" int Evaluator::eval( int v1, int v2, Parser::EnumTokens token) throw (DivisionByZero) { if (token == Parser::EXP) return static_cast < int >(pow( static_cast < double >(v1), v2)); else if (token == Parser::PLUS) return v1 + v2; else if (token == Parser::MINUS) return v1 - v2; else if (token == Parser::MULT) return v1 * v2; else if (token == Parser::DIV) if (v2 != 0)
Image of page 22

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 23
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern