gpt4 book ai didi

c# - bool 逻辑/真值表和输出

转载 作者:行者123 更新时间:2023-11-30 14:33:59 25 4
gpt4 key购买 nike

我目前正在尝试复制一种在 C# 中将真值表转换为 bool 表达式的方法。我已经能够生成一个 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
}
}

enter image description here

enter image description here

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

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

最佳答案

尝试封装您的 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));
}
}

关于c# - bool 逻辑/真值表和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14925277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com