Ejemplo n.º 1
0
        public static string GetSimplified(IEnumerable <LogicItem> List, int variables, bool hazardsafe = false, bool lsba = false, bool negate = false)
        {
            var implicants = new List <Implicant>();

            var items        = (from i in List where i.Checked == true || i.Checked == null orderby i.Index ascending select i.Index).ToArray();
            var careminterms = (from i in List where i.Checked == true orderby i.Index ascending select i.Index).ToArray();



            foreach (var item in items)
            {
                var m = new Implicant();
                m.Mask = LogicItem.GetBinaryValue(item, variables);
                m.Minterms.Add(item);
                implicants.Add(m);
            }

            //int count = 0;
            while (Simplify(ref implicants))
            {
                //Populate a matrix.
                bool[,] matrix = new bool[implicants.Count, items.Length]; //x, y
                PopulateMatrix(ref matrix, implicants, items);
            }
            List <Implicant> selected;

            if (hazardsafe)
            {
                selected = implicants;
            }
            else
            {
                selected = SelectImplicants(implicants, careminterms);
            }
            return(GetFinalExpression(selected, lsba, negate));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Simplifies a givenset of implicants.
        /// </summary>
        private static bool Simplify(ref List <Implicant> implicants)
        {
            //Group by number of 1's and determine relationships by comparing.
            var groups        = (from i in Group(implicants) orderby i.Key select i).ToDictionary(i => i.Key, i => i.Value);
            var relationships = new List <ImplicantRelationship>();

            for (int i = 0; i < groups.Keys.Count; i++)
            {
                if (i == (groups.Keys.Count - 1))
                {
                    break;
                }

                var thisGroup = groups[groups.Keys.ElementAt(i)];
                var nextGroup = groups[groups.Keys.ElementAt(i + 1)];

                var q = from a in thisGroup
                        from b in nextGroup
                        where Utility.GetDifferences(a.Mask, b.Mask) == 1
                        select new ImplicantRelationship(a, b);

                relationships.AddRange(q);
            }


            //For each relationship, find the affected minterms and remove them.
            //Then add a new implicant which simplifies the affected minterms.
            foreach (ImplicantRelationship relationship in relationships)
            {
                var removeList = new List <Implicant>();

                foreach (Implicant m in implicants)
                {
                    if (relationship.A.Equals(m) || relationship.B.Equals(m))
                    {
                        removeList.Add(m);
                    }
                }

                foreach (Implicant m in removeList)
                {
                    implicants.Remove(m);
                }

                var newImplicant = new Implicant
                {
                    Mask = Utility.GetMask(relationship.A.Mask, relationship.B.Mask)
                };
                newImplicant.Minterms.AddRange(relationship.A.Minterms);
                newImplicant.Minterms.AddRange(relationship.B.Minterms);

                bool exist = false;
                foreach (Implicant m in implicants)
                {
                    if (m.Mask == newImplicant.Mask)
                    {
                        exist = true;
                    }
                }

                if (!exist) //Why am I getting dupes?
                {
                    implicants.Add(newImplicant);
                }
            }

            //Return true if simplification occurred, false otherwise.
            return(!(relationships.Count == 0));
        }