0

我目前正在使用布尔逻辑和真值表。我已经能够创建一个将生成 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 在此处输入图像描述

在此处输入图像描述

4

2 回答 2

2

You can bind a list of objects to a DataGridView on your form and create columns for each of the input and output fields. Using DataGridViewCheckBoxColumn for the columns gives you a set of check boxes that the user can interact with. Set ReadOnly on the input columns, and maybe change the background color to indicate that they are fixed.

You can either define the columns dynamically or create as many columns as you might ever need and hide the ones you're not using. Depends on how you want to define the data you're attaching them to more than anything.

Here's a mockup I did in VS2010:

enter image description here

This is bound to a list of the following object type:

public class TruthTableEntry
{
    public bool A { get; set; }
    public bool B { get; set; }
    public bool C { get; set; }
    public bool D { get; set; }
    public bool Out { get; set; }
}

Here's the form code for my mockup:

public partial class Form1 : Form
{
    List<TruthTableEntry> table;

    public Form1()
    {
        // generate a 3-input truth table (8 input variations)
        table = GenerateTruthTable(3);

        // setup DataGridView
        dataGridView1.DataSource = table;
        dataGridView1.Columns[0].DataPropertyName = "A";
        dataGridView1.Columns[1].DataPropertyName = "B";
        dataGridView1.Columns[2].DataPropertyName = "C";
        dataGridView1.Columns[3].DataPropertyName = "D";
        dataGridView1.Columns[4].DataPropertyName = "Out";
        // hide column 3 ('D') as unused
        dataGridView1.Columns[3].Visible = false;
    }
}

// Create a truth table for 1-4 inputs
static List<TruthTableEntry> GenerateTruthTable(int numInputs)
{
    // range-check number of inputs
    if (numInputs < 1 || numInputs > 4)
        return null;

    // calculate number of input states
    int permutations = 1 << numInputs;

    // create result list of input states
    List<TruthTableEntry> res = new List<TruthTableEntry>();
    for (int i = 0; i < permutations; ++i)
    {
        // use bit manipulation to initialize a TruthTableEntry:
        TruthTableEntry ent = new TruthTableEntry 
            {
                A = (i & (1 >> (NumInputs - 1))) != 0,
                B = (i & (1 >> (NumInputs - 2))) != 0,
                C = (i & (1 >> (NumInputs - 3))) != 0,
                D = (i & (1 >> (NumInputs - 4))) != 0,
                Out = false,
            };
        res.Add(ent);
    }
    return res;
}

dataGridView1 is a DataGridView with 5 DataGridViewCheckBoxColumn columns defined, with first 4 columns set to ReadOnly and colored.

于 2013-02-19T01:18:39.333 回答
0

我建议使用带有单独文本框的动态 DataGridView 来指示变量的数量。

默认值可以是 2 个变量,并且在启动时会生成一个 DataGridView 作为真值表,并带有一个额外的最后一列,用于编辑每行的所需输出。

如果用户更改了变量的数量,请相应地调整您的 DataGridView。(您可能需要添加一个按钮来更改 DataGridView)

无论如何,我建议将界面设计为需要尽可能少的点击/击键。

于 2013-02-19T00:47:46.690 回答