Last active
November 14, 2015 21:18
-
-
Save mathewmariani/10a5dbc418ad2f0fa82a to your computer and use it in GitHub Desktop.
The Karnaugh map, also known as the K-map, is a method to simplify boolean algebra expressions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class MainClass | |
{ | |
// The Karnaugh map, also known as the K-map, is a method to simplify boolean algebra expressions. | |
public static void Main(string[] args) | |
{ | |
// kmap cells | |
var kmap = new bool[4]; | |
// kmap variables | |
string[] variables = | |
{ | |
"x", | |
"y" | |
}; | |
Console.Write("Input must contain either TrueString or FalseString; otherwise, false\n"); | |
Console.Write("The comparison is case-insensitive\n"); | |
Console.Write("TrueString: \"True\"\n"); | |
Console.Write("FalseString: \"False\"\n"); | |
Console.Write("\n"); | |
// Y !Y | |
// --------- | |
// X | 0 | 1 | | |
// --------- | |
// !X | 2 | 3 | | |
// --------- | |
// user input | |
for (var i = 0; i < kmap.Length; i++) | |
{ | |
Console.Write("Input for cell {0}: ", i); | |
// must contain either TrueString or FalseString; otherwise, an exception is thrown. | |
// The comparison is case-insensitive. | |
// all other strings will be represented as false | |
Boolean.TryParse(Console.ReadLine(), out kmap[i]); | |
} | |
Console.Write("\n"); | |
// Print the cell listing | |
for (var i = 0; i < kmap.Length; i++) | |
{ | |
Console.WriteLine("Cell {0}: {1}", i, kmap[i]); | |
} | |
Console.Write("\n"); | |
// If everything is false the function must be false / empty / 0 | |
if (!kmap[0] && !kmap[1] && !kmap[2] && !kmap[3]) | |
{ | |
Console.WriteLine("This is an empty function"); | |
return; | |
} | |
// If everything is true the function must be true / 1 | |
if (kmap[0] && kmap[1] && kmap[2] && kmap[3]) | |
{ | |
Console.WriteLine("the value of this function is 1"); | |
return; | |
} | |
// just some polish | |
Console.Write("F(x, y) = "); | |
// Y column | |
if (kmap[0]) | |
{ | |
Console.Write("{0}{1}", variables[0], variables[1]); | |
// check our neighbors | |
if (kmap[1] || kmap[2]) | |
{ | |
Console.Write("+"); | |
} | |
} | |
// !Y Column | |
if (kmap[1]) | |
{ | |
Console.Write("{0}!{1}", variables[0], variables[1]); | |
if (kmap[0] || kmap[3]) | |
{ | |
Console.Write("+"); | |
} | |
} | |
// X row | |
if (kmap[2]) | |
{ | |
Console.Write("!{0}{1}", variables[0], variables[1]); | |
if (kmap[3]) | |
{ | |
Console.Write("+"); | |
} | |
} | |
// !X row | |
if (kmap[3]) | |
{ | |
Console.Write("!{0}!{1}", variables[0], variables[1]); | |
if (kmap[0] || kmap[3]) | |
{ | |
// we dont care about our neihbors | |
// this is the last cell and nothing will | |
// come after | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment