Ejemplo n.º 1
0
        public static List <Minterm> NewGroupMake(TruthTable groupTruthTable, int i, int j)
        {
            string result = GroupMake(groupTruthTable.TruthTabales[i][0].BinaryCode,
                                      groupTruthTable.TruthTabales[j][0].BinaryCode);
            List <Minterm> mintermsList = new List <Minterm>();

            for (int k = 0; k < groupTruthTable.TruthTabales[i].Count; k++)
            {
                Minterm minterm = new Minterm(groupTruthTable.TruthTabales[i][k].Number)
                {
                    BinaryCode = result
                };
                mintermsList.Add(minterm);
            }

            for (int k = 0; k < groupTruthTable.TruthTabales[j].Count; k++)
            {
                Minterm minterm = new Minterm(groupTruthTable.TruthTabales[j][k].Number)
                {
                    BinaryCode = result
                };
                mintermsList.Add(minterm);
            }

            return(mintermsList);
        }
Ejemplo n.º 2
0
        public static TruthTable GroupMintermCreate(TruthTable groupTruthTable, bool x)
        {
            List <List <Minterm> > mintermList1 = new List <List <Minterm> >();

            if (x == false)
            {
                for (int i = 0; i < groupTruthTable.TruthTabales.Length; i++)
                {
                    for (int j = 0; j < groupTruthTable.TruthTabales.Length; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        if (CheckDifferent(groupTruthTable.TruthTabales[i][0].BinaryCode,
                                           groupTruthTable.TruthTabales[j][0].BinaryCode))
                        {
                            mintermList1.Add(NewGroupMake(groupTruthTable, i, j));
                        }
                    }
                }
            }

            else
            {
                for (int i = 0; i < groupTruthTable.TruthTabales.Length - 1; i++)
                {
                    for (int j = 0; j < groupTruthTable.TruthTabales[i].Count; j++)
                    {
                        for (int k = 0; k < groupTruthTable.TruthTabales[i + 1].Count; k++)
                        {
                            if (CheckDifferent(groupTruthTable.TruthTabales[i][j].BinaryCode,
                                               groupTruthTable.TruthTabales[i + 1][k].BinaryCode))
                            {
                                mintermList1.Add(NewGroupMake(groupTruthTable, i, j, k));
                            }
                        }
                    }
                }
            }

            TruthTable truthTableGroup2 = new TruthTable(mintermList1.Count);

            for (int i = 0; i < mintermList1.Count; i++)
            {
                truthTableGroup2.TruthTabales[i] = mintermList1[i];
            }

            return(truthTableGroup2);
        }
Ejemplo n.º 3
0
        public static List <Minterm> NewGroupMake(TruthTable groupTruthTable, int i, int j, int k)
        {
            string result = GroupMake(groupTruthTable.TruthTabales[i][j].BinaryCode,
                                      groupTruthTable.TruthTabales[i + 1][k].BinaryCode);
            Minterm minterm1 = new Minterm(groupTruthTable.TruthTabales[i][j].Number);
            Minterm minterm2 = new Minterm(groupTruthTable.TruthTabales[i + 1][k].Number);

            minterm1.BinaryCode = result;
            minterm2.BinaryCode = result;
            List <Minterm> mintermsList = new List <Minterm>()
            {
                minterm1, minterm2
            };

            return(mintermsList);
        }
