From a0b3e97d555ee504636c06fd3a72983b95c3bfb6 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 26 Sep 2014 22:51:49 +0200 Subject: Eliminated manual division through zero prevention * while integer division through zero is obviously a low level error that should be prevented, this operation is well-defined for floating point values as used by SimpleParser --- src/exceptions.h | 7 ------- src/nodes.cc | 18 ++++++------------ 2 files changed, 6 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/exceptions.h b/src/exceptions.h index b17f424..1408603 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -17,13 +17,6 @@ class operator_exception: public std::exception { } }; -class divide_exception: public std::exception { - virtual const char* what() const throw() - { - return "A divison through zero had to be prevented by the parser - check your input term."; - } -}; - class identifier_exception: public std::exception { virtual const char* what() const throw() { diff --git a/src/nodes.cc b/src/nodes.cc index 283b583..256a609 100644 --- a/src/nodes.cc +++ b/src/nodes.cc @@ -30,29 +30,23 @@ OperatorNode::OperatorNode(TokenType token): double OperatorNode::solve() const { switch ( this->operator_ ) { - case TokenType::OPERATOR_MULTIPLY: { - return this->leftChild->solve() * this->rightChild->solve(); - } case TokenType::OPERATOR_PLUS: { return this->leftChild->solve() + this->rightChild->solve(); } case TokenType::OPERATOR_MINUS: { return this->leftChild->solve() - this->rightChild->solve(); } + case TokenType::OPERATOR_MULTIPLY: { + return this->leftChild->solve() * this->rightChild->solve(); + } + case TokenType::OPERATOR_DIVIDE: { + return this->leftChild->solve() / this->rightChild->solve(); + } case TokenType::OPERATOR_POWER: { return std::pow( this->leftChild->solve(), this->rightChild->solve() ); } - case TokenType::OPERATOR_DIVIDE: { - const double rightChild{ this->rightChild->solve() }; - - if ( rightChild != 0 ) { - return this->leftChild->solve() / rightChild; - } else { - throw divide_exception(); - } - } default: { throw operator_exception(); } -- cgit v1.2.3