3

現在、C# で真理値表をブール式に変換する方法を複製しようとしています。3 変数 (a、b、c) の真理値表を生成し、複数行のテキスト ボックスに表示することができました。ユーザーが入力の出力ごとに決定できるように、追加の 8 つのテキスト ボックスを作成しまし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
    }
}

ここに画像の説明を入力

ここに画像の説明を入力

上の表は一例です。次のように真の出力値を表示したいと思います。

以下の結果: FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE

4

3 に答える 3

3

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));
    }
}
于 2013-02-17T20:55:13.240 に答える
2

センサー入力を保持して出力を生成するクラスを作成します。

public class SensorInput
{
    public SensorInput(bool a, bool b, bool c)
    {
        A = a;
        B = b;
        C = c;
    }

    public bool A { get; private set; }
    public bool B { get; private set; }
    public bool C { get; private set; }
    public bool Output
    {
        // output logic goes here
        get { return A || B || C; }
    }
}

次に、入力のリストを DataGridView コントロールにバインドします。

var inputs = new List<SensorInput>()
{
    new SensorInput(true, true, true),
    new SensorInput(true, true, false),
    new SensorInput(true, false, true),
    new SensorInput(true, false, false),
    new SensorInput(false, true, true),
    new SensorInput(false, true, false),
    new SensorInput(false, false, true),
    new SensorInput(false, false, false)
};

dataGridView1.DataSource = inputs;

デフォルトでは、ブール値が CheckBoxColumns にバインドされます。True/False をテキストとして表示する場合は、4 つの列を手動で追加します。それらのタイプを (読み取り専用) TextBoxColumns として選択し、バインディング用のプロパティ名を指定します。結果は次のようになります。

DataGridView

trueに等しい出力でテーブルをフィルタリングするには、Linq を使用できます。このような:

dataGridView1.DataSource = inputs.Where(i => i.Output);
于 2013-02-17T21:16:10.290 に答える
1

データを保持するクラスをいくつか作成します。

public class TruthTable {

    public TruthTable(int sensorCount) {
      if (sensorCount<1 || sensorCount >26) {
        throw new ArgumentOutOfRangeException("sensorCount");
      }
      this.Table=new Sensor[(int)Math.Pow(2,sensorCount)];
      for (var i=0; i < Math.Pow(2,sensorCount);i++) {
          this.Table[i]=new Sensor(sensorCount);
          for (var j = 0; j < sensorCount; j++) {
              this.Table[i].Inputs[sensorCount - (j + 1)] = ( i / (int)Math.Pow(2, j)) % 2 == 1;
          }
       }
    }   

    public Sensor[] Table {get; private set;}

    public string LiveOutputs {
      get {
        return string.Join("\n", Table.Where(x => x.Output).Select(x => x.InputsAsString));
      }
    }

    public string LiveOutPuts2 {
    get { 
        return string.Join(" + ", Table.Where(x => x.Output).Select (x => x.InputsAsString2));
    }
  }
}

// Define other methods and classes here
public class Sensor {

  public Sensor(int sensorCount) {
    if (sensorCount<1 || sensorCount >26) {
      throw new ArgumentOutOfRangeException("sensorCount");
    }
    this.SensorCount = sensorCount;
    this.Inputs=new bool[sensorCount];
  }

  private int SensorCount {get;set;}
  public bool[] Inputs { get; private set;}
  public bool Output {get;set;}

  public string InputsAsString {
    get {
        return string.Join(" ",Inputs.Select(x => x.ToString().ToUpper()));
    }
  }

  public string InputsAsString2 {
   get {
      var output=new StringBuilder();
      for (var i=0; i < this.SensorCount; i++) {
        var letter = (char)(i+65);
        output.AppendFormat("{0}{1}",Inputs[i] ? "" : "!", letter);
      }
      return output.ToString();
    }
  }
}

その後、真理値表のインスタンスを作成できます。

var table = new TruthTable(3);

次に、適切な出力を true に設定します

table.Table[3].Output=true;
table.Table[5].Output=true;
table.Table[6].Output=true;
table.Table[7].Output=true;

それからtable.LiveOutputsあなたを与える

FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE FALSE
TRUE TRUE TRUE

そしてtable.LiveOutputs2あなたに文字列を与えるでしょう

!ABC + A!BC + AB!C + ABC

使いました!上線の代わりに誤った入力を示す

編集 --- winforms についてのコメントの後

winforms コードを書いてから長い時間が経ちましたが、通常は WPF を使用しています。

コード レベルでコントロールを追加する場合、一部のコードはフォームの生成方法に依存します...

private CheckBox[] checkBoxes;
private TruthTable table;
private int sensors;

//Call this function from the constructor....
void InitForm() {
   this.sensors = 3;
   this.table= new TruthTable(this.sensors);
   this.checkBoxes = new CheckBox[this.sensors];
   for (Var i = 0; i < this.sensors; i++) {
     this.checkBox[i] = new CheckBox();
     // set the positioning of the checkbox - eg this.checkBox[i].Top = 100 + (i * 30);
     this.Controls.Add(this.checkBox[i]);

     // You can perform similar logic to create a text label control with the sensors in it.
   }
}


private void button1_Click(object sender, EventArgs e) {
   for (var i=0; i<this.sensors;i++) {
      this.table.Table[i].Output = this.checkBoxes[i].IsChecked;
   }
   this.outputTextBox.Text = this.table.LiveOutputs2;
}
于 2013-02-17T21:49:37.413 に答える