-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.cpp
235 lines (218 loc) · 6.25 KB
/
play.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
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
#include <bits/stdc++.h>
using namespace std;
const int OUTPUT_WIDTH = 3;
#define ASSERT_EXCEPTION(cond, msg) do { if (!(cond)) throw msg; } while(0)
typedef bool T;
function<T(vector<T>)> f_equiv = [](vector<T> input) {
return input[0] == input[1];
};
function<T(vector<T>)> f_implies = [](vector<T> input) {
return !input[0] || input[1];
};
function<T(vector<T>)> f_or = [](vector<T> input) {
return input[0] || input[1];
};
function<T(vector<T>)> f_and = [](vector<T> input) {
return input[0] && input[1];
};
function<T(vector<T>)> f_not = [](vector<T> input) {
return !input[0];
};
function<T(vector<T>)> f_id = [](vector<T> input) {
return input[0];
};
struct Operator {
string name;
int n_operands;
function<T(vector<T>)> calculate;
Operator(string name, int n_operands, function<T(vector<T>)> calc)
: name(name), n_operands(n_operands), calculate(calc) {}
};
bool operator == (const Operator &A, const Operator &B) {
return A.name == B.name;
}
const Operator o_equiv("=", 2, f_equiv);
const Operator o_implies(">", 2, f_implies);
const Operator o_or("v", 2, f_or);
const Operator o_and("^", 2, f_and);
const Operator o_not("!", 1, f_not);
const Operator o_id("?", 1, f_id);
const vector<Operator> operators = {o_equiv, o_implies, o_or, o_and, o_not};
const vector<T> value_set = {0, 1};
vector<string> symbol_set;
map<string, T> value_of_symbols;
struct Node {
vector<Node*> chs;
Operator op;
string symbol;
Node(Operator op) : op(op), symbol("") {}
Node(string symbol) : op(o_id), symbol(symbol) {}
T value_cache;
T evaluate() {
if (op == o_id) {
return value_cache = value_of_symbols[symbol];;
} else {
vector<T> operands(chs.size());
for (size_t i = 0; i < chs.size(); ++i) operands[i] = chs[i]->evaluate();
return value_cache = op.calculate(operands);
}
}
void print_heading() {
if (op == o_id) {
cout << setw(OUTPUT_WIDTH) << symbol;
} else {
cout << setw(OUTPUT_WIDTH) << op.name;
for (auto ch : chs) ch->print_heading();
}
}
void print_content() {
if (op == o_id) {
cout << setw(OUTPUT_WIDTH) << value_cache;
} else {
cout << setw(OUTPUT_WIDTH) << value_cache;
for (auto ch : chs) ch->print_content();
}
}
};
int locate(string s, string op) {
int dep = 0;
for (int i = 0; i < (int)s.size(); ++i) {
if (s[i] == '(') dep++;
if (s[i] == ')') dep--;
if (dep == 0 && s.substr(i, op.size()) == op)
return i;
}
return -1;
}
Node* resolve(string s) {
// find an operator
if (s.size() == 0) return nullptr;
for (auto op : operators) {
int pos = locate(s, op.name);
if (pos == -1) continue;
Node* u = new Node(op); // an operator
// cerr << op.name << endl;
Node *L = resolve(s.substr(0, pos));
Node *R = resolve(s.substr(pos + op.name.size()));
if (L) u->chs.push_back(L);
if (R) u->chs.push_back(R);
if (op.n_operands == 2) ASSERT_EXCEPTION(L && R,
"An occurrence of " + op.name + "got wrong numbers of input: "
+ (L ? "" : "L missed ") + (R ? "" : "R missed"));
if (op.n_operands == 1) ASSERT_EXCEPTION(!L && R,
"An occurrence of " + op.name + "got wrong input: "
+ (L ? "L shouldnt be there" : "") + (R ? "" : "R missed"));
return u;
}
// no operands
if (s[0] == '(' && s.back() == ')') {
return resolve(s.substr(1, s.size() - 2));
}
// a symbol
Node *u = new Node(s);
// cerr << "symbol" << endl;
symbol_set.push_back(s);
return u;
}
void trimspaces(string &s) // from Internet
{
int index = 0;
if(!s.empty())
{
while( (index = s.find(' ',index)) != string::npos)
{
s.erase(index,1);
}
}
}
bool checkBracesMatch(string s) {
int dep = 0;
for (size_t i = 0; i < s.size(); ++i) {
if (s[i] == '(') dep++;
if (s[i] == ')') dep--;
if (s[i] < 0) return false;
}
return dep == 0;
}
bool checkAlphabet(string s) {
set<char> alphabet;
for (char c = 'a'; c <= 'z'; ++c) alphabet.insert(c);
for (char c = 'A'; c <= 'Z'; ++c) alphabet.insert(c);
for (auto op : operators) for (char c : op.name) alphabet.insert(c);
alphabet.insert('(');
alphabet.insert(')');
alphabet.insert(' ');
for (char c : s) if (!alphabet.count(c)) return false;
return true;
}
Node *root;
void print_heading() {
cout << "format: symbols | prefix notation" << endl;
for (string s : symbol_set) {
cout << setw(OUTPUT_WIDTH) << s;
}
cout << setw(OUTPUT_WIDTH) << "|";
root->print_heading();
cout << endl;
}
void print_content() {
for (string s : symbol_set) {
cout << setw(OUTPUT_WIDTH) << value_of_symbols[s];
}
cout << setw(OUTPUT_WIDTH) << "|";
root->print_content();
cout << endl;
}
void dfs(int depth) {
if (depth == symbol_set.size()) {
root->evaluate();
print_content();
} else {
for (auto val : value_set) {
value_of_symbols[symbol_set[depth]] = val;
dfs(depth + 1);
}
}
}
int onecall() {
cout << "Please input a logical expression, with =>^v! as five possible operators(in ascending priority)," << endl;
cout << "any combination of uppercase/lowercase string(except for 'v') as symbols." << endl;
string s;
getline(cin, s);
// if (s == "STOP") return -1;
if (!checkAlphabet(s)) {
cerr << "Fail: Bad alphabet" << endl;
cerr << "Should be a~z, A~Z, space, (), or the following operators:" << endl;
for (auto op : operators) cerr << op.name << " ";
cerr << endl;
return 1;
}
if (!checkBracesMatch(s)) {
cerr << "Fail: Braces not matching." << endl;
return 2;
}
trimspaces(s);
symbol_set.clear();
value_of_symbols.clear();
try {
root = resolve(s);
} catch (string e) {
cerr << "Error: " << e << endl;
cerr << "Anyway your expression is flawed. Bye." << endl;
return 3;
}
cerr << "Expression resolved" << endl;
// with the expression tree, lets make it happen
sort(symbol_set.begin(), symbol_set.end());
symbol_set.erase(unique(symbol_set.begin(), symbol_set.end()), symbol_set.end());
print_heading();
dfs(0);
return 0;
}
int main() {
int res = 99;
while (res) {
res = onecall();
}
return 0;
}