aboutsummaryrefslogtreecommitdiff
path: root/src/tree.h
blob: 4177098290730b178b4bd1e07791985b9050f4cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef PARSER_NODE_H_
#define PARSER_NODE_H_

#include <vector>
#include <string>
#include <sstream>
#include <memory>
#include <cmath>

namespace SimpleParser {

enum NodeType {
	OPERAND_NODE,
	OPERATOR_NODE,
};

class Node {
	public:
		virtual ~Node() {};

		virtual double solve()      = 0;
		virtual NodeType getType()  = 0;
		virtual std::string print() = 0;
		
		Node* leftChild;
		Node* rightChild;
};

class OperatorNode: public Node {
	public:
		explicit OperatorNode(char);

		virtual double solve();
		virtual NodeType getType();
		virtual std::string print();

		char getFunction();

	private:
		char function_;
};


class OperandNode: public Node {
	public:
		explicit OperandNode(double);

		virtual double solve();
		virtual NodeType getType();
		virtual std::string print();

	private:
		double value_;
};

class Tree {
	public:
		void setRoot(Node*);
		double solve();

		Node* addOperand(Node**, double);
		Node* addOperator(Node**, char);

		std::string print(std::string);

	private:
		Node* root_node_;
		std::vector<std::unique_ptr<Node>> node_collection_;
};

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.";
	}
};

}

#endif  // PARSER_NODE_H_