我目前正在使用布尔逻辑和真值表。我已经能够创建一个将生成2个变量(a,b)
表的类。我的主要兴趣是在表中选择某些输入并为其输出设置“真”值。我已经能够通过override bool GetTruthValue()
对上述内容进行硬编码,并在值为OutputTextBox
的多行文本框中显示这些值输出结果。我想要计算真值的逻辑由用户提供。我目前的方法逻辑是由代码提供的。我如何让用户选择哪个输入的输出值为true?通过复选框或单独的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
+1这太好了。虽然我知道它是一个模拟。但是,你能详细说明这个答案,所以它可以成为一个有效的例子。我很难得到这个部分的工作:'List table;'和'table = GenerateTruthTable(3);'在我的表单中 –
CodingWonders90
2013-02-21 04:01:21