Discrete CCP
Discrete CCP
Discrete Structures
COMPLEX COMPUTING PROBLEM
Submi ed by: Zunayra Junaid
Submi ed to: Sir A f
Roll No: fall23-bscs-632
Sec: A
ASSIGNMENT NO. 2
#include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <string>
#include <algorithm>
if (op == '!') {
operands.push(!val2);
} else {
bool val1 = operands.top();
operands.pop();
if (op == '&') {
operands.push(val1 && val2);
} else if (op == '|') {
operands.push(val1 || val2);
} else if (op == '>') {
operands.push(!val1 || val2);
} else if (op == '=') {
operands.push(val1 == val2);
}
}
}
operators.pop();
} else if (ch == '&' || ch == '|' || ch == '!' || ch == '>' || ch == '=') {
while (!operators.empty() && operators.top() != '(' &&
((ch != '!' && operators.top() != '!') ||
(ch == '!' && operators.top() == '!'))) {
char op = operators.top();
operators.pop();
if (op == '!') {
operands.push(!val2);
} else {
bool val1 = operands.top();
operands.pop();
if (op == '&') {
operands.push(val1 && val2);
} else if (op == '|') {
operands.push(val1 || val2);
} else if (op == '>') {
operands.push(!val1 || val2);
} else if (op == '=') {
operands.push(val1 == val2);
}
}
}
operators.push(ch);
} else {
operands.push(values.at(ch));
}
}
while (!operators.empty()) {
char op = operators.top();
operators.pop();
if (op == '!') {
operands.push(!val2);
} else {
bool val1 = operands.top();
operands.pop();
if (op == '&') {
operands.push(val1 && val2);
} else if (op == '|') {
operands.push(val1 || val2);
} else if (op == '>') {
operands.push(!val1 || val2);
} else if (op == '=') {
operands.push(val1 == val2);
}
}
}
return operands.top();
}
int main() {
string expression;
cout << "Enter a logical expression (use & for AND, | for OR, ! for NOT, > for
IMPLICATION, = for BICONDITIONAL, and parentheses for grouping): ";
getline(cin, expression);
vector<char> variables;
for (size_t i = 0; i < expression.length(); ++i) {
char ch = expression[i];
if (isalpha(ch) && find(variables.begin(), variables.end(), ch) ==
variables.end()) {
variables.push_back(ch);
}
}
sort(variables.begin(), variables.end());
generateTruthTable(expression, variables);
return 0;
}