我目前正在使用布尔逻辑和真值表。我已经能够创建一个将生成 2 个变量(a,b)
表的类。我的主要兴趣是在表中选择某些输入并为其输出设置“真实”值。我已经能够对上述内容进行硬编码,override bool GetTruthValue()
并将这些true
值输出结果显示在名为OutputTextBox
. 我希望用户提供计算真值的逻辑。我目前的方法是由代码提供逻辑。如何让用户选择哪个输入的输出值为“真”?通过复选框或单独的 texboxes(带 1 或 0)指示?或者其他建议?
namespace table_outputs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public abstract class TwoItemTruthRow
{
protected TwoItemTruthRow(bool a, bool b)
{
A = a; B = b;
}
public bool A { get; protected set; }
public bool B { get; protected set; }
public abstract bool GetTruthValue();
}
public class MyCustomTwoItemTruthRow : TwoItemTruthRow
{
public MyCustomTwoItemTruthRow(bool a, bool b)
: base(a, b)
{
}
public override bool GetTruthValue()
{
// My custom logic- Hard coded
return (A && B) || (A && !B) || (!A && !B);
}
}
private static string GetHorizontalLineText()
{
return "-----------------------------------------------\r\n";
}
private static string GetFormattedTwoItemText(MyCustomTwoItemTruthRow item)
{
return string.Format("{0}\t{1}\r\n", item.A, item.B);
}
private static IEnumerable<MyCustomTwoItemTruthRow> GenerateTruthTableTwo()
{
for (var a = 0; a < 2; a++)
for (var b = 0; b < 2; b++)
yield return new MyCustomTwoItemTruthRow(
Convert.ToBoolean(a),
Convert.ToBoolean(b));
}
private void GenerateTableButton_Click(object sender, EventArgs e)
{
InputTextBox.Clear();
InputTextBox.Text += "A\tB\r\n";
InputTextBox.Text += GetHorizontalLineText();
var myTruthTable = GenerateTruthTable().ToList();
foreach (var item in myTruthTable)
{
InputTextBox.Text += GetFormattedTwoItemText(item);
InputTextBox.Text += GetHorizontalLineText();
}
}
private void ShowTrueValuesButton_Click(object sender, EventArgs e)
{
OutputTextBox.Clear();
OutputTextBox.Text += "True Values\r\n";
OutputTextBox.Text += "A\tB\r\n";
OutputTextBox.Text += GetHorizontalLineText();
var myTruthTable = GenerateTruthTableTwo().ToList();
foreach (var item in myTruthTable)
{
if (item.GetTruthValue())
OutputTextBox.Text += GetFormattedTwoItemText(item);
}
}
}
}
当前的 WinForm