0% found this document useful (0 votes)
4 views6 pages

Discrete CCP

This document presents an assignment on evaluating logical expressions using C++. It includes a C++ program that evaluates expressions and generates truth tables based on user input. The program utilizes stacks for operands and operators to compute the results of logical operations.

Uploaded by

zunayrajunaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Discrete CCP

This document presents an assignment on evaluating logical expressions using C++. It includes a C++ program that evaluates expressions and generates truth tables based on user input. The program utilizes stacks for operands and operators to compute the results of logical operations.

Uploaded by

zunayrajunaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

27/01/25

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>

using namespace std;

bool evaluateExpression(const string& expr, const map<char, bool>& values) {


stack<bool> operands;
stack<char> operators;

for (size_t i = 0; i < expr.length(); ++i) {


char ch = expr[i];

if (ch == ' ') {


con nue;
} else if (ch == '(') {
operators.push(ch);
} else if (ch == ')') {
while (!operators.empty() && operators.top() != '(') {
char op = operators.top();
operators.pop();

bool val2 = operands.top();


operands.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.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();

bool val2 = operands.top();


operands.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();

bool val2 = operands.top();


operands.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();
}

void generateTruthTable(const string& expr, const vector<char>& variables) {


int numVars = variables.size();
int numCombina ons = 1 << numVars;

for (size_t i = 0; i < variables.size(); ++i) {


cout << variables[i] << " ";
}
cout << "| " << expr << endl;

for (int i = 0; i < numVars; ++i) {


cout << "--";
}
cout << "|--" << string(expr.length(), '-') << endl;

for (int i = 0; i < numCombina ons; ++i) {


map<char, bool> values;
for (int j = 0; j < numVars; ++j) {
values[variables[j]] = (i >> (numVars - j - 1)) & 1;
cout << values[variables[j]] << " ";
}
cout << "| " << evaluateExpression(expr, values) << endl;
}
}

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;
}

You might also like