aboutsummaryrefslogtreecommitdiff
path: root/tree.h
blob: 4527095eea3b12b39bae34d8baf499a7e6bec350 (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
#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:
		Node* root;

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

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

	private:
		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_