-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpTree.h
More file actions
142 lines (116 loc) · 4.08 KB
/
ExpTree.h
File metadata and controls
142 lines (116 loc) · 4.08 KB
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
/*
@file ExpTree.h */
#ifndef _EXP_TREE
#define _EXP_TREE
#include "BT.h"
#include <string>
using namespace std;
class ExpTree : public binary_tree<string>
{
private:
// vector<symtolType> symbolTable;
protected:
node<string> * makeExpTree(string s, int f, int l);
// Part 2 - Recursive funtion that will return true id eval is successful
bool eval(node<string>* rt, double &result);
public:
void makeExpTree(string create);
bool eval(double &result); // or double eal();
}; // end BinaryNodeTree
//#include "ExpTree.cpp"
node<string> * ExpTree::makeExpTree(string s, int f, int l) {
int parentheses = 0;
int lowestPriority = 999999;
int indexOP = -1;
// Check the string from left to right
for (int i = f; i <= l; ++i) {
//Checks if current i is a parenthesses
if (s[i] == '(') {
parentheses++;
}
else if (s[i] == ')') {
parentheses--;
}
else if (parentheses == 0 && (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')) { // Inside of parentheses
int priority; // Priority of symbols
if (s[i] == '+' || s[i] == '-') {
priority = 1;
}
else {
priority = 2;
}
if (priority <= lowestPriority) {
lowestPriority = priority;
indexOP = i;
}
}
}
// If theres none operators, then it is a number
if (indexOP == -1) {
// Check for outer parentheses
if (s[f] == '(' && s[l] == ')') {
return makeExpTree(s, f + 1, l - 1);
}
else {
return new node<string>(s.substr(f, l - f + 1));
}
}
// Creates a new node for the operator
node<string>* root = new node<string>(string(1, s[indexOP]));
// Recursively build left and right subtrees
root->left = makeExpTree(s, f, indexOP - 1);
root->right = makeExpTree(s, indexOP + 1, l);
return root;
}
bool ExpTree::eval(node <string>* rt, double& result) {
if (!root) { // Without a root a tree is ubable to be formed
return false;
}
// If there is a root, need to move throughout the tree
double leftPlace; // Evaluating to the left of the node
double rightPlace; // Evaluating to the right of the nodes
if (isdigit(rt->item[0])) { // If passed in value is a digit, evaluate numbers
result = stod(rt->item); // Stod(): makes integer that's passed through into double (utilizing decimal placing)
return true;
}
// If current node has digits on the left and the right execute
if (eval(rt->left, leftPlace) && eval(rt->right, rightPlace)) {
if (rt->item == "+") {
result = leftPlace + rightPlace; // Result of addition
}
else if (rt->item == "-") {
result = leftPlace - rightPlace; // Result of subtraction
}
else if (rt->item == "*") {
result = leftPlace * rightPlace; // Result of multiplication
}
else if (rt->item == "/") {
if (rightPlace != 0) // If the denominator is not 0 we can do division
result = leftPlace / rightPlace; // Result of division
else { // If the denominator is 0, division can not be done
cout << "Inf" << endl; // Requirement
return false;
}
}
else {
cerr << "Evaluation Failed" << endl; // Requirement
return false;
}
return true;
}
return false;
}
// Creating the tree in assigned order
void ExpTree::makeExpTree(string create) {
root = makeExpTree(create, 0, create.length() - 1);
}
// Expression tree funciton that assigns result to the reference argument
bool ExpTree::eval(double &result) {
if (root != nullptr) { // If root is not null, return eval() that tree exists
return eval(root, result); // Recursion functions
}
else {
cout << "Evaluation Failed"; // Requirement
}
}
#endif