-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
625 lines (496 loc) · 15.1 KB
/
main.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <map>
// Some typedefs to make things less verbose.
typedef std::string string;
typedef std::vector<string> stringVector;
typedef stringVector::const_iterator stringIter;
typedef std::map<string, bool> boolMap;
// Parses the input into tokens. "(A v B) ^ C <-> D" becomes
// a vector with the following elements: "(", "A", "v", "B", ")", "^", "C",
// "<->", and "D".
// |
// v
stringVector getTokens(const string& inputString);
// Boolean functions to make it easier to understand how the Shunting-Yard
// algorithm works.
// |
// v
bool isOperator(const char& ch);
bool isParentheses(const char& ch);
bool isVariable(const string& s);
bool isOperator(const string& s);
bool isLeftParen(const string& s);
bool isRightParen(const string& s);
int getPrecedence(const string& s);
// Obtains the variables from the vector of tokens. A variable is any
// alphabet character that isn't "v", "c", or "t". Those are reserved for
// the OR operator, logical falsity, and logical trith, respectively.
// |
// v
stringVector getVariables(const stringVector& tokenVector);
// Uses Djikstra's Shunting-Yard Algorithm to turn infix notation into
// Reverse Polish Notation.
// |
// v
stringVector convertRPN(const stringVector& tokenVector);
// Given a vector in RPN and a map of Boolean values, evaluate the result.
// |
// v
bool evalRPN(const stringVector& tokenVector, const boolMap& bmap);
// Used inside evalRPN to evaluate individual pairs of arguments.
// |
// v
string evalExpression(const string& arg1, const string& arg2,
const string& op, const boolMap& bmap);
// Given an integer value, take the bits of that integer and use them to
// set the truth-false values in bmap.
// For example, providing 5 (101) as the generator integer and the elements of
// "p", "q", and "r" in the vector of variables will make bmap set the following
// values: bmap["p"] = true, bmap["q"] = false, bmap["r"] = true.
// |
// v
void setValues(boolMap& bmap, const stringVector& variableVector, int comboGen);
// Combines two vectors, eliminating duplicates,
// and also sorts them. This will then get plugged into setValues to generate
// truth-false tables for all variables.
// |
// v
stringVector combineVectors(const stringVector& vector1,
const stringVector& vector2);
void printBool(bool b);
void printSpaces(int n);
int main() {
stringVector tokenVector1, tokenVector2;
stringVector variableVector1, variableVector2;
stringVector variableVectorUnion;
stringVector RPNVector1, RPNVector2;
string inputString1, inputString2;
boolMap bmap;
bool result1, result2;
std::cout << "Premise: ";
getline(std::cin, inputString1);
std::cout << "Conclusion: ";
getline(std::cin, inputString2);
tokenVector1 = getTokens(inputString1);
tokenVector2 = getTokens(inputString2);
variableVector1 = getVariables(tokenVector1);
variableVector2 = getVariables(tokenVector2);
variableVectorUnion = combineVectors(variableVector1, variableVector2);
RPNVector1 = convertRPN(tokenVector1);
RPNVector2 = convertRPN(tokenVector2);
bool validConclusion = true;
// Set initial values of bmap. "c" and "t" never change. ComboGen
// is initially set to make every variable "true."
bmap["c"] = false;
bmap["t"] = true;
int comboGen = 1;
for(int i = 0; i < variableVectorUnion.size(); i++) {
comboGen *= 2;
}
comboGen--;
// Print out the top part of the output table.
int colWidth1 = inputString1.length();
int colWidth2 = inputString2.length();
for(stringIter i = variableVectorUnion.begin(); i !=
variableVectorUnion.end(); ++i) {
std::cout << *i << " ";
}
std::cout << "| ";
std::cout << inputString1 << " | " << inputString2 << "\n";
// Print the truth table values.
while(comboGen >= 0) {
setValues(bmap, variableVectorUnion, comboGen);
result1 = evalRPN(RPNVector1, bmap);
result2 = evalRPN(RPNVector2, bmap);
for(stringIter i = variableVectorUnion.begin();
i != variableVectorUnion.end(); ++i) {
printBool(bmap.at(*i));
std::cout << " ";
}
std::cout << "|";
printSpaces(colWidth1 / 2);
if(inputString1.length() % 2 == 1) {
std::cout << " ";
}
printBool(result1);
printSpaces(colWidth1 / 2);
std::cout << " |";
printSpaces(colWidth2 / 2);
if(inputString2.length() % 2 == 1) {
std::cout << " ";
}
printBool(result2);
printSpaces(colWidth2 / 2);
if(result1 != result2) {
std::cout << " Invalid!";
validConclusion = false;
}
std::cout << "\n";
comboGen--;
}
if(validConclusion) {
std::cout << "Valid statement. All truth table values line up.\n";
}
else {
std::cout << "Invalid statement. Invalid truth table values are "
<< "labelled.\n";
}
return 0;
}
stringVector getTokens(const string& inputString) {
stringVector tokenVector;
string currentString = "";
int i = 0;
while(i < inputString.length()) {
currentString = "";
// Test for "implies" operator.
if(inputString[i] == '-' && inputString.length() - i > 1) {
if(inputString.substr(i, 2) == "->") {
tokenVector.push_back("->");
i += 2;
}
else {
i++;
}
}
// Test for "XOR" operator.
else if(inputString[i] == 'X' && inputString.length() - i > 2) {
if(inputString.substr(i, 3) == "XOR") {
tokenVector.push_back("XOR");
i += 3;
}
else {
i++;
}
}
// Test for "iff" operator.
else if(inputString[i] == '<' && inputString.length() - i > 2) {
if(inputString.substr(i, 3) == "<->") {
tokenVector.push_back("<->");
i += 3;
}
else {
i++;
}
}
// Otherwise, it's hopefully an operator ('v', '^', '!', '(', or ')')
// or a character.
else if(isOperator(inputString[i]) || isParentheses(inputString[i])
|| isalpha(inputString[i])) {
currentString += inputString[i];
tokenVector.push_back(currentString);
i++;
}
// If it's not, then it's just ignored.
else {
i++;
}
}
return tokenVector;
}
bool isOperator(const char& ch) {
char validOperators[] = {'!', 'v', '^'};
for(int i = 0; i < 3; i++) {
if(ch == validOperators[i]) {
return true;
}
}
return false;
}
bool isParentheses(const char& ch) {
return ch == '(' || ch == ')';
}
bool isVariable(const string& s) {
return s.length() == 1 && isalpha(s[0]) && s[0] != 'v';
}
bool isOperator(const string& s) {
string validOperators[] = {"!", "v", "^", "->", "<->", "XOR"};
for(int i = 0; i < 6; i++) {
if(s == validOperators[i]) {
return true;
}
}
return false;
}
bool isLeftParen(const string& s) {
return s.length() == 1 && s[0] == '(';
}
bool isRightParen(const string& s) {
return s.length() == 1 && s[0] == ')';
}
int getPrecedence(const string& s) {
if(s == "<->") {
return 0;
}
if(s == "->") {
return 1;
}
if(s == "XOR") {
return 2;
}
if(s == "v") {
return 3;
}
if(s == "^") {
return 4;
}
if(s == "!") {
return 5;
}
else {
return -1;
}
}
stringVector getVariables(const stringVector& tokenVector) {
stringVector variableVector;
for(stringIter i = tokenVector.begin(); i != tokenVector.end(); ++i) {
if(i->length() == 1 && isalpha(i->at(0)) && i->at(0) != 'v' &&
i->at(0) != 'c' && i->at(0) != 't') {
if(!(std::find(variableVector.begin(), variableVector.end(), *i) !=
variableVector.end())) {
variableVector.push_back(*i);
}
}
}
return variableVector;
}
stringVector convertRPN(const stringVector& tokenVector) {
stringVector RPNVector;
std::stack<string> operatorStack;
int precedence;
for(stringIter i = tokenVector.begin(); i != tokenVector.end(); ++i) {
if(isVariable(*i)) {
RPNVector.push_back(*i);
}
else if(isOperator(*i)) {
precedence = getPrecedence(*i);
if(precedence == -1) {
std::cout << "Invalid operator plugged into "
<< "precedence function!\n";
RPNVector.clear();
return RPNVector;
}
while(!(operatorStack.empty()) &&
precedence <= getPrecedence(operatorStack.top())) {
RPNVector.push_back(operatorStack.top());
operatorStack.pop();
}
operatorStack.push(*i);
}
else if(isLeftParen(*i)) {
operatorStack.push(*i);
}
else if(isRightParen(*i)) {
if(operatorStack.empty()) {
std::cout << "Parentheses mismatch!\n";
RPNVector.clear();
return RPNVector;
}
while(operatorStack.top() != "(") {
if(operatorStack.empty()) {
std::cout << "Parentheses mismatch!\n";
RPNVector.clear();
return RPNVector;
}
RPNVector.push_back(operatorStack.top());
operatorStack.pop();
}
operatorStack.pop();
}
}
while(!(operatorStack.empty())) {
RPNVector.push_back(operatorStack.top());
operatorStack.pop();
}
return RPNVector;
}
bool evalRPN(const stringVector& RPNVector, const boolMap& bmap) {
std::stack<string> variableStack;
string arg1, arg2;
if(RPNVector.empty()) {
return false;
}
// If it's just one variable in the statement, return the variable.
if(RPNVector.size() == 1) {
if(evalExpression(RPNVector[0], "", "", bmap) == "t") {
return true;
}
else {
return false;
}
}
for(stringIter i = RPNVector.begin(); i != RPNVector.end(); ++i) {
if(isVariable(*i)) {
variableStack.push(*i);
}
// Otherwise, it's an operator.
else {
// Unary operator takes one argument.
if(*i == "!") {
if(variableStack.size() > 0) {
arg1 = variableStack.top();
variableStack.pop();
arg2 = "";
// Evaluate the arguments and push the result onto the
// stack.
variableStack.push(evalExpression(arg1, arg2, *i, bmap));
}
else {
std::cout << "Invalid expression! Not enough arguments.\n";
return false;
}
}
// Otherwise, it's a binary operator and takes two.
else {
if(variableStack.size() > 1) {
arg1 = variableStack.top();
variableStack.pop();
arg2 = variableStack.top();
variableStack.pop();
variableStack.push(evalExpression(arg2, arg1, *i, bmap));
}
else {
std::cout << "Invalid expression! Not enough arguments.\n";
return false;
}
}
}
}
// We should have a single expression at this point - either c or t.
if(variableStack.size() != 1) {
std::cout << "Invalid expression! Too many arguments.\n";
return false;
}
if(variableStack.top() == "t") {
return true;
}
else {
if(variableStack.top() != "c") {
std::cout << "Invalid expression! It didn't simplify to t or c.\n";
}
return false;
}
}
string evalExpression(const string& arg1, const string& arg2,
const string& op, const boolMap& bmap) {
bool bArg1, bArg2;
std::string result;
if(op == "") {
bArg1 = bmap.at(arg1);
if(bArg1) {
return "t";
}
else {
return "c";
}
}
else if(op == "!") {
bArg1 = bmap.at(arg1);
if(bArg1) {
result = "c";
}
else {
result = "t";
}
}
else {
bArg1 = bmap.at(arg1);
bArg2 = bmap.at(arg2);
// Logical AND.
if(op == "^") {
if(bArg1 && bArg2) {
result = "t";
}
else {
result = "c";
}
}
// Logical OR.
else if(op == "v") {
if(bArg1 || bArg2) {
result = "t";
}
else {
result = "c";
}
}
// Logical XOR.
else if(op == "XOR") {
if((bArg1 || bArg2) && !(bArg1 && bArg2)) {
result = "t";
}
else {
result = "c";
}
}
// Implies operator.
else if(op == "->") {
if(!bArg1 || bArg2) {
result = "t";
}
else {
result = "c";
}
}
// If and only if operator.
else if(op == "<->") {
if((bArg1 && bArg2) || (!bArg1 && !bArg2)) {
result = "t";
}
else {
result = "c";
}
}
else {
std::cout << "Invalid operator!\n";
result = "c";
}
}
// std::cout << result << "\n";
return result;
}
void setValues(boolMap& bmap, const stringVector& variableVector,
int comboGen) {
int j = 0;
for(stringIter i = variableVector.begin(); i != variableVector.end(); i++) {
if((comboGen >> j) & 0x01) {
bmap[*i] = true;
}
else {
bmap[*i] = false;
}
j++;
}
return;
}
stringVector combineVectors(const stringVector& vector1,
const stringVector& vector2) {
stringVector newVector = vector1;
for(stringIter i = vector2.begin(); i != vector2.end(); ++i) {
if(std::find(newVector.begin(),
newVector.end(), *i) == newVector.end()) {
newVector.push_back(*i);
}
}
std::sort(newVector.begin(), newVector.end());
return newVector;
}
void printBool(bool b) {
if(b) {
std::cout << "T";
}
else {
std::cout << "F";
}
return;
}
void printSpaces(int n) {
for(int i = 0; i < n; i++) {
std::cout << " ";
}
return;
}