-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.h
63 lines (59 loc) · 2.14 KB
/
evaluator.h
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
#ifndef COURSEWORK_EVALUATOR_H
#define COURSEWORK_EVALUATOR_H
#include <math.h>
#include "tree.h"
#include "variables.h"
// evaluates the expression tree recursively.
double evaluate(tree_node* node)
{
token token = node->token;
switch (token.type)
{
case addition:
// add the left and right subtrees.
return evaluate(node->left_child) + evaluate(node->right_child);
case subtraction:
// subtract the right subtree from the left subtree.
return evaluate(node->left_child) - evaluate(node->right_child);
case multiplication:
// multiply the left and right subtrees.
return evaluate(node->left_child) * evaluate(node->right_child);
case division:
// divide the left subtree by the right subtree.
return evaluate(node->left_child) / evaluate(node->right_child);
case power:
// left subtree raised to the power of the right subtree.
return pow(evaluate(node->left_child), evaluate(node->right_child));
case negation:
// negate the right subtree.
return -evaluate(node->right_child);
case squareroot:
// square root of the left subtree.
return sqrt(evaluate(node->left_child));
case log_10:
// log base 10 of the left subtree.
return log10(evaluate(node->left_child));
case log_e:
// natural log of the left subtree.
return log(evaluate(node->left_child));
case sine:
// sin of the left subtree.
return sin(evaluate(node->left_child));
case cosine:
// cos of the left subtree.
return cos(evaluate(node->left_child));
case tangent:
// tan of the left subtree.
return tan(evaluate(node->left_child));
case constant:
// value of a constant.
return token.value;
case variable:
// value of a variable.
return token.value;
default:
// not a valid token type.
return 0;
}
}
#endif //COURSEWORK_EVALUATOR_H