Ejemplo n.º 1
0
        public bool SetFormula(string mainFormula)
        {
            if (FormulaValidationHelper.CheckValidFormula(mainFormula))
            {
                MainFormula = AnalyzeFormula(mainFormula);
                TruthTable  = new TruthTable(MainFormula, MainFormula.Variables);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private void CreateTruthTable()
        {
            if (_myTree.MainFormula == null)
            {
                MessageBox.Show("Could not provide a truth table for an empty formula!!", "Error");
                return;
            }
            try
            {
                //Compute truth value
                TruthTable   myTruthTable   = _myTree.TruthTable;
                List <int[]> preComputeData = TruthTableHelper.GetPreComputeValues(_myTree.Variables);
                List <int>   result         = myTruthTable.ComputeTruthTable();

                //Setup DataGridView
                dgvTruthTable.ColumnCount       = myTruthTable.NumberOfColumn + 1;
                dgvTruthTable.RowCount          = myTruthTable.NumberOfRow;
                dgvTruthTable.Name              = "Truth Table";
                dgvTruthTable.RowHeadersVisible = false;
                dgvTruthTable.AutoSize          = true;

                for (int i = 0; i < myTruthTable.NumberOfColumn; i++)
                {
                    dgvTruthTable.Columns[i].Name = myTruthTable.Variables[i].Proposition.ToString();
                }

                dgvTruthTable.Columns[dgvTruthTable.ColumnCount - 1].Name = _originalFormula;

                for (int i = 0; i < myTruthTable.Variables.Count + 1; i++)
                {
                    for (int j = 0; j < myTruthTable.NumberOfRow; j++)
                    {
                        dgvTruthTable.Rows[j].Cells[i].Value = i != myTruthTable.Variables.Count ? preComputeData[i][j] : result[j];
                    }
                }

                foreach (DataGridViewColumn column in dgvTruthTable.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    column.SortMode     = DataGridViewColumnSortMode.NotSortable;
                }

                dgvTruthTable.Columns[dgvTruthTable.ColumnCount - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
                throw;
            }
        }
        private void ShowSimplifyTruthTable()
        {
            try
            {
                //Compute truth value
                TruthTable myTruthTable     = _myTree.TruthTable;
                var        truthTableValues = TruthTableHelper.GetPreComputeValues(_myTree.Variables);
                var        truthTableResult = myTruthTable.ComputeTruthTable();
                var        result           = myTruthTable.ComputeSimplifiedTruthTable();
                var        falseRows        = TruthTableHelper.GetFalseRows(truthTableValues, truthTableResult, _myTree.Variables);

                //Setup DataGridView
                dgvSimplifiedTruthTable.Name = "Simplified Truth Table";
                dgvSimplifiedTruthTable.RowHeadersVisible = false;
                dgvSimplifiedTruthTable.AutoSize          = true;


                if (result != null)
                {
                    dgvSimplifiedTruthTable.ColumnCount = myTruthTable.NumberOfColumn + 1;
                    dgvSimplifiedTruthTable.RowCount    = falseRows.Count + TruthTableHelper.GetNrOfSimplifiedRows(result);

                    for (int i = 0; i < myTruthTable.NumberOfColumn; i++)
                    {
                        dgvSimplifiedTruthTable.Columns[i].Name = myTruthTable.Variables[i].Proposition.ToString();
                    }

                    dgvSimplifiedTruthTable.Columns[dgvSimplifiedTruthTable.ColumnCount - 1].Name = _originalFormula;
                    dgvSimplifiedTruthTable.Rows.Clear();


                    for (int j = 0; j < falseRows.Count; j++)
                    {
                        string[] rows = new string[falseRows[j].Length + 1];
                        falseRows[j].CopyTo(rows, 0);
                        rows[falseRows[j].Length] = "0";
                        dgvSimplifiedTruthTable.Rows.Add(rows);
                    }

                    foreach (var listOfSimplifiedRows in result)
                    {
                        for (int i = 0; i < listOfSimplifiedRows.Count; i++)
                        {
                            string[] rows = new string[listOfSimplifiedRows[i].Length + 1];
                            listOfSimplifiedRows[i].CopyTo(rows, 0);
                            rows[listOfSimplifiedRows[i].Length] = "1";
                            dgvSimplifiedTruthTable.Rows.Add(rows);
                        }
                    }
                }
                else
                {
                    dgvSimplifiedTruthTable.ColumnCount = 1;
                    dgvSimplifiedTruthTable.RowCount    = 1;
                    dgvSimplifiedTruthTable.Rows.Clear();
                    string[] errorMessage = { "Could not simplify the truth table of this formula!!" };
                    dgvSimplifiedTruthTable.Rows.Add(errorMessage);
                }

                foreach (DataGridViewColumn column in dgvSimplifiedTruthTable.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    column.SortMode     = DataGridViewColumnSortMode.NotSortable;
                }

                dgvSimplifiedTruthTable.Columns[dgvSimplifiedTruthTable.ColumnCount - 1].AutoSizeMode =
                    DataGridViewAutoSizeColumnMode.Fill;

                this.Width  = dgvSimplifiedTruthTable.Width + 40;
                this.Height = dgvSimplifiedTruthTable.Height + 60;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
                throw;
            }
        }