aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.cpp4
-rw-r--r--parser.cpp86
-rw-r--r--parser.h4
-rw-r--r--tree.cpp12
-rw-r--r--tree.h1
5 files changed, 58 insertions, 49 deletions
diff --git a/main.cpp b/main.cpp
index 5044f18..d12119e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -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 )
{
diff --git a/parser.cpp b/parser.cpp
index 968053d..7ce5cab 100644
--- a/parser.cpp
+++ b/parser.cpp
@@ -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;
diff --git a/parser.h b/parser.h
index 0aeaae9..e9cdde1 100644
--- a/parser.h
+++ b/parser.h
@@ -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
diff --git a/tree.cpp b/tree.cpp
index 0ef3a39..29dbab1 100644
--- a/tree.cpp
+++ b/tree.cpp
@@ -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 );
diff --git a/tree.h b/tree.h
index b112dcb..f2a7017 100644
--- a/tree.h
+++ b/tree.h
@@ -47,6 +47,7 @@ class Tree
{
public:
Tree();
+ ~Tree();
Node *root;