-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConditionParser.cpp
153 lines (138 loc) · 3.19 KB
/
ConditionParser.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
//
// Created by franckito on 12/20/18.
//
#include "ConditionParser.h"
#include "GlobalTables.h"
#include "ShuntingYard.h"
/**
* function name: conditionReader
* operation: read and parse condition
* input: vec of strings and index
* @return int
*/
int ConditionParser::conditionReader(vector<string> cmdLines, int index){
int posOp = opCheck(cmdLines, index);
int endLine = enterKey(cmdLines, index);
string temp1 = vectorToString(cmdLines, index + 1, posOp - 1);
string temp2 = vectorToString(cmdLines, index + posOp + 1, endLine - posOp - 2);
this->a = temp1;
this->op = cmdLines.at(index + posOp);
this->b = temp2;
return endLine;
}
/**
* function name: check
* operation: check keys and calculate
* input: none
* @return boolean
*/
bool ConditionParser::check(){
ShuntingYard* sY = new ShuntingYard();
GlobalTables* gT = new GlobalTables();
double temp1 = 0, temp2 = 0;
if(gT->keyExists(a)) {
temp1 = symbolTable.at(a);
} else
temp1 = sY->calculate(a);
if(gT->keyExists(b)) {
temp2 = symbolTable.at(b);
} else
temp2 = sY->calculate(b);
delete sY;
delete gT;
if(op == "!="){
if(temp1 != temp2){
return true;
}
else
return false;
}
else if(op == "=="){
if(temp1 == temp2){
return true;
}
else
return false;
}
else if(op == ">="){
if(temp1 >= temp2){
return true;
}
else
return false;
}
else if(op == "<="){
if(temp1 <= temp2){
return true;
}
else
return false;
}
else if(op == "<"){
if(temp1 < temp2){
return true;
}
else
return false;
}
else if(op == ">"){
if(temp1 > temp2){
return true;
}
else
return false;
}
return false;
}
/**
* function name: execute
* operation: execute data
* input: vec of strs and index
* @return int val
*/
int ConditionParser::execute(vector<string> cmdTemp, int index) {
return 0;
}
/**
* function name: opCheck
* operation: check keys and calculate
* input: vec of strs and index
* @return int val
*/
int ConditionParser::opCheck(vector<string> vector, int index) {
int i = 0;
string s = vector.at(index);
while(s != ">" && s != "<" && s != "<=" && s != ">=" && s != "==" && s != "!=") {
s = vector.at(index + i);
i++;
}
return i - 1;
}
/**
* function name: vectorToString
* operation: convert vector to string
* input: vec of strs and index
* @return string val
*/
string ConditionParser::vectorToString(vector<string> vector, int index, int end) {
string ret = vector.at(index);
int i = 1;
while(index + i < index + end){
ret += vector.at(index + i);
i++;
}
return ret;
}
/**
* function name: enterKey
* operation: enter a key
* input: vec of strs and index
* @return int val
*/
int ConditionParser::enterKey(vector<string> vector, int index) {
int i = 0;
while(vector.at(index + i) != "\n") {
i++;
}
return i - 2;
}