-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogicCalc2.cpp
More file actions
121 lines (112 loc) · 2.51 KB
/
LogicCalc2.cpp
File metadata and controls
121 lines (112 loc) · 2.51 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
#include <iostream>
#include <stdlib.h>
#include "LogicCalc2.h"
using namespace std;
LogicCalc::LogicCalc(string ekspresi, int mode){
this->ekspresi = ekspresi;
this->mode = mode;
}
LogicCalc::~LogicCalc(){
// not needed
}
bool LogicCalc::Calculate(){
bool hasil;
if(mode == 1){
hasil = CalculatePrefix();
}
else if (mode == 2){
hasil = CalculateInfix();
}
else if(mode == 3){
hasil = CalculatePostfix();
}
return hasil;
}
bool 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);
operand << x;
}
else if(ekspresi[i] == '&' || ekspresi[i] == '|' || ekspresi[i]=='~'){
operatorx << ekspresi.substr(i,1);
operand << Evaluate();
}
}
x = operand.getLastData();
if(operand.getLastData() == 0){
jawab = "false";
}
else{
jawab = "true";
}
cout << "Answer is " << jawab << endl;
return x;
}
bool LogicCalc::CalculateInfix(){
}
bool 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);
operand << x;
}
else if(ekspresi[i] == '&' || ekspresi[i] == '|' || ekspresi[i]=='~'){
operatorx << ekspresi.substr(i,1);
operand << Evaluate();
}
}
x = operand.getLastData();
if(operand.getLastData() == 0){
jawab = "false";
}
else{
jawab = "true";
}
cout << "Answer is " << jawab << endl;
return x;
}
bool LogicCalc::Evaluate(){
bool ans, op1, op2;
string operat;
this->operatorx >> operat;
if (operat == "&"){
operand >> op1;
operand >> op2;
ans = op1 & op2;
cout << op1 <<" & " << op2 << "=" << ans << endl;
}
else if(operat == "|"){
operand >> op1;
operand >> op2;
ans = op1 | op2;
cout << op1 <<" | " << op2 << "=" << ans << endl;
}
else if(operat=="~"){
operand >> op1;
if (op1 == 1){
ans = false;
}
else{
ans = true;
}
cout << "~"<<op1 << "=" << ans << endl;
}
return ans;
}