0 return v1 v2 else throw divisionbyzero return 0 c

Info icon This preview shows pages 22–26. Sign up to view the full content.

View Full Document Right Arrow Icon
(v2 != 0) return v1 / v2; else throw DivisionByZero(); return 0; } c circlecopyrt Teresa Leyk Slide 22 Parsing & Evaluating Expressions
Image of page 22

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

View Full Document Right Arrow Icon
CPSC 221 Data Structures & Algorithms int Evaluator::eval( int v1, Parser::EnumTokens token) { if (token == Parser::UNARY_MINUS) return -v1; return 0; } bool Evaluator::isOperator(Parser::EnumTokens token) { return token == Parser::EXP || token == Parser::DIV || token == Parser::MULT || token == Parser::PLUS || token == Parser::MINUS || token == Parser::UNARY_MINUS; } c circlecopyrt Teresa Leyk Slide 23 Parsing & Evaluating Expressions
Image of page 23
CPSC 221 Data Structures & Algorithms int Evaluator::getValue() { Parser::EnumTokens token; int v1, v2; LinkedQueue& postfix = parser->postfix; // alias while (! postfix.isEmpty()) { try { // prevToken is not used here token = parser->getToken(postfix.dequeue()); } catch (...) { cerr << "postfix queue error " << endl; } c circlecopyrt Teresa Leyk Slide 24 Parsing & Evaluating Expressions
Image of page 24

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

View Full Document Right Arrow Icon
CPSC 221 Data Structures & Algorithms // Evaluator::getValue continued if (isOperator(token)) { // operator try { if (token == Parser::UNARY_MINUS) { v1 = valStack.pop(); valStack.push(eval(v1, token)); } else { v2 = valStack.pop(); v1 = valStack.pop(); valStack.push(eval(v1, v2, token)); } } catch (DivisionByZero& e) { cerr << e << endl; return 0; } catch (...) { cerr << "Wrong operand(s) " << endl; return 0; } c circlecopyrt Teresa Leyk Slide 25 Parsing & Evaluating Expressions
Image of page 25
CPSC 221 Data Structures & Algorithms // Evaluator::getValue continued } else if (token != Parser::END) // operand try { int val = parser->curVal; if (! isdigit(val)) // only digits are accepted throw RuntimeException("Value is not an integer"); val = val - ’0’; // replace char with an int value valStack.push(val); } catch (...) { cerr << "Wrong value: " << parser->curVal << endl; } } // end while loop try { return valStack.pop(); // return the final value } catch (...) { cerr << "Wrong expression" << endl; } return 0; } c circlecopyrt Teresa Leyk Slide 26 Parsing & Evaluating Expressions
Image of page 26
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