-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMap-Generator.cs
86 lines (78 loc) · 2.43 KB
/
KMap-Generator.cs
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
using System;
using System.Collections.Generic;
public class KMap_Generator {
public static void Main(string[] args) {
// Define K-Map
bool[, ] kMap = new bool[4, 4];
kMap[0, 0] = true;
kMap[0, 1] = false;
kMap[0, 2] = true;
kMap[0, 3] = false;
kMap[1, 0] = false;
kMap[1, 1] = true;
kMap[1, 2] = false;
kMap[1, 3] = true;
kMap[2, 0] = true;
kMap[2, 1] = false;
kMap[2, 2] = true;
kMap[2, 3] = false;
kMap[3, 0] = false;
kMap[3, 1] = true;
kMap[3, 2] = false;
kMap[3, 3] = true;
// Print K-Map
Console.WriteLine("K-Map:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Console.Write(Convert.ToInt32(kMap[i, j]) + " ");
}
Console.WriteLine();
}
// Find minterms and maxterms
List < int > minterms = new List < int > ();
List < int > maxterms = new List < int > ();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (kMap[i, j]) {
int minterm = (i * 4) + j;
minterms.Add(minterm);
} else {
int maxterm = (i * 4) + j;
maxterms.Add(maxterm);
}
}
}
// Print minterms and maxterms
Console.WriteLine("MinTerms: " + string.Join(",", minterms));
Console.WriteLine("MaxTerms: " + string.Join(",", maxterms));
// Convert minterms and maxterms to expressions
string mintermExpression = ConvertToExpression(minterms, "m");
string maxtermExpression = ConvertToExpression(maxterms, "M");
// Print the expressions
Console.WriteLine("MinTerm Expression: " + mintermExpression);
Console.WriteLine("MaxTerm Expression: " + maxtermExpression);
Console.ReadLine();
}
public static string ConvertToExpression(List < int > terms, string prefix) {
if (terms.Count == 0) {
return "1";
} else if (terms.Count == 16) {
return "0";
} else {
List < string > expressionTerms = new List < string > ();
foreach(int term in terms) {
string termString = Convert.ToString(term, 2).PadLeft(4, '0');
string expressionTerm = "";
for (int i = 0; i < termString.Length; i++) {
if (termString[i] == '0') {
expressionTerm += prefix + (i + 1) + "'";
} else {
expressionTerm += prefix + (i + 1);
}
}
expressionTerms.Add(expressionTerm);
}
return string.Join(" + ", expressionTerms);
}
}
}