コード例 #1
0
ファイル: MainForm.cs プロジェクト: hamed1m54/PL-SYSTEM
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (!equRadioBtn.Checked && !conRadioBtn.Checked && !logConRadio.Checked)
            {
                MessageBox.Show(this, "Select a question first.", "Questions", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            string[] statements = inputBox.Text.Replace(" ", "").Split(new char[] { ',' });
            if (statements.Length < 2)
            {
                MessageBox.Show(this, "This operation need more than one statement." + Environment.NewLine +
                                "Enter a comma separated statements, select a question then click Answer again.",
                                "Questions", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            PL[] pl = new PL[statements.Length];
            for (int i = 0; i < statements.Length; i++)
            {
                try
                {
                    pl[i] = new PL(statements[i]);
                }
                catch (MalFormedException ex)
                {
                    logLabel.Text = "Formmat error at statement " + (i + 1) + ": " + ex.Message;
                    return;
                }
            }

            for (int i = 0; i < statements.Length; i++)
            {
                pl[i].GenerateTruthTable();
            }

            if (equRadioBtn.Checked)
            {
                for (int i = 0; i < pl.Length - 1; i++)
                {
                    if (pl[i].Letters.Length != pl[i + 1].Letters.Length)
                    {
                        MessageBox.Show(this, "Non homogeneous statement!." + Environment.NewLine +
                                        "All statements must have equal number of defferent letters.",
                                        "Hetrogeneous statements", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }

                for (int i = 0; i < pl[0].TruthTable.Height; i++)
                {
                    int width;
                    for (int j = 0; j < statements.Length - 1; j++)
                    {
                        width = pl[j].TruthTable.Width;
                        if (pl[j].TruthTable.GetOpTruthValue(pl[j].TruthTable.Width - 1)[i] != pl[j + 1].TruthTable.GetOpTruthValue(pl[j + 1].TruthTable.Width - 1)[i])
                        {
                            logLabel.Text = "The " + statements.Length + " statements are not equivalance";
                            return;
                        }
                    }
                    logLabel.Text = "The " + statements.Length + " statements are equivalance";
                }
            }
            else if (conRadioBtn.Checked)
            {
                for (int i = 0; i < pl.Length - 1; i++)
                {
                    if (pl[i].Letters.Length != pl[i + 1].Letters.Length)
                    {
                        MessageBox.Show(this, "Non homogeneous statement!." + Environment.NewLine +
                                        "All statements must have equal number of defferent letters.",
                                        "Hetrogeneous statements", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }

                for (int i = 0; i < pl[0].TruthTable.Height; i++)
                {
                    bool test  = false;
                    int  width = pl[0].TruthTable.Width;
                    if (pl[0].TruthTable.GetOpTruthValue(width - 1)[i])
                    {
                        test = true;
                        for (int j = 0; j < statements.Length; j++)
                        {
                            width = pl[j].TruthTable.Width;
                            test  = test && pl[j].TruthTable.GetOpTruthValue(width - 1)[i];
                        }
                    }
                    if (test)
                    {
                        logLabel.Text = "The " + statements.Length + " statements are consistent";
                        return;
                    }
                }
                logLabel.Text = "The " + statements.Length + " statements are inconsistent";
            }
            else
            {
                string alpha = "(" + statements[0] + ")";
                for (int i = 1; i < statements.Length - 1; i++)
                {
                    alpha += "^(" + statements[i] + ")";
                }

                string beta = statements[statements.Length - 1];

                PL first = new PL(alpha);
                first.GenerateTruthTable();
                PL second = new PL(beta);
                second.GenerateTruthTable();

                PL all = new PL(first.PL_Statement + "^" + second.PL_Statement);
                all.GenerateTruthTable();

                bool[] premises    = all.TruthTable.GetOpTruthValue(all.TruthTable.GetIndexOf(first.PL_Statement));
                bool[] concolusion = all.TruthTable.GetOpTruthValue(all.TruthTable.GetIndexOf(second.PL_Statement));

                for (int i = 0; i < all.TruthTable.Height; i++)
                {
                    if (premises[i] && !concolusion[i])
                    {
                        logLabel.Text = "The " + statements.Length + " statements are not logical consequence.";
                        return;
                    }
                }
                logLabel.Text = "The " + statements.Length + " statements are logical consequence.";
            }
        }