-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
276 lines (262 loc) · 11.4 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta lang="zh-CN">
<title>Karnaugh Map Simplifier</title>
<link rel="stylesheet" href="katex.min.css">
<style>
body { user-select: none; }
.cols { width: 100%; }
.cols div {
display: inline-block;
vertical-align: top;
}
button {
height: 30px;
}
.karnaugh { border: none; }
.karnaugh td {
padding: 0;
border: none;
text-align: center;
}
.karnaugh button {
width: 50px;
height: 50px;
border: none;
font-size: 1em;
}
.truth {
margin: auto;
border-collapse: collapse;
}
.truth td {
width: 40px;
height: 30px;
border: 1px solid gray;
text-align: center;
}
.truth button {
width: 40px;
height: 30px;
border: none;
font-size: 1em;
}
.cell-0 { background-color: lightsteelblue; }
.cell-1 { background-color: aquamarine; }
.cell-2 { background-color: khaki; }
.hover { background-color: darksalmon; }
.hover button { background-color: darksalmon; }
.term:hover { background-color: lightsalmon; }
</style>
</head>
<body>
<h2>K-MAP <small>by Gopinath Padhy</small></h2>
<ul>
<li>Click the truth value on the right side of the truth table below or the cell on the K-Map to change the minterm state;</li>
<ul>
<li><span class="cell-0"> </span> $0$ / Zeros</li>
<li><span class="cell-1"> </span> $1$ / Ones</li>
<li><span class="cell-2"> </span> $\varphi$ / Don't Care</li>
</ul>
<li>Move the mouse over a term/factor of the expression to<span class="hover"> </span> highlight the row in the truth table corresponding to the corresponding minimum term, and the cell on the Karnaugh map.</li>
<ul>
<li> The "corresponding minimum term" refers to the minimum term included in the item in the SOP, or the minimum term not included in the factor in the POS.</li>
</ul>
<li>The Simplified result obtained by this tool may not be the simplest, especially when there are too many irrelevant items. At this time, reducing the irrelevant items or modifying the setting options may simplify the result to a certain extent.</li>
</ul>
<div class="cols">
<div id="row-1"></div>
<div id="row-2">
<p>
<button id="all-1">Set to all $1$</button>
<button id="all-0">Set to all $0$</button>
<button id="all-2">Set to all $\varphi$</button>
</p>
<ul>
<li>Simplified Way:</li>
<ul>
<li>
<input id="SOP" type="radio" name="exprtype" value="SOP" checked />
<label for="SOP">Sum of Product, SOP</label>
</li>
<li>
<input id="POS" type="radio" name="exprtype" value="POS" />
<label for="POS">Product of Sum, POS</label>
</li>
</ul>
<li>Default Values for Irrelevant Items:</li>
<ul>
<li>
<input id="Care" type="radio" name="caretype" value="true" checked />
<label for="Care">1 (SOP) / 0 (POS)</label>
</li>
<li>
<input id="DontCare" type="radio" name="caretype" value="false" />
<label for="DontCare">0 (SOP) / 1 (POS)</sup></label>
</li>
</ul>
<li>Simplification Algorithm:</li>
<ul>
<li>
<input id="QM" type="radio" name="algotype" value="QM" />
<label for="QM">Quine-McCluskey Algorithm</label>
</li>
<li>
<input id="QMP" type="radio" name="algotype" value="QMP" checked />
<label for="QMP">Quine-McCluskey Algorithm + Prime Table Simplification</label>
</li>
</ul>
</ul>
<p>Simplified Expression:<span id='result'></span></p>
</div>
</div>
</body>
<script src="karnaugh.js"></script>
<script src="katex.min.js"></script>
<script src="auto-render.min.js"></script>
<script>
// introduce
document.addEventListener("DOMContentLoaded", function() {
renderMathInElement(document.body, {
delimiters: [
{left: '$', right: '$', display: false}
],
throwOnError : false
});
});
// default setting
let kar = new karnaugh();
let cells = [0, 1, 3, 2, 4, 5, 7, 6, 12, 13, 15, 14, 8, 9, 11, 10];
let cols = ["\\bar C\\bar D", "\\bar CD", "CD", "C\\bar D"];
let rows = ["\\bar A\\bar B","\\bar AB", "AB", "A\\bar B"];
let truthValue = ["0", "1", "\\varphi"];
let status = new Array();
for (i in cells)
status[i] = 0;
// setting item
let POS = false;
let Care = true;
let Prim = true;
// generate page content
// k-map Table
let ktable = "<table class='karnaugh'><tr><td></td>";
for (let i of cols)
ktable += `<td>$${i}$</td>`;
for (let i = 0; i < 4; i ++) {
ktable += `</tr><tr><td>$${rows[i]}$</td>`;
for (let j = 0; j < 4; j ++)
ktable += `<td><button class="cell-0" id="${cells[i * 4 + j]}">$m_{${cells[i * 4 + j]}}$</button></td>`;
}
ktable += "</tr></table>";
// truth table
let ttable = "<table class='truth'><tr><th></th>";
for (let i = 0; i < 4; i ++)
ttable += `<th>$${kar.chars[i]}$</th>`;
ttable += "<th>Truth Table</th>";
for (let i = 0; i < 16; i ++) {
ttable += `</tr><tr class="truth-row"><td>$m_{${i}}$</td>`;
let bits = kar.binaryify(i);
for (let j = 0; j < 4; j ++)
ttable += `<td>$${bits[j]}$</td>`;
ttable += `<td><button class="cell-0" id="r-${i}">$0$</button></td>`;
}
ttable += "</tr></table>";
// action button
document.getElementById("row-1").innerHTML = ttable;
document.getElementById("row-2").innerHTML = ktable + document.getElementById("row-2").innerHTML;
for (let i = 0; i < 3; i ++) {
document.getElementById(`all-${i}`).addEventListener("click", () => {
for (let j = 0; j < 16; j ++)
change(j, i);
update();
});
}
for (let i = 0; i < 16; i ++) {
let event = () => {
change(i, (status[i] + 1) % 3);
update();
};
document.getElementById(`${i}`).addEventListener("click", event);
document.getElementById(`r-${i}`).addEventListener("click", event);
}
function change(index, state) {
status[index] = state;
document.getElementById(`${index}`).classList = `cell-${state}`;
document.getElementById(`r-${index}`).classList = `cell-${state}`;
katex.render(
`${truthValue[state]}`,
document.getElementById(`r-${index}`), {
throwOnError: false
});
}
document.getElementById("SOP").addEventListener("click", function () {
if (this.checked) { POS = false; update(); }
});
document.getElementById("POS").addEventListener("click", function () {
if (this.checked) { POS = true; update(); }
});
document.getElementById("Care").addEventListener("click", function () {
if (this.checked) { Care = true; update(); }
});
document.getElementById("DontCare").addEventListener("click", function () {
if (this.checked) { Care = false; update(); }
});
document.getElementById("QM").addEventListener("click", function () {
if (this.checked) { Prim = false; update(); }
});
document.getElementById("QMP").addEventListener("click", function () {
if (this.checked) { Prim = true; update(); }
});
function update() {
let ones = new Set();
let dontcares = new Set();
for (let i = 0; i < status.length; i ++) {
if (POS && status[i] == 0) ones.add(i);
if (!POS && status[i] == 1) ones.add(i);
if (Care && status[i] == 2) dontcares.add(i);
}
// Render Expression
let terms;
if (Prim) terms = kar.simplify_prime(ones, dontcares);
else terms = kar.simplify(ones, dontcares);
document.getElementById("result").innerHTML = "";
if (terms.length == 0) {
document.getElementById("result").innerHTML = '<span class="term" id="t-" data=""></span>'
katex.render(POS ? "1" : "0", document.getElementById("t-"), {
throwOnError: false
});
} else terms.forEach((item, index) => {
document.getElementById("result").innerHTML +=
`<span class="term" id="t-${item}" data="${item}"></span>`;
let termDom = document.getElementById(`t-${item}`);
katex.render(
POS ? ((index > 0 ? "\\cdot" : "") + kar.termify_POS(item)) :
((index > 0 ? "+" : "") + kar.termify(item)),
termDom, {
throwOnError: false
}
);
});
// Set "highlight" event
let doms = document.getElementsByClassName("term");
for (let i = 0; i < doms.length; i ++) {
doms[i].addEventListener("mouseover", function () {
let terms = kar.minimumTermify(this.getAttribute("data"));
terms.forEach(term => {
document.getElementById(term).classList.add("hover");
document.getElementsByClassName("truth-row")[term].classList.add("hover");
});
});
doms[i].addEventListener("mouseout", function () {
let terms = kar.minimumTermify(this.getAttribute("data"));
terms.forEach(term => {
document.getElementById(term).classList.remove("hover");
document.getElementsByClassName("truth-row")[term].classList.remove("hover");
});
});
}
}
</script>
</html>