diff options
Diffstat (limited to 'tree.h')
-rw-r--r-- | tree.h | 85 |
1 files changed, 47 insertions, 38 deletions
@@ -1,69 +1,78 @@ +#ifndef PARSER_NODE_H_ +#define PARSER_NODE_H_ + #include <vector> #include <string> #include <sstream> -#include <math.h> +#include <memory> +#include <cmath> -using namespace std; +namespace SimpleParser { -enum NodeType -{ +enum NodeType { OPERAND_NODE, OPERATOR_NODE, }; -class Node -{ +class Node { public: - Node(); - double solve(); + virtual ~Node() {}; - template <class T> - double castSolve(Node*); + virtual double solve() = 0; + virtual NodeType getType() = 0; + virtual std::string print() = 0; - Node *leftChild; - Node *rightChild; - - NodeType type; + Node* leftChild; + Node* rightChild; }; -class OperatorNode: public Node -{ +class OperatorNode: public Node { public: - OperatorNode(); - double solve(); - char function; + explicit OperatorNode(char); + + virtual double solve(); + virtual NodeType getType(); + virtual std::string print(); + + char getFunction(); + + private: + char function_; }; -class OperandNode: public Node -{ +class OperandNode: public Node { public: - OperandNode(); - double solve(); - double value; + explicit OperandNode(double); + + virtual double solve(); + virtual NodeType getType(); + virtual std::string print(); + + private: + double value_; }; -class Tree -{ +class Tree { public: - Tree(); - ~Tree(); - - Node *root; - + Node* root; + Node* addOperand(Node**, double); Node* addOperator(Node**, char); - - string print(string); - + + std::string print(std::string); + private: - vector<Node*> *nodeCollection; + std::vector<std::unique_ptr<Node>> node_collection_; }; -class divide_exception: public exception -{ +class divide_exception: public std::exception { virtual const char* what() const throw() { return "A divison through zero had to be prevented by the parser - check your input term."; } -};
\ No newline at end of file +}; + +} + +#endif // PARSER_NODE_H_ |