-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogicCalc.cpp
More file actions
333 lines (312 loc) · 6.22 KB
/
LogicCalc.cpp
File metadata and controls
333 lines (312 loc) · 6.22 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* @author
* @file LogicCalc.cpp
* @class LogicCalc
* @brief Kelas untuk pengolahan ekspresi dengan operand boolean
*/
#include <iostream>
#include <stdlib.h>
#include "LogicCalc.h"
using namespace std;
//tools debug
void LogicCalc::Print(Stack<int> bil,Stack<string>operatorx){
cout<<"Stack bilangan"<<endl;
for(int i = 0;i<=bil.getDataCount()-1;i++)
{
cout<<i<<"). "<<bil.getData(i)<<endl;
}
cout<<"Stack operator"<<endl;
for(int j = 0;j<=operatorx.getDataCount()-1;j++)
{
cout<<j<<"). "<<operatorx.getData(j)<<endl;
}
}
/**
* @fn LogicCalc(string ekspresi,int mode,int size)
* @brief Konstruktor parameter kalkulator logika
*/
LogicCalc::LogicCalc(string ekspresi,int mode,int size) :bil(size),operatorx(size){
this->ekspresi = ekspresi;
this->mode = mode;
}
/**
* @fn ~LogicCalc()
* @brief Destruktor kalkulator logika
*/
LogicCalc::~LogicCalc(){
//no memory to free
}
/**
* @fn Calculate()
* @brief Penghitungan sesuai mode ekspresi (prefix, infix, postfix)
*/
string LogicCalc::Calculate(){
int hasil;
string hasilstr;
if(mode == 1)
{
hasil = CalculatePrefix();
}
else if(mode == 2)
{
hasil = CalculateInfix();
}
else if(mode == 3)
{
hasil = CalculatePostfix();
}
if(hasil == 1)
{
hasilstr = "true";
}
else if(hasil == 0)
{
hasilstr = "false";
}
else
{
hasilstr = "Undefined";
}
return hasilstr;
}
/**
* @fn SmallCalculate(Stack<int> &bil,Stack<string> &operatorx)
* @brief Prosedur penentu operator untuk perhitungan mode infix
*/
void LogicCalc::SmallCalculate(Stack<int> &bil,Stack<string> &operatorx)
{
int popbil1;
int popbil2;
string dumpoperator;
int dumpbil;
string popoperator = operatorx.getLastData();
operatorx>>dumpoperator;
if(popoperator == "~")
{
if(!bil.isEmpty() && bil.getLastData() == -999)
{
bil>>dumpbil; //Pop -999
}
else if(!operatorx.isEmpty() && operatorx.getLastData() == "~")
{
operatorx>>dumpoperator;
}
else
{
bil<< -999; //counter untuk merubah bil masukan berikut nya menjadi negasi dari input
}
}
else if(popoperator == "&")
{
popbil1 = bil.getLastData();
bil>>dumpbil;
popbil2 = bil.getLastData();
bil>>dumpbil;
if(popbil1 && popbil2)
{
bil<< 1;
}
else
{
bil<< 0;
}
}
else if(popoperator == "|")
{
popbil1 = bil.getLastData();
bil>>dumpbil;
popbil2 = bil.getLastData();
bil>>dumpbil;
if(popbil1 || popbil2)
{
bil<< 1;
}
else
{
bil<< 0;
}
}
}
/**
* @fn CalculateInfix()
* @brief Penghitungan mode infix
*/
int LogicCalc::CalculateInfix(){
string dumpoperator;
int dumpbil;
int hasil;
string ekspresitemp = ekspresi;
string temp;
while(ekspresitemp.length() != 0)
{
int isCompleteRead = 0;
int ctemp = 0; //Menghitung panjang buffer angka
if(ekspresitemp[0] == '0' || ekspresitemp[0] == '1')
{
if(bil.isEmpty())
{
if(ekspresitemp[0] == '1')
{
bil<<1;
}
else if(ekspresitemp[0] == '0')
{
bil<<0;
}
}
else // stack bil mempunyai 1 elemen
{
if(bil.getLastData() == -999) //cek counter untuk merubah bil masukan menjadi negasi dari input
{
if(!operatorx.isEmpty())
{
if(operatorx.getLastData() != "{")
{
bil>>dumpbil;
if(ekspresitemp[0] == '1')
{
bil<<0;
}
else if(ekspresitemp[0] == '0')
{
bil<<1;
}
}
}
else
{
bil>>dumpbil;
if(ekspresitemp[0] == '1')
{
bil<<0;
}
else if(ekspresitemp[0] == '0')
{
bil<<1;
}
}
}
else
{
if(ekspresitemp[0] == '1')
{
bil<<1;
}
else if(ekspresitemp[0] == '0')
{
bil<<0;
}
}
}
}
if(ekspresitemp[0] == '~' || ekspresitemp[0] == '&' || ekspresitemp[0] == '|' || ekspresitemp[0] == '(' || ekspresitemp[0] == ')')
{
if(ekspresitemp[0] == '~')
{
operatorx<<ekspresitemp.substr(0,1);
SmallCalculate(bil,operatorx);
}
if((operatorx.isEmpty() || ekspresitemp[0] == '(') && ekspresitemp[0] != '~')
{
operatorx<<ekspresitemp.substr(0,1);
}
else
{
if(ekspresitemp[0] == '&' || ekspresitemp[0] == '|')
{
if(operatorx.getLastData() == "~")
{
SmallCalculate(bil,operatorx);
operatorx<<ekspresitemp.substr(0,1);
}
else //operatorx.getLastData() == "~"
{
operatorx<<ekspresitemp.substr(0,1);
}
}
else if(ekspresitemp[0] == ')')
{
while(operatorx.getLastData() != "(")
{
SmallCalculate(bil,operatorx);
}
operatorx>>dumpoperator;
if(!bil.isEmpty())
{
if(bil.getLastData() == -999)
{
bil>>dumpbil;
if(ekspresitemp[0] == '1')
{
bil<<0;
}
else if(ekspresitemp[0] == '0')
{
bil<<1;
}
}
}
}
}
}
ekspresitemp.erase(0,1);
}
while(!operatorx.isEmpty())
{
SmallCalculate(bil,operatorx);
}
hasil = bil.getLastData();
cout<<"Hasil : "<<hasil<<endl;
return hasil;
//End
}
/**
* @fn CalculatePrefix()
* @brief Penghitungan mode prefix
*/
int LogicCalc::CalculatePrefix() {
char buffer[15];
string jawab;
int i,j; //iterator
bool op1, op2, x; //operand dan hasil
int len; //panjang
len = this->ekspresi.length();
j = 0;
for(i=len-1; i>=0; i--){
if(ekspresi[i] == '0' || ekspresi[i]=='1'){
buffer[0] = ekspresi[i];
x = atoi(buffer);
bil << x;
}
else if(ekspresi[i] == '&' || ekspresi[i] == '|' || ekspresi[i]=='~'){
operatorx << ekspresi.substr(i,1);
SmallCalculate(bil,operatorx);
}
}
return bil.getLastData();
}
/**
* @fn CalculatePostfix()
* @brief Penghitungan mode postfix
*/
int LogicCalc::CalculatePostfix() {
char buffer[15];
string jawab;
int i,j; //iterator
bool op1, op2, x; //operand dan hasil
int len; //panjang
len = this->ekspresi.length();
j = 0;
for(i=0; i<len;i++){
if(ekspresi[i] == '0' || ekspresi[i]=='1'){
buffer[0] = ekspresi[i];
x = atoi(buffer);
bil << x;
}
else if(ekspresi[i] == '&' || ekspresi[i] == '|' || ekspresi[i]=='~'){
operatorx << ekspresi.substr(i,1);
SmallCalculate(bil,operatorx);
}
}
return bil.getLastData();
}