Is there any simple way to resemble a truth table in code? It has 2 inputs and 4 outcomes, as shown below:
My current code is:
private void myMethod(bool param1, bool param2)
{
Func<int, int, bool> myFunc;
if (param1)
{
if (param2)
myFunc = (x, y) => x >= y;
else
myFunc = (x, y) => x <= y;
}
else
{
if (param2)
myFunc = (x, y) => x < y;
else
myFunc = (x, y) => x > y;
}
//do more stuff
}
A truth table is a mathematical table used to determine if a compound statement is true or false. In a truth table, each statement is typically represented by a letter or variable, like p, q, or r, and each statement also has its own corresponding column in the truth table that lists all of the possible truth values.
We can use truth tables to determine if the structure of a logical argument is valid.To tell if the structure of a logical argument is valid, we first need to translate our argument into a series of logical statements written using letters and logical connectives.
I suggest using an array, i.e.
// XOR truth table
bool[][] truthTable = new bool[][] {
new bool[] {false, true},
new bool[] {true, false},
};
...
private void myMethod(bool param1, bool param2, bool[][] table) {
return table[param1 ? 0 : 1][param2 ? 0 : 1];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With