public TruthTable GenerateTruthTable()
        {
            TruthTable truthTable = new TruthTable(this);

            truthTable.Calculate();
            return(truthTable);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate the DNF in prefix form, from the simplified truth table
        /// </summary>
        /// <param name="truthTable">The truth table</param>
        /// <returns>DNF in prefix form</returns>
        public string ConvertToDNF(TruthTable truthTable)
        {
            string[]      header = truthTable.GenerateHeaders();
            List <string> rows   = truthTable.GenerateSimplifiedRows();

            // filter out the 0 rows
            rows = rows.Where(row => row.Last() == '1').ToList();

            // Tautology
            if (truthTable.Result.Contains("0") == false)
            {
                return("|(a, ~(a))");
            }
            // Contradiction
            if (truthTable.Result.Contains("1") == false)
            {
                return("&(a, ~(a))");
            }

            string expression = "";

            // Convert each row to a DNF clause
            for (int i = 0; i < rows.Count; i++)
            {
                string clause = ConvertToDNFClause(header, rows[i]);

                // only one clause
                if (rows.Count == 1)
                {
                    return(string.Format("{0}", clause));
                }

                if (i != rows.Count - 1)
                {
                    expression = expression + "|(" + clause + ",";
                }
                else // last row
                {
                    expression += clause;
                }
            }

            for (int i = 0; i < rows.Count - 1; i++)
            {
                expression += ")";
            }

            return(expression);
        }