Ejemplo n.º 4
0
        private void BtnSimplify_Click(object sender, RoutedEventArgs e)
        {
            if (ComboBoxVariableCount.SelectedIndex == 0)
            {
                MessageBox.Show("Error");
                return;
            }

            List <Minterm> minterms = _mintermButtons
                                      .Where(b =>
                                             b.IsEnabled &&
                                             (b.Content.Equals("1") ||
                                              b.Content.Equals("D")))
                                      .Select(b =>
                                              new Minterm(Regex.Match(b.Name, @"\d+").Value))
                                      .ToList();

            if (ComboBoxVariableCount.SelectedIndex == 1)
            {
                try
                {
                    Minterm minterm4 = minterms.First(m => m.Number == "4");
                    minterms.Remove(minterm4);
                    minterms.Add(new Minterm("2"));
                }
                catch (InvalidOperationException invalidOperationException)
                {
                    Console.WriteLine(invalidOperationException);
                }

                try
                {
                    Minterm minterm5 = minterms.First(m => m.Number == "5");
                    minterms.Remove(minterm5);
                    minterms.Add(new Minterm("3"));
                }
                catch (InvalidOperationException invalidOperationException)
                {
                    Console.WriteLine(invalidOperationException);
                }
            }

            new Thread(() =>
            {
                List <Minterm> orginMinterms = minterms;

                List <int> numberOfOne = new List <int>();
                foreach (Minterm minterm in minterms)
                {
                    numberOfOne.Add(minterm.NumberofOnes);
                }

                int maxNumberOfOne = numberOfOne.Max();
                foreach (Minterm minterm in minterms)
                {
                    while (minterm.BinaryCode.Length < VariableCount)
                    {
                        minterm.BinaryCode = '0' + minterm.BinaryCode;
                    }
                }

                TruthTable groupTruthTable = new TruthTable(maxNumberOfOne);
                groupTruthTable.GroupLists(minterms);

                List <List <Implicant> > firstPrimeImplicants = new List <List <Implicant> >();
                List <Implicant> implicants = (from mintermList in groupTruthTable.TruthTabales
                                               from minterm in mintermList
                                               select new Implicant(new List <Minterm>()
                {
                    minterm
                })).ToList();

                firstPrimeImplicants.Add(implicants);
                TruthTable gpTruthTable = groupTruthTable;
                bool x = true;
                while (true)
                {
                    TruthTable gp;
                    if (x)
                    {
                        gp = GroupMintermCreate(gpTruthTable, true);
                        x  = false;
                    }
                    else
                    {
                        gp = GroupMintermCreate(gpTruthTable, false);
                    }

                    gp.SortGroupList();
                    if (gp.TruthTabales.Length == 0)
                    {
                        break;
                    }
                    List <Implicant> implicantsInWhileList = new List <Implicant>();
                    foreach (List <Minterm> mintrms in gp.TruthTabales)
                    {
                        List <Minterm> implicantMinterms = new List <Minterm>();
                        foreach (Minterm minterm in mintrms)
                        {
                            implicantMinterms.Add(minterm);
                        }

                        Implicant implicant = new Implicant(implicantMinterms);
                        implicantsInWhileList.Add(implicant);
                    }

                    firstPrimeImplicants.Add(implicantsInWhileList);
                    gpTruthTable = gp;
                }


                for (int i = firstPrimeImplicants.Count - 1; i >= 0; i--)
                {
                    for (int j = 0; j < firstPrimeImplicants[i].Count; j++)
                    {
                        if (firstPrimeImplicants[i][j].Status)
                        {
                            for (int k = 0; k < i; k++)
                            {
                                for (int l = 0; l < firstPrimeImplicants[k].Count; l++)
                                {
                                    if (CheckEqual(firstPrimeImplicants[i][j], firstPrimeImplicants[k][l]))
                                    {
                                        firstPrimeImplicants[k][l].Status = false;
                                    }
                                }
                            }
                        }
                    }
                }

                List <Implicant> primeImplicants = new List <Implicant>();
                foreach (List <Implicant> implicantList in firstPrimeImplicants)
                {
                    foreach (Implicant implicant in implicantList)
                    {
                        if (implicant.Status)
                        {
                            primeImplicants.Add(implicant);
                        }
                    }
                }


                List <Implicant> finalImplicants = new List <Implicant>();
                bool[] mintermsUse = new bool[orginMinterms.Count];
                for (int i = 0; i < mintermsUse.Length; i++)
                {
                    mintermsUse[i] = false;
                }

                bool[,] implicantsTable = new bool[primeImplicants.Count, orginMinterms.Count];
                for (int i = 0; i < orginMinterms.Count; i++)
                {
                    for (int j = 0; j < primeImplicants.Count; j++)
                    {
                        if (primeImplicants[j].Minterms.Contains(orginMinterms[i]))
                        {
                            implicantsTable[j, i] = true;
                        }
                        else
                        {
                            implicantsTable[j, i] = false;
                        }
                    }
                }

                while (!CheckMinterUse(mintermsUse))
                {
                    List <int> essentialList = new List <int>();
                    for (int i = 0; i < implicantsTable.GetLength(1); i++)
                    {
                        if (CheckEssentialImplicants(implicantsTable, i))
                        {
                            essentialList.Add(SendIndexOfEssential(implicantsTable, i));
                        }
                    }

                    foreach (int essential in essentialList)
                    {
                        for (int j = 0; j < orginMinterms.Count; j++)
                        {
                            if (primeImplicants[essential].Minterms.Contains(orginMinterms[j]))
                            {
                                mintermsUse[j] = true;
                            }
                        }
                        finalImplicants.Add(primeImplicants[essential]);
                    }

                    if (essentialList.Count > 0)
                    {
                        implicantsTable = UpdateImplicantsTable(implicantsTable, essentialList);
                        continue;
                    }

                    for (int i = 0; i < implicantsTable.GetLength(1); i++)
                    {
                        for (int j = 0; j < implicantsTable.GetLength(1); j++)
                        {
                            if (NumberOfDifferences(implicantsTable, i, j, false) == 1)
                            {
                                implicantsTable = UpdateImplicantsTable(implicantsTable,
                                                                        NumberOfTrue(implicantsTable, i, false) > NumberOfTrue(implicantsTable, j, false)
                                    ? i
                                    : j, false);
                            }
                        }
                    }


                    for (int i = 0; i < implicantsTable.GetLength(0); i++)
                    {
                        for (int j = 0; j < implicantsTable.GetLength(0); j++)
                        {
                            if (i == j)
                            {
                                continue;
                            }

                            if (NumberOfDifferences(implicantsTable, i, j, true) == 1)
                            {
                                implicantsTable = UpdateImplicantsTable(implicantsTable,
                                                                        NumberOfTrue(implicantsTable, i, true) > NumberOfTrue(implicantsTable, j, true) ? j : i,
                                                                        true);
                            }
                            else if (NumberOfDifferences(implicantsTable, i, j, true) == 0)
                            {
                                implicantsTable = UpdateImplicantsTable(implicantsTable, j, true);
                            }
                        }
                    }
                }

                List <Implicant> finalImplicantSorted = new List <Implicant>();
                foreach (Implicant implicant in finalImplicants)
                {
                    if (!finalImplicantSorted.Contains(implicant))
                    {
                        finalImplicantSorted.Add(implicant);
                    }
                }


                string result = "";
                for (int i = 0; i < finalImplicantSorted.Count; i++)
                {
                    result += finalImplicantSorted[i].ToString();
                    if (i % 10 == 0 && i != 0)
                    {
                        result += "\n";
                    }
                }

                Dispatcher.Invoke(() => { LblOutput.Content = result.Remove(result.Length - 1); });
            }).Start();
        }