-
Notifications
You must be signed in to change notification settings - Fork 3
/
LogiCalc.cxx
216 lines (188 loc) · 4.61 KB
/
LogiCalc.cxx
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
/*
*
*
*/
#ifndef LOGICALC_CXX
#define LOGICALC_CXX
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <algorithm>
#include "infixtorp.cxx"
class LogiCalc
{
public:
LogiCalc(){};
virtual ~LogiCalc(){};
std::vector<std::string> & SetFormula(std::string);
std::string& GetFormula();
int GetSigMax(){return fSigMax;};
bool Calc(uint32_t);
void Dump(){};
protected:
private:
bool ExtractBit(uint32_t, int);
//std::vector<std::string> &Split(std::string);
int ComPushBack(std::string &);
std::vector<std::string> fCommands;
std::stack<bool> fStack;
int fNsigMax = 32;
int fSigMax = 0;
const std::vector<std::string> fFuncs = {"&", "|", "!", "x", "d"};
const std::string fSeparator = std::string(" ");
};
bool LogiCalc::ExtractBit(uint32_t val, int digit)
{
if (digit < 32) {
uint32_t bit = (val >> digit) & 0x00000001;
return (bit > 0) ? true : false;
} else {
return false;
}
}
int LogiCalc::ComPushBack(std::string &word)
{
if (word.length() > 0) {
if (std::all_of(word.cbegin(), word.cend(), isdigit)) {
int sig = atoi(word.c_str());
if ((sig >= 0) && (sig < fNsigMax)) {
fCommands.push_back(word);
if (sig > fSigMax) fSigMax = sig;
} else {
std::cout << "#E Over range : " << sig << std::endl;
}
} else {
bool hit = false;
for (auto & fword : fFuncs) {
if (word == fword) {
fCommands.push_back(word);
hit = true;
break;
}
}
if (! hit) std::cout << "#E Unknown talken : " << word << std::endl;
}
}
return fCommands.size();
}
std::vector<std::string> & LogiCalc::SetFormula(std::string sparam)
{
std::string sform;
if ((sparam.substr(0,3) == "RPN") || (sparam.substr(0,3) == "rpn")) {
sform = sparam.substr(3);
} else {
sform = infixtorp(sparam)->to_rpn();
}
#if 0
std::cout << "#D input formula: " << sparam << std::endl;
std::cout << "#D rpn formula : " << sform << std::endl;
#endif
fCommands.clear();
fCommands.resize(0);
fSigMax = 0;
int slen = fSeparator.length();
auto offset = std::string::size_type(0);
while (true) {
auto pos = sform.find(fSeparator, offset);
if (pos == std::string::npos) {
auto word = sform.substr(offset);
if (word.length() > 0) ComPushBack(word);
break;
}
auto word = sform.substr(offset, pos - offset);
if (word.length() > 0) ComPushBack(word);
offset = pos + slen;
}
return fCommands;
}
bool LogiCalc::Calc(uint32_t val)
{
while (! fStack.empty()) fStack.pop();
for (auto & com : fCommands) {
if (std::all_of(com.cbegin(), com.cend(), isdigit)) {
int sig = atoi(com.c_str());
fStack.push(ExtractBit(val, sig));
} else {
if (fStack.size() > 1) {
if ((com == "&") || (com == "*")) {
auto val1 = fStack.top(); fStack.pop();
auto val2 = fStack.top(); fStack.pop();
fStack.push(val1 & val2);
}
if ((com == "|") || (com == "+")) {
auto val1 = fStack.top(); fStack.pop();
auto val2 = fStack.top(); fStack.pop();
fStack.push(val1 | val2);
}
if (com == "x") {
auto val1 = fStack.top(); fStack.pop();
auto val2 = fStack.top(); fStack.pop();
fStack.push(val1);
fStack.push(val2);
}
}
if ((com == "!") || (com == "^")) {
auto vtop = fStack.top(); fStack.pop();
fStack.push(! vtop);
}
if ((com == "d") || (com == "p")) {
if (fStack.size() > 0) {
fStack.pop();
} else {
std::cout << "#E empty stack" << std::endl;
}
}
}
}
bool ret;
if (fStack.size() > 0) {
ret = fStack.top(); fStack.pop();
} else {
std::cout << "#E empty stack result" << std::endl;
ret = false;
}
return ret;
}
#ifdef LOGICALC_TEST_MAIN
int main(int argc, char *argv[])
{
std::string formula;
if (argc > 1) {
formula = argv[1];
} else {
//formula = "RPN 0 1 & 2 3 & | 4 5 & 6 7 & | &";
formula = "((0 & 1) | (2 & 3)) & ((4 & 5) | (6 & 7))";
}
LogiCalc calc;
#if 0
const std::vector<std::string> form = {
"1 2 & 3 4 & |",
"32 && 3 4 & | *"};
for (auto & f : form) {
auto & commands = calc.SetFormula(f);
for (auto & com : commands) {
std::cout << "-" << com;
}
std::cout << std::endl;
}
#endif
auto & commands = calc.SetFormula(formula);
std::cout << "Formula(RPN): ";
for (auto & com : commands) std::cout << " " << com;
uint32_t range = 1 << (calc.GetSigMax() +1);
for (uint32_t i = 0 ; i < range ; i++) {
if ((i % 16) == 0) std::cout << std::endl;
std::cout << " " << calc.Calc(i);
}
#if 1
for (uint32_t i = 0 ; i < range ; i++) {
if ((i % 16) == 0) std::cout << std::endl;
std::cout << " " << std::hex << std::setw(2) << i << ":" << calc.Calc(i);
}
#endif
std::cout << std::dec << std::endl;
return 0;
}
#endif
#endif //LOGICALC_CXX