本文介绍了布尔逻辑/真值表和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试复制一种在 C# 中将真值表转换为布尔表达式的方法.我已经能够生成一个 3 变量 (a,b,c) 真值表并将其显示在多行文本框中.我创建了额外的八个文本框供用户决定每个输入的输出: true(1)false(0) .但是在生成表格后,我如何才能显示所有具有 true 值的输出?

I am currently trying to replicate a way of converting truth tables into Boolean expressions in C#. I have been able to generate a 3 variable (a,b,c) truth table and display it on a multiline textbox. I have created an additional eight textboxes for user to decide for each input’s output: either true(1) or false(0) . But After generating the table how can I then display the all the outputs that have a true value?

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
    }
}

上表是一个例子.我想以某种方式显示真实的输出值:

Table above is an example. I would like to display true output values somehow like this:

结果如下:假 真 真真假真真真假真真假

Results Below:FALSE TRUE TRUETRUE FALSE TRUETRUE TRUE FALSETRUE TRUE False

推荐答案

尝试封装您的 TruthItem(以及计算 TruthValue 的逻辑).那么使用真值表会很容易(生成、迭代、计算等)

Try encapsulating your TruthItem (along with the logic to calculate the TruthValue). It would be easy to work with the truth table then (generation, iteration, calculation, etc.)

这是示例控制台应用程序.它没有你的文本框,但你会明白的.

Here's sample console app. It doesn't have your textboxes, but you would get the idea.

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).

EDIT (included sample code for WinForm):
Use and refer the classes above (ThreeItemTruthRow and 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));
    }
}

这篇关于布尔逻辑/真值表和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 14:50