aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-01-05 22:04:23 +0100
committerAdrian Kummerländer2013-01-05 22:04:23 +0100
commite3081360c65eb4994e7e8042898cec72de0d560b (patch)
tree2cac723733c674381ccaf32df5a64fe23b026467
parent0ab1ad8c67ac5579e10104f53040d962a7f98f17 (diff)
downloadSimpleParser-e3081360c65eb4994e7e8042898cec72de0d560b.tar
SimpleParser-e3081360c65eb4994e7e8042898cec72de0d560b.tar.gz
SimpleParser-e3081360c65eb4994e7e8042898cec72de0d560b.tar.bz2
SimpleParser-e3081360c65eb4994e7e8042898cec72de0d560b.tar.lz
SimpleParser-e3081360c65eb4994e7e8042898cec72de0d560b.tar.xz
SimpleParser-e3081360c65eb4994e7e8042898cec72de0d560b.tar.zst
SimpleParser-e3081360c65eb4994e7e8042898cec72de0d560b.zip
Folder structure change; Further improvements of parser code
-rw-r--r--Makefile2
-rw-r--r--main.cpp3
-rw-r--r--src/parser.cpp (renamed from parser.cpp)4
-rw-r--r--src/parser.h (renamed from parser.h)0
-rw-r--r--src/tree.cpp (renamed from tree.cpp)12
-rw-r--r--src/tree.h (renamed from tree.h)4
-rw-r--r--test.cpp2
7 files changed, 19 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index bfc1768..dcfeee7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-LIB_FILES = tree.cpp parser.cpp
+LIB_FILES = src/tree.cpp src/parser.cpp
PROG_FILES = main.cpp
TEST_FILES = test.cpp
diff --git a/main.cpp b/main.cpp
index 33788ae..0342e28 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,6 +1,7 @@
#include <iostream>
#include <limits>
-#include "parser.h"
+
+#include "src/parser.h"
int main(int argc, char *argv[])
{
diff --git a/parser.cpp b/src/parser.cpp
index 683d26f..3aaf7e2 100644
--- a/parser.cpp
+++ b/src/parser.cpp
@@ -182,9 +182,9 @@ Node* Parser::buildTree(Tree *tree, std::string term) {
double Parser::calculate(std::string term) {
Tree termTree;
- termTree.root = this->buildTree(&termTree, term);
+ termTree.setRoot(this->buildTree(&termTree, term));
- return termTree.root->solve();
+ return termTree.solve();
}
}
diff --git a/parser.h b/src/parser.h
index 12d745a..12d745a 100644
--- a/parser.h
+++ b/src/parser.h
diff --git a/tree.cpp b/src/tree.cpp
index 2fd5deb..c8f0aac 100644
--- a/tree.cpp
+++ b/src/tree.cpp
@@ -64,7 +64,15 @@ char OperatorNode::getFunction() {
return this->function_;
}
-Node* Tree::addOperand(Node **place, double value) {
+void Tree::setRoot(Node* root) {
+ this->root_node_ = root;
+}
+
+double Tree::solve() {
+ return this->root_node_->solve();
+}
+
+Node* Tree::addOperand(Node** place, double value) {
this->node_collection_.emplace_back(new OperandNode(value));
if ( place != nullptr ) {
@@ -74,7 +82,7 @@ Node* Tree::addOperand(Node **place, double value) {
return this->node_collection_.back().get();
}
-Node* Tree::addOperator(Node **place, char oper) {
+Node* Tree::addOperator(Node** place, char oper) {
this->node_collection_.emplace_back(new OperatorNode(oper));
if ( place != nullptr ) {
diff --git a/tree.h b/src/tree.h
index 4527095..4177098 100644
--- a/tree.h
+++ b/src/tree.h
@@ -55,7 +55,8 @@ class OperandNode: public Node {
class Tree {
public:
- Node* root;
+ void setRoot(Node*);
+ double solve();
Node* addOperand(Node**, double);
Node* addOperator(Node**, char);
@@ -63,6 +64,7 @@ class Tree {
std::string print(std::string);
private:
+ Node* root_node_;
std::vector<std::unique_ptr<Node>> node_collection_;
};
diff --git a/test.cpp b/test.cpp
index 04837ee..a752ad3 100644
--- a/test.cpp
+++ b/test.cpp
@@ -1,4 +1,4 @@
-#include "parser.h"
+#include "src/parser.h"
#include "gtest/gtest.h"
class ParserTest : public ::testing::Test {