-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
329 lines (290 loc) · 12.2 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
/******************************************************
* Course: CSC510 DISCRETE STRUCTURE *
* *
* Selected theory: *
* Propositional Logic *
* *
* Title: *
* Propositional Logic Assistant *
******************************************************/
#include <iostream>
using namespace std;
// A class that encapsulates logic operations
class LogicOperations
{
private:
bool p, q; // Private variables for logic values
public:
// Constructor that initializes p and q with the provided values
LogicOperations(bool p, bool q)
{
this->p = p;
this->q = q;
}
// Negation (¬p) reverses the Boolean value of p.
bool Negation()
{
return !p; // Reverses the value of p
}
// Conjunction (p ∧ q) is true if both p and q are true.
bool Conjunction()
{
return p && q; // Returns true if p and q are both true
}
// Disjunction (p ∨ q) is true if either p or q, or both are true.
bool Disjunction()
{
return p || q; // Returns true if at least one of p or q is true
}
// Implication (p → q) is true except when p is true and q is false.
bool Implication()
{
return !p || q; // Returns true except in the case where p is true and q is false
}
// Biconditional (p ↔ q) is true if p and q have the same value.
bool Biconditional()
{
return (p && q) || (!p && !q); // Returns true if p and q have the same value
}
// Function to print the truth table for the chosen operation
void printTruthTable(char op)
{
switch (op)
{
case '!':
// Truth table for ¬p
cout << "Truth Table for NEGATION operation (NOT P)" << endl;
cout << "+---+-------+" << endl;
cout << "| p | NOT p |" << endl;
cout << "+---+-------+" << endl;
cout << "| 0 | 1 |" << endl;
cout << "| 1 | 0 |" << endl;
cout << "+---+-------+" << endl;
break;
case '&':
// Truth table for p ∧ q
cout << "Truth Table for CONJUNCTION operation (p AND q)" << endl;
cout << "+---+---+---------+" << endl;
cout << "| p | q | p AND q |" << endl;
cout << "+---+---+---------+" << endl;
cout << "| 0 | 0 | 0 |" << endl;
cout << "| 0 | 1 | 0 |" << endl;
cout << "| 1 | 0 | 0 |" << endl;
cout << "| 1 | 1 | 1 |" << endl;
cout << "+---+---+---------+" << endl;
break;
case '|':
// Truth table for p ∨ q
cout << "Truth Table for DISJUNCTION operation (p OR q)" << endl;
cout << "+---+---+--------+" << endl;
cout << "| p | q | p OR q |" << endl;
cout << "+---+---+--------+" << endl;
cout << "| 0 | 0 | 0 |" << endl;
cout << "| 0 | 1 | 1 |" << endl;
cout << "| 1 | 0 | 1 |" << endl;
cout << "| 1 | 1 | 1 |" << endl;
cout << "+---+---+--------+" << endl;
break;
case '>':
// Truth table for p → q
cout << "Truth Table for IMPLICATION operation (p IMPLIES q)" << endl;
cout << "+---+---+-------------+" << endl;
cout << "| p | q | p IMPLIES q |" << endl;
cout << "+---+---+-------------+" << endl;
cout << "| 0 | 0 | 1 |" << endl;
cout << "| 0 | 1 | 1 |" << endl;
cout << "| 1 | 0 | 0 |" << endl;
cout << "| 1 | 1 | 1 |" << endl;
cout << "+---+---+-------------+" << endl;
break;
case '=':
// Truth table for p ↔ q
cout << "Truth Table for BICONDITIONAL operation (p IFF q)" << endl;
cout << "+---+---+---------+" << endl;
cout << "| p | q | p IFF q |" << endl;
cout << "+---+---+---------+" << endl;
cout << "| 0 | 0 | 1 |" << endl;
cout << "| 0 | 1 | 0 |" << endl;
cout << "| 1 | 0 | 0 |" << endl;
cout << "| 1 | 1 | 1 |" << endl;
cout << "+---+---+---------+" << endl;
break;
default:
cout << "Error. Invalid operator!\n";
break;
}
}
};
// Function that asks the user questions to test their understanding of logic operations
void askUserQuestions()
{
int answer; // To store the correct answer for comparison
int userAnswer; // To store the user's answer
cout << "\n***** INTERACTIVE MODE *****\n";
cout << "Let's test your propositional logic skills with some problems.\n";
// First question asks about the operation NOT (P AND Q)
cout << "\nQuestion 1: What is the result of the operation NOT (P AND Q) if P is true and Q is false? (Enter 1 for true, 0 for false)\n";
LogicOperations logic1(true, false); // Initializing LogicOperations object with P=true and Q=false
answer = !logic1.Conjunction(); // Performing NOT (P AND Q)
cin >> userAnswer;
if (userAnswer == answer)
{
cout << "Correct! Great job.\n";
}
else
{
cout << "Incorrect. The correct answer is " << answer << ".\n";
}
// Second question asks about the operation (P OR Q)
cout << "\nQuestion 2: What is the result of the operation (P OR Q) if P is false and Q is false? (Enter 1 for true, 0 for false)\n";
LogicOperations logic2(false, false); // Initializing LogicOperations object with P=false and Q=false
answer = logic2.Disjunction(); // Performing (P OR Q)
cin >> userAnswer;
if (userAnswer == answer) // If user's answer matches the correct answer
{
cout << "Correct! Great job.\n";
}
else
{
cout << "Incorrect. The correct answer is " << answer << ".\n";
}
// Third question asks about the operation (P → Q)
cout << "\nQuestion 3: What is the result of the operation (P → Q) if P is true and Q is false? (Enter 1 for true, 0 for false)\n";
LogicOperations logic3(true, false); // Initializing LogicOperations object with P=true and Q=false
answer = logic3.Implication(); // Performing (P → Q)
cin >> userAnswer;
if (userAnswer == answer) // If user's answer matches the correct answer
{
cout << "Correct! Great job.\n";
}
else
{
cout << "Incorrect. The correct answer is " << answer << ".\n";
}
// Fourth question asks about the operation (P ↔ Q)
cout << "\nQuestion 4: What is the result of the operation (P ↔ Q) if P is true and Q is false? (Enter 1 for true, 0 for false)\n";
LogicOperations logic4(true, false); // Initializing LogicOperations object with P=true and Q=false
answer = logic4.Biconditional(); // Performing (P ↔ Q)
cin >> userAnswer;
if (userAnswer == answer) // If user's answer matches the correct answer
{
cout << "Correct! Great job.\n";
}
else
{
cout << "Incorrect. The correct answer is " << answer << ".\n";
}
// Fifth question asks about the operation (P → Q)
cout << "\nQuestion 5: What is the result of the operation (P → Q) if P is false and Q is false? (Enter 1 for true, 0 for false)\n";
LogicOperations logic5(false, false); // Initializing LogicOperations object with P=false and Q=false
answer = logic5.Implication(); // Performing (P → Q)
cin >> userAnswer;
if (userAnswer == answer) // If user's answer matches the correct answer
{
cout << "Correct! Great job.\n";
}
else
{
cout << "Incorrect. The correct answer is " << answer << ".\n";
}
}
// Function that serves as a logic calculator, asking the user for input and displaying the results
void calculatorMode()
{
char op; // To store the operator entered by the user
bool p, q; // To store the truth values entered by the user
char exitLoop; // To control whether to continue the loop or not
do
{
// Asking the user for the operator
cout << "\nPlease select the logical operator you'd like to use:\n"
<< "'!' for NEGATION (NOT)\n"
<< "'&' for CONJUNCTION (AND)\n"
<< "'|' for DISJUNCTION (OR)\n"
<< "'>' for IMPLICATION (IMPLIES)\n"
<< "'=' for BICONDITIONAL (IF AND ONLY IF)\n"
<< "Enter Operator: ";
cin >> op;
// Error checking for operator
if (op != '!' && op != '&' && op != '|' && op != '>' && op != '=')
{
cout << "\nError: The operator you entered is not recognized! Please try again with a valid operator.\n";
continue;
}
// If operator is NOT, only one truth value is needed.
// If operator is not NOT, both truth values are needed.
// Truth values are then asked from the user.
if (op == '!')
{
cout << "Enter the truth value of P to apply NOT operator (0 for false, 1 for true)\nValue of P: ";
cin >> p;
q = false; // Not used in this case
}
else
{
cout << "Enter the truth value of P (0 for false, 1 for true)\nValue of P: ";
cin >> p;
cout << "Enter the truth value of Q (0 for false, 1 for true)\nValue of Q: ";
cin >> q;
}
// Result of the operation is calculated and displayed
cout << "\n***** EVALUATING YOUR PROPOSITION *****\n";
LogicOperations logic(p, q);
// Results are displayed based on the operator
if (op == '!')
cout << "Negation (NOT P) is: " << logic.Negation() << endl;
else if (op == '&')
cout << "Conjunction (P AND Q) is: " << logic.Conjunction() << endl;
else if (op == '|')
cout << "Disjunction (P OR Q) is: " << logic.Disjunction() << endl;
else if (op == '>')
cout << "Implication (P IMPLIES Q) is: " << logic.Implication() << endl;
else if (op == '=')
cout << "Biconditional (P IF AND ONLY IF Q) is: " << logic.Biconditional() << endl;
// Truth table for the operation is printed
logic.printTruthTable(op);
// Asking the user if they want to continue or not
cout << "\nWould you like to evaluate another proposition? Please enter 'Y' for Yes and 'N' for No: ";
cin >> exitLoop;
} while (toupper(exitLoop) == 'Y'); // The loop will continue as long as the user enters 'y' or 'Y'
}
int main()
{
char mode;
cout << "***** Welcome to the Propositional Logic Assistant! *****\n";
do
{
// Allow the user to choose between Calculator mode and Quiz mode
cout << "\nPlease select the mode you'd like to use:\n"
<< "'C' for CALCULATOR\n"
<< "'Q' for QUIZ\n"
<< "'E' for EXIT\n"
<< "Enter mode: ";
cin >> mode;
mode = toupper(mode); // Convert to upper case for consistency
switch (mode)
{
case 'C':
// Calculator mode
calculatorMode();
break;
case 'Q':
// Quiz mode
askUserQuestions();
break;
case 'E':
// Exit the program
cout << "\n***** Thank you for using the Propositional Logic Assistant! Have a great day! *****\n";
return 0;
default:
// If the user entered an unrecognized mode, display an error message
cout << "\nError: The mode you entered is not recognized! Please try again with a valid mode.\n";
break;
}
// Ask the user if they want to switch mode or exit the program
cout << "\nWould you like to switch mode or exit the program? Please enter 'Y' for Yes and 'N' for No: ";
cin >> mode;
} while (toupper(mode) != 'N'); // If the user enters 'N' or 'n', exit the program
cout << "\n***** Thank you for using the Propositional Logic Assistant! Have a great day! *****\n";
return 0;
}