diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | main.cpp | 3 | ||||
-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.cpp | 2 |
7 files changed, 19 insertions, 8 deletions
@@ -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 @@ -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(); } } @@ -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 ) { @@ -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_; }; @@ -1,4 +1,4 @@ -#include "parser.h" +#include "src/parser.h" #include "gtest/gtest.h" class ParserTest : public ::testing::Test { |