-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eval_Expr_Tree.cpp
163 lines (151 loc) · 4.18 KB
/
Eval_Expr_Tree.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Honor Pledge:
//
// I pledge that I have neither given nor
// received any help on this assignment.
//
// blakbenn
#include "Eval_Expr_Tree.h"
#include "Add_Node.h"
#include "Subtract_Node.h"
#include "Multiply_Node.h"
#include "Divide_Node.h"
#include "Mod_Node.h"
#include "Integer_Node.h"
//
// Constructor
//
Eval_Expr_Tree::Eval_Expr_Tree (void)
{
}
//
// Destructor
//
Eval_Expr_Tree::~Eval_Expr_Tree (void)
{
}
//
// Visit Add Node
//
void Eval_Expr_Tree::Visit_Add_Node(Add_Node & add_node)
{
//visit left and right children
add_node.getRightChild()->accept(*this);
add_node.getLeftChild()->accept(*this);
//pull first number from the stack
int number1 = this->Result_Stack.top();
this->Result_Stack.pop();
//pull second number from the stack
int number2 = this->Result_Stack.top();
this->Result_Stack.pop();
//evaluate expression
int result = number1 + number2;
//push result to the stack
this->Result_Stack.push(result);
return;
}
//
// Visit Subtract Node
//
void Eval_Expr_Tree::Visit_Subtract_Node(Subtract_Node & subtract_node)
{
//visit left and right children
subtract_node.getRightChild()->accept(*this);
subtract_node.getLeftChild()->accept(*this);
//pull first number from the stack
int number1 = this->Result_Stack.top();
this->Result_Stack.pop();
//pull second number from the stack
int number2 = this->Result_Stack.top();
this->Result_Stack.pop();
//evaluate expression
int result = number1 - number2;
//push result to the stack
this->Result_Stack.push(result);
return;
}
//
// Visit Multiply Node
//
void Eval_Expr_Tree::Visit_Multiply_Node(Multiply_Node & multiply_node)
{
//visit left and right children
multiply_node.getRightChild()->accept(*this);
multiply_node.getLeftChild()->accept(*this);
//pull first number from the stack
int number1 = this->Result_Stack.top();
this->Result_Stack.pop();
//pull second number from the stack
int number2 = this->Result_Stack.top();
this->Result_Stack.pop();
//evaluate expression
int result = number1 * number2;
//push result to the stack
this->Result_Stack.push(result);
return;
}
//
// Visit Divide Node
//
void Eval_Expr_Tree::Visit_Divide_Node(Divide_Node & divide_node)
{
//visit left and right children
divide_node.getRightChild()->accept(*this);
divide_node.getLeftChild()->accept(*this);
//pull first number from the stack
int number1 = this->Result_Stack.top();
this->Result_Stack.pop();
//pull second number from the stack
int number2 = this->Result_Stack.top();
this->Result_Stack.pop();
//evaluate expression
if (number2 == 0)
{
throw divide_by_zero_exception();
return;
}
int result = number1 / number2;
//push result to the stack
this->Result_Stack.push(result);
return;
}
//
// Visit Mod Node
//
void Eval_Expr_Tree::Visit_Mod_Node(Mod_Node & mod_node)
{
//visit left and right children
mod_node.getRightChild()->accept(*this);
mod_node.getLeftChild()->accept(*this);
//pull first number from the stack
int number1 = this->Result_Stack.top();
this->Result_Stack.pop();
//pull second number from the stack
int number2 = this->Result_Stack.top();
this->Result_Stack.pop();
//evaluate expression
if (number2 == 0)
{
throw mod_by_zero_exception();
return;
}
int result = number1 % number2;
//push result to the stack
this->Result_Stack.push(result);
return;
}
//
// Visit Integer Node
//
void Eval_Expr_Tree::Visit_Integer_Node(Integer_Node & integer_node)
{
this->Result_Stack.push(integer_node.getValue());
return;
}
//
// Result
//
int Eval_Expr_Tree::result(void)
{
//return the result
return this->Result_Stack.top();
}