-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cpp
136 lines (111 loc) · 2.78 KB
/
Parser.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
#include "Parser.h"
Parser::Parser(void)
{
const unsigned numOps = 5;
std::string ops[numOps] = {"+","*","^","-","/"};
auto add = [](float a, float b){return a+b;};
const char* plus = "+";
Operator<float> *addition = new Operator<float>(plus,add,2);
_operators.push_back(addition);
auto mult = [](float a, float b){return a*b;};
const char* star = "*";
Operator<float> *multiplication = new Operator<float>(star,mult,2);
_operators.push_back(multiplication);
auto div = [](float a, float b){return a/b;};
const char* slash = "/";
Operator<float> *division = new Operator<float>(slash,div,2);
_operators.push_back(division);
auto power = [](float a,float b){return pow(a,b);};
const char* carrot = "^";
Operator<float> *exponent = new Operator<float>(carrot,power,2);
_operators.push_back(exponent);
auto negate = [](float a,float b){return -b;};
const char* minus = "-";
Operator<float> *negative = new Operator<float>(minus,negate,1);
_operators.push_back(negative);
}
Parser::~Parser(void)
{
_operators.clear();
}
float Parser::ParseInput(char *in)
{
NewInput(in);
unsigned len = strlen(in);
float solution;
if(len>1)
{
solution = BuildSubTree(ExpressionInterval(0,len-1),0);
}
else if(len == 1)
{
solution = Leaf(ExpressionInterval(0,0));
}
return solution;
}
void Parser::NewInput(char *in)
{
_inputString = in;
}
bool Parser::StrCompare(char *one, const char* two, unsigned numOfChars)
{
int diff = 0;
for(unsigned i=0;i<numOfChars;i++)
{
diff = (*(one+i)) - (*(two+i));
}
return (diff == 0);
}
float Parser::BuildSubTree(ExpressionInterval expression, int delimiter)
{
int k = expression._start;
bool isFound = false;
float lhs = 0, rhs = 0, result = 0;
if(delimiter < _operators.size() && (expression._end < strlen(_inputString) && expression._start <= expression._end))
{
while(k <= expression._end && isFound == false)
{
if(StrCompare(_inputString+k, _operators[delimiter]->_symbol,1))
{
switch(_operators[delimiter]->_arity)
{
case 2:
lhs = BuildSubTree(ExpressionInterval(expression._start,k-1),delimiter+1);
break;
case 1:
default:
break;
}
rhs = BuildSubTree(ExpressionInterval(k+1,expression._end),0);
isFound = true;
}
k++;
}
if(!isFound)
{
result = BuildSubTree(expression,delimiter+1);
}
else
{
result = _operators[delimiter]->Fire(lhs,rhs);
}
}
else
{
result = Leaf(expression);
}
return result;
}
/*
safely delimit acording to the next order of operations
*/
float Parser::Leaf(ExpressionInterval expression)
{
unsigned siz = expression._end - expression._start + 1;
char *number = new char[siz + 1];
strncpy(number,_inputString+expression._start,siz);
number[siz] = '\0';
double result = atof(number);
delete []number;
return float(result);
}