From 1482e7ede3133b8bf78c68c2e0618b1de90d328c Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sun, 4 Mar 2012 20:50:05 +0100 Subject: Got rid of boost dependency --- main.cpp | 3 +- parser.cpp | 96 ++++++++++++++++++++++++++++++++++---------------------------- parser.h | 5 +--- tree.cpp | 6 ++-- tree.h | 3 +- 5 files changed, 58 insertions(+), 55 deletions(-) diff --git a/main.cpp b/main.cpp index d3c3799..96735da 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,4 @@ #include - #include "parser.h" int main(int argc, char *argv[]) @@ -16,7 +15,7 @@ int main(int argc, char *argv[]) catch ( exception &e ) { std::cerr << e.what() << std::endl; - std::exit(1); + exit(1); } return 0; diff --git a/parser.cpp b/parser.cpp index 757f8c5..d3a9bb7 100644 --- a/parser.cpp +++ b/parser.cpp @@ -16,7 +16,7 @@ int Parser::getPriority(char tmp) case '(': return 90; case ')': - return 90; + return 90; case ',': return -1; default: @@ -30,14 +30,14 @@ vector* Parser::lexer(string term) int last_priority = 0; int level = 0; string tmp, tmp_num; - + vector *output = new vector(); - + string::iterator termIter; - + for ( termIter = term.begin(); termIter != term.end(); termIter++ ) { priority = this->getPriority( *termIter ); - + if ( priority == -1 || ( termIter == term.begin() && priority == 10 ) ) { if ( level > 0 ) { tmp += *termIter; @@ -51,19 +51,19 @@ vector* Parser::lexer(string term) output->push_back( tmp_num ); tmp_num = ""; } - + switch ( *termIter ) { case '(': { if ( level > 0 ) { tmp += *termIter; } - + level++; break; } case ')': { level--; - + if ( level == 0 ) { output->push_back( tmp ); tmp = ""; @@ -73,9 +73,12 @@ vector* Parser::lexer(string term) } break; } - default: { + default: { if ( level == 0 ) { - output->push_back( boost::lexical_cast(*termIter) ); + string helper; + helper = *termIter; + + output->push_back( helper ); } else { tmp += *termIter; @@ -84,21 +87,21 @@ vector* Parser::lexer(string term) } } } - - last_priority = priority; + + last_priority = priority; } - + if ( last_priority == -1 ) { output->push_back( tmp_num ); } else if ( last_priority != 90 ) { throw operator_exception(); } - + if (level != 0) { throw parenthese_exception(); } - + return output; } @@ -106,72 +109,77 @@ Node* Parser::buildTree(Tree **tree, string term) { stack *operandStack = new stack(); stack *operatorStack = new stack(); - + int priority; - Node *newNode; - + vector *tmpLexer; - + vector *lexerOutput = lexer(term); - + for ( vector::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( operatorStack->top() ); - + if ( this->getPriority( lastNode->function ) < priority ) { - newNode = (*tree)->addOperator( NULL, (*termIter)[0] ); - operatorStack->push( newNode ); + operatorStack->push( + (*tree)->addOperator( NULL, (*termIter)[0] ) + ); } else { Node *currOperator = operatorStack->top(); operatorStack->pop(); - + currOperator->rightChild = operandStack->top(); operandStack->pop(); - + currOperator->leftChild = operandStack->top(); operandStack->pop(); - + operandStack->push( currOperator ); - + termIter--; } } else { - newNode = (*tree)->addOperator( NULL, (*termIter)[0] ); - operatorStack->push( newNode ); + operatorStack->push( + (*tree)->addOperator( NULL, (*termIter)[0] ) + ); } } else { tmpLexer = lexer( *termIter ); - + if ( tmpLexer->size() == 1 ) { - newNode = (*tree)->addOperand( NULL, boost::lexical_cast(*termIter) ); - operandStack->push( newNode ); + operandStack->push( + (*tree)->addOperand( NULL, + strtod( termIter->c_str(), NULL ) + ) + ); } else if ( tmpLexer->size() > 1 ) { - newNode = buildTree(tree, *termIter); - operandStack->push( newNode ); + operandStack->push( + buildTree(tree, *termIter) + ); } } } - + while ( !operatorStack->empty() ) { OperatorNode *currOperator = static_cast( operatorStack->top() ); operatorStack->pop(); - + currOperator->rightChild = operandStack->top(); operandStack->pop(); - + currOperator->leftChild = operandStack->top(); operandStack->pop(); - + operandStack->push( currOperator ); } - + return operandStack->top(); } @@ -179,14 +187,14 @@ ParserResult Parser::calculate(string term, bool getTreeAsDot) { Tree *termTree = new Tree(); termTree->root = buildTree(&termTree, term); - + ParserResult returnVal; - + returnVal.result = termTree->root->solve(); - + if ( getTreeAsDot ) { returnVal.tree = termTree->print(term); } - + return returnVal; } diff --git a/parser.h b/parser.h index e4299dc..0aeaae9 100644 --- a/parser.h +++ b/parser.h @@ -1,9 +1,6 @@ +#include #include -#include -#include #include -#include - #include "tree.h" struct ParserResult diff --git a/tree.cpp b/tree.cpp index 3a44833..ee45e57 100644 --- a/tree.cpp +++ b/tree.cpp @@ -1,8 +1,6 @@ #include "tree.h" #include -#include - Node::Node() { @@ -111,12 +109,12 @@ string Tree::print(string term) switch ( (*it)->type ) { case OPERAND_NODE: { OperandNode *tmp = static_cast( *it ); - out << "node" << i << " [ label = \"" << boost::lexical_cast(tmp->value) << "\"];" << endl; + out << "node" << i << " [ label = \"" << tmp->value << "\"];" << endl; break; } case OPERATOR_NODE: { OperatorNode *tmp = static_cast( *it ); - out << "node" << i << " [ label = \"" << boost::lexical_cast(tmp->function) << "\"];" << endl; + out << "node" << i << " [ label = \"" << tmp->function << "\"];" << endl; for ( vector::iterator iter = this->nodeCollection->begin(); iter != this->nodeCollection->end(); ++iter ) { if ( *iter == (*it)->leftChild ) { diff --git a/tree.h b/tree.h index 48f4de1..2a0bb84 100644 --- a/tree.h +++ b/tree.h @@ -1,6 +1,7 @@ #include #include -#include +#include +#include using namespace std; -- cgit v1.2.3