diff options
author | Adrian Kummerländer | 2012-04-15 00:26:12 +0200 |
---|---|---|
committer | Adrian Kummerländer | 2012-04-15 00:26:12 +0200 |
commit | 4daed5ebe3e8eae184a96055974ac15a3c55507f (patch) | |
tree | 900920a59780915d8b8eb75eccdf6afd45c680e0 | |
parent | de97f27c7aa3c74a40d5ba7ce9477d4bbdefbe8e (diff) | |
download | SimpleParser-4daed5ebe3e8eae184a96055974ac15a3c55507f.tar SimpleParser-4daed5ebe3e8eae184a96055974ac15a3c55507f.tar.gz SimpleParser-4daed5ebe3e8eae184a96055974ac15a3c55507f.tar.bz2 SimpleParser-4daed5ebe3e8eae184a96055974ac15a3c55507f.tar.lz SimpleParser-4daed5ebe3e8eae184a96055974ac15a3c55507f.tar.xz SimpleParser-4daed5ebe3e8eae184a96055974ac15a3c55507f.tar.zst SimpleParser-4daed5ebe3e8eae184a96055974ac15a3c55507f.zip |
Removed all memory leaks detected by valgrind
-rw-r--r-- | main.cpp | 4 | ||||
-rw-r--r-- | parser.cpp | 86 | ||||
-rw-r--r-- | parser.h | 4 | ||||
-rw-r--r-- | tree.cpp | 12 | ||||
-rw-r--r-- | tree.h | 1 |
5 files changed, 58 insertions, 49 deletions
@@ -8,13 +8,13 @@ int main(int argc, char *argv[]) std::cin >> inputTerm; // Example: 2.5*(2+3-(3/2+1)) - Parser *parser = new Parser(); + Parser parser; try { typedef std::numeric_limits<double> dbl; std::cout.precision(dbl::digits10); - std::cout << parser->calculate(inputTerm, false).result << std::endl; + std::cout << parser.calculate(inputTerm, false).result << std::endl; } catch ( exception &e ) { @@ -24,14 +24,14 @@ int Parser::getPriority(char tmp) } } -vector<string>* Parser::lexer(string term) +vector<string> Parser::lexer(string term) { int priority = 0; int last_priority = 0; int level = 0; string tmp, tmp_num; - vector<string> *output = new vector<string>(); + vector<string> output; string::iterator termIter; @@ -48,7 +48,7 @@ vector<string>* Parser::lexer(string term) } else { if ( last_priority == -1 && level == 0 ) { - output->push_back( tmp_num ); + output.push_back( tmp_num ); tmp_num = ""; } @@ -65,7 +65,7 @@ vector<string>* Parser::lexer(string term) level--; if ( level == 0 ) { - output->push_back( tmp ); + output.push_back( tmp ); tmp = ""; } else { @@ -78,7 +78,7 @@ vector<string>* Parser::lexer(string term) string helper; helper = *termIter; - output->push_back( helper ); + output.push_back( helper ); } else { tmp += *termIter; @@ -92,7 +92,7 @@ vector<string>* Parser::lexer(string term) } if ( last_priority == -1 ) { - output->push_back( tmp_num ); + output.push_back( tmp_num ); } else if ( last_priority != 90 ) { throw operator_exception(); @@ -105,95 +105,95 @@ vector<string>* Parser::lexer(string term) return output; } -Node* Parser::buildTree(Tree **tree, string term) +Node* Parser::buildTree(Tree *tree, string term) { - stack<Node*> *operandStack = new stack<Node*>(); - stack<Node*> *operatorStack = new stack<Node*>(); + stack<Node*> operandStack; + stack<Node*> operatorStack; int priority; - vector<string> *tmpLexer; + vector<string> tmpLexer; - vector<string> *lexerOutput = this->lexer(term); + vector<string> lexerOutput = this->lexer(term); - for ( vector<string>::iterator termIter = lexerOutput->begin(); termIter != lexerOutput->end(); termIter++ ) { + for ( vector<string>::iterator termIter = lexerOutput.begin(); termIter != lexerOutput.end(); termIter++ ) { priority = this->getPriority( (*termIter)[0] ); if ( priority != -1 && (*termIter).size() == 1 ) { - if ( !operatorStack->empty() ) { - OperatorNode *lastNode = static_cast<OperatorNode*>( operatorStack->top() ); + if ( !operatorStack.empty() ) { + OperatorNode *lastNode = static_cast<OperatorNode*>( operatorStack.top() ); if ( this->getPriority( lastNode->function ) < priority ) { - operatorStack->push( - (*tree)->addOperator( NULL, (*termIter)[0] ) + operatorStack.push( + tree->addOperator( NULL, (*termIter)[0] ) ); } else { - Node *currOperator = operatorStack->top(); - operatorStack->pop(); + Node *currOperator = operatorStack.top(); + operatorStack.pop(); - currOperator->rightChild = operandStack->top(); - operandStack->pop(); + currOperator->rightChild = operandStack.top(); + operandStack.pop(); - currOperator->leftChild = operandStack->top(); - operandStack->pop(); + currOperator->leftChild = operandStack.top(); + operandStack.pop(); - operandStack->push( currOperator ); + operandStack.push( currOperator ); termIter--; } } else { - operatorStack->push( - (*tree)->addOperator( NULL, (*termIter)[0] ) + operatorStack.push( + tree->addOperator( NULL, (*termIter)[0] ) ); } } else { tmpLexer = this->lexer( *termIter ); - if ( tmpLexer->size() == 1 ) { - operandStack->push( - (*tree)->addOperand( NULL, + if ( tmpLexer.size() == 1 ) { + operandStack.push( + tree->addOperand( NULL, strtod( termIter->c_str(), NULL ) ) ); } - else if ( tmpLexer->size() > 1 ) { - operandStack->push( + else if ( tmpLexer.size() > 1 ) { + operandStack.push( buildTree(tree, *termIter) ); } } } - while ( !operatorStack->empty() ) { - OperatorNode *currOperator = static_cast<OperatorNode*>( operatorStack->top() ); - operatorStack->pop(); + while ( !operatorStack.empty() ) { + OperatorNode *currOperator = static_cast<OperatorNode*>( operatorStack.top() ); + operatorStack.pop(); - currOperator->rightChild = operandStack->top(); - operandStack->pop(); + currOperator->rightChild = operandStack.top(); + operandStack.pop(); - currOperator->leftChild = operandStack->top(); - operandStack->pop(); + currOperator->leftChild = operandStack.top(); + operandStack.pop(); - operandStack->push( currOperator ); + operandStack.push( currOperator ); } - return operandStack->top(); + return operandStack.top(); } ParserResult Parser::calculate(string term, bool getTreeAsDot) { - Tree *termTree = new Tree(); - termTree->root = buildTree(&termTree, term); + Tree termTree; + termTree.root = this->buildTree(&termTree, term); ParserResult returnVal; - returnVal.result = termTree->root->solve(); + returnVal.result = termTree.root->solve(); if ( getTreeAsDot ) { - returnVal.tree = termTree->print(term); + returnVal.tree = termTree.print(term); } return returnVal; @@ -16,8 +16,8 @@ class Parser private: int getPriority(char); - vector<string>* lexer(string); - Node* buildTree(Tree**, string); + vector<string> lexer(string); + Node* buildTree(Tree*, string); }; class parenthese_exception: public exception @@ -69,11 +69,20 @@ Tree::Tree() this->nodeCollection = new vector<Node*>(); } +Tree::~Tree() +{ + for ( vector<Node*>::iterator it = this->nodeCollection->begin(); it != this->nodeCollection->end(); it++ ) + { + delete *it; + } + + delete this->nodeCollection; +} + Node* Tree::addOperand(Node **place, double value) { OperandNode *newNode = new OperandNode(); - newNode = new OperandNode(); newNode->value = value; this->nodeCollection->push_back( newNode ); @@ -89,7 +98,6 @@ Node* Tree::addOperator(Node **place, char oper) { OperatorNode *newNode = new OperatorNode(); - newNode = new OperatorNode(); newNode->function = oper; this->nodeCollection->push_back( newNode ); @@ -47,6 +47,7 @@ class Tree { public: Tree(); + ~Tree(); Node *root; |