-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruth-table.js
More file actions
120 lines (105 loc) · 3.66 KB
/
truth-table.js
File metadata and controls
120 lines (105 loc) · 3.66 KB
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
function generateTruthTable() {
const expression = document.getElementById('expressionInput').value;
const errorDiv = document.getElementById('error');
const tableContainer = document.getElementById('tableContainer');
errorDiv.textContent = '';
tableContainer.innerHTML = '';
const variables = [...new Set(expression.match(/\b[A-Z]\b/g) || [])];
if (variables.length === 0) {
errorDiv.textContent = 'Please enter a valid boolean expression using uppercase letters as variables.';
return;
}
try {
const rows = Math.pow(2, variables.length);
const table = [];
for (let i = 0; i < rows; i++) {
const row = {};
variables.forEach((variable, index) => {
row[variable] = Number(Boolean(i & (1 << (variables.length - 1 - index))));
});
row.result = evaluateExpression(expression, row);
table.push(row);
}
renderTable(variables, table);
} catch (err) {
errorDiv.textContent = err.message || 'An unknown error occurred';
}
}
function evaluateExpression(expr, values) {
const tokenize = (str) => {
const regex = /\s*(NAND|NOR|XOR|AND|OR|NOT|[A-Z])\s*/g;
return (str.match(regex) || []).map(token => token.trim()).filter(token => token !== '');
};
const applyOperator = (op, a, b) => {
switch (op) {
case 'AND': return Number(Boolean(a && b));
case 'OR': return Number(Boolean(a || b));
case 'NOT': return Number(!a);
case 'NAND': return Number(!(a && b));
case 'NOR': return Number(!(a || b));
case 'XOR': return Number(Boolean(a ^ b));
default: throw new Error(`Unknown operator: ${op}`);
}
};
const evaluate = (tokens) => {
const output = [];
const operators = [];
const precedence = { 'NOT': 4, 'NAND': 3, 'NOR': 3, 'XOR': 3, 'AND': 2, 'OR': 1 };
const applyTop = () => {
const op = operators.pop();
if (op === 'NOT') {
const a = output.pop();
output.push(applyOperator(op, a));
} else {
const b = output.pop();
const a = output.pop();
output.push(applyOperator(op, a, b));
}
};
for (const token of tokens) {
if (token in values) {
output.push(values[token]);
} else if (token === '(') {
operators.push(token);
} else if (token === ')') {
while (operators.length && operators[operators.length - 1] !== '(') {
applyTop();
}
if (!operators.length) throw new Error('Mismatched parentheses');
operators.pop();
} else if (token in precedence) {
while (operators.length && operators[operators.length - 1] !== '(' && precedence[operators[operators.length - 1]] >= precedence[token]) {
applyTop();
}
operators.push(token);
} else {
throw new Error(`Invalid token: ${token}`);
}
}
while (operators.length) {
if (operators[operators.length - 1] === '(') throw new Error('Mismatched parentheses');
applyTop();
}
if (output.length !== 1) throw new Error('Invalid expression');
return output[0];
};
const tokens = tokenize(expr);
return evaluate(tokens);
}
function renderTable(variables, truthTable) {
const tableContainer = document.getElementById('tableContainer');
let tableHTML = '<table><thead><tr>';
variables.forEach(variable => {
tableHTML += `<th>${variable}</th>`;
});
tableHTML += '<th>Result</th></tr></thead><tbody>';
truthTable.forEach(row => {
tableHTML += '<tr>';
variables.forEach(variable => {
tableHTML += `<td>${row[variable]}</td>`;
});
tableHTML += `<td>${row.result}</td></tr>`;
});
tableHTML += '</tbody></table>';
tableContainer.innerHTML = tableHTML;
}