【C#】bool(boolean) 邏輯/真值表和輸出
2021-01-17 C#
我目前正在嘗試複製一種將真值表轉換為C#中的布林表示式的方法。我已經能夠生成3變數(a,b,c)真值表並將其顯示在多行文字框中。我建立了另外八個文字框,供使用者確定每個輸入的輸出:true(1)或false(0)。但是,在生成表格之後,如何顯示所有具有true值的輸出?
public partial class Form1 : Form
{
public Form1() => InitializeComponent();
string newLine = Environment.NewLine;
bool a, b, c, d;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.AppendText(newLine + "A" + "\t" + "B" + "\t" + "C" + newLine);
textBox1.AppendText("______________________________" + newLine);
a = true; b = true; c = true;
textBox1.AppendText(newLine + a + "\t" + b + "\t" + c +newLine);
textBox1.AppendText("______________________________" + newLine);
a = true; b = true; c = false;
textBox1.AppendText(newLine + a + "\t" + b + "\t" + c + newLine);
textBox1.AppendText("______________________________" + newLine);
a = true; b = false; c = true;
textBox1.AppendText(newLine + a + "\t" + b + "\t" + c + newLine);
textBox1.AppendText("______________________________" + newLine);
a = true; b = false; c = false;
textBox1.AppendText(newLine + a + "\t" + b + "\t" + c + newLine);
textBox1.AppendText("______________________________" + newLine);
a = false; b = true; c = true;
textBox1.AppendText(newLine + a + "\t" + b + "\t" + c + newLine);
textBox1.AppendText("______________________________" + newLine);
a = false; b = true; c = false;
textBox1.AppendText(newLine + a + "\t" + b + "\t" + c + newLine);
textBox1.AppendText("______________________________" + newLine);
a = false; b = false; c = true;
textBox1.AppendText(newLine + a + "\t" + b + "\t" + c + newLine);
textBox1.AppendText("______________________________" + newLine);
a = false; b = false; c = false;
textBox1.AppendText(newLine + a + "\t" + b + "\t" + c + newLine);
textBox1.AppendText("______________________________" + newLine);
}
private void button1_Click(object sender, EventArgs e)
{
//Grab true value outputs and display in string
}
}
上表是一個示例。我想以某種方式顯示真實的輸出值:
結果如下:
否是是
是否是
真真假
是是是
解決辦法
嘗試封裝您的TruthItem(以及用於計算TruthValue的邏輯)。然後,使用真值表(生成,迭代,計算等)將很容易。
這是示例控制檯應用程式。它沒有您的文字框,但是您會明白的。
public abstract class ThreeItemTruthRow
{
protected ThreeItemTruthRow(bool a, bool b, bool c)
{
A = a; B = b; C = c;
}
public bool A { get; protected set; }
public bool B { get; protected set; }
public bool C { get; protected set; }
public abstract bool GetTruthValue();
}
public class MyCustomThreeItemTruthRow : ThreeItemTruthRow
{
public MyCustomThreeItemTruthRow(bool a, bool b, bool c)
: base(a, b, c)
{
}
public override bool GetTruthValue()
{
// My custom logic
return (!A && B && C) || (A && !B && C) || (A && B && !C) || (A && B && C);
}
}
class Program
{
static void Main(string[] args)
{
var myTruthTable = GenerateTruthTable().ToList();
//Print only true values
foreach (var item in myTruthTable)
{
if (item.GetTruthValue())
Console.WriteLine("{0}, {1}, {2}", item.A, item.B, item.C);
}
////Print all values
//foreach (var itemTruthRow in myTruthTable)
//{
// Console.WriteLine("{0}, {1}, {2}", itemTruthRow.A, itemTruthRow.B, itemTruthRow.C);
//}
////Print only false values
//foreach (var item in myTruthTable)
//{
// if (!item.GetTruthValue())
// Console.WriteLine("{0}, {1}, {2}", item.A, item.B, item.C);
//}
Console.ReadLine();
}
public static IEnumerable<MyCustomThreeItemTruthRow> GenerateTruthTable()
{
for (var a = 0; a < 2; a++)
for (var b = 0; b < 2; b++)
for (var c = 0; c < 2; c++)
yield return new MyCustomThreeItemTruthRow(
Convert.ToBoolean(a),
Convert.ToBoolean(b),
Convert.ToBoolean(c));
}
}
編輯(包括WinForm的示例程式碼):
使用並引用上面的類(ThreeItemTruthRow和MyCustomThreeItemTruthRow)。
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void GenerateButton_Click(object sender, EventArgs e)
{
OutputTextBox.Clear();
OutputTextBox.Text += "A\tB\tC\r\n";
OutputTextBox.Text += GetHorizontalLineText();
var myTruthTable = GenerateTruthTable().ToList();
foreach(var item in myTruthTable)
{
OutputTextBox.Text += GetFormattedItemText(item);
OutputTextBox.Text += GetHorizontalLineText();
}
}
private void ShowTrueValuesButton_Click(object sender, EventArgs e)
{
OutputTextBox.Clear();
OutputTextBox.Text += "True Values\r\n";
OutputTextBox.Text += "A\tB\tC\r\n";
OutputTextBox.Text += GetHorizontalLineText();
var myTruthTable = GenerateTruthTable().ToList();
foreach(var item in myTruthTable)
{
if(item.GetTruthValue())
OutputTextBox.Text += GetFormattedItemText(item);
}
}
private static string GetHorizontalLineText()
{
return "-----------------------------------------------\r\n";
}
private static string GetFormattedItemText(MyCustomThreeItemTruthRow item)
{
return string.Format("{0}\t{1}\t{2}\r\n", item.A, item.B, item.C);
}
private static IEnumerable<MyCustomThreeItemTruthRow> GenerateTruthTable()
{
for (var a = 0; a < 2; a++)
for (var b = 0; b < 2; b++)
for (var c = 0; c < 2; c++)
yield return new MyCustomThreeItemTruthRow(
Convert.ToBoolean(a),
Convert.ToBoolean(b),
Convert.ToBoolean(c));
}
}