Example #1
0
        public override bool Equals(object obj)
        {
            TruthTable table = (TruthTable)obj;

            if (table == this)
            {
                return(true);
            }
            else if (table == null)
            {
                return(false);
            }
            else if (this.outputValues.Count != table.outputValues.Count)
            {
                return(false);
            }
            else
            {
                for (int i = 0; i < this.outputValues.Count; i++)
                {
                    if (this.outputValues[i] != table.outputValues[i])
                    {
                        return(false);
                    }
                }

                return(true);
            }
        }
        /*
         * This uses patricks method or a rough version of it. It is assumed to be demonstoubly slow, and improvement is sure to exist
         */
        private List <Implicant> patricksMethod(Tuple <List <Implicant>, List <int> > maybeEssential)
        {
            String patricksStringExpression = "";

            //Loops over each minterm
            for (int i = 0; i < maybeEssential.Item2.Count; i++)
            {
                // A Sum of Product String
                String SOP = "(";
                //Loops over each Implicant
                for (int j = 0; j < maybeEssential.Item1.Count; j++)
                {
                    //Checks if the specified implicant contains a reference to the targer minterm
                    if (maybeEssential.Item1[j].parents.Contains(maybeEssential.Item2[i]))
                    {
                        //Simply the First implicnt is labeled A, Second B and so on
                        if (SOP.Length != 1)
                        {
                            SOP += "+";
                        }
                        SOP += (char)(j + 'A');
                    }
                }
                patricksStringExpression += SOP + ")";
                if (i != maybeEssential.Item2.Count - 1)
                {
                    patricksStringExpression += "*";
                }
            }

            //Just a quick check to see if the expression doesnt exist. This is the case where there exists no possibly essential implicants
            if (patricksStringExpression.Equals(""))
            {
                return(new List <Implicant>());
            }

            //Creates a new BooleanExpression to represent the expression
            BooleanExpression patricksExpression = new BooleanExpression(patricksStringExpression);
            TruthTable        patricksTable      = new TruthTable(patricksExpression);
            //Console.WriteLine(patricksTable.ToString());

            List <List <Int32> > InputWithLowestImplicantCount = new List <List <int> >();

            for (int i = 0; i < patricksTable.inputValues.Count; i++)
            {
                //Only enter if the table output is true for this input combo (input i coresponds directly to output i)
                if (patricksTable.outputValues[i] == 1)
                {
                    //if master list is empty add first elemtn into it, garunteed to have less ones than null
                    if (!InputWithLowestImplicantCount.Any())
                    {
                        InputWithLowestImplicantCount.Add(patricksTable.inputValues[i]);
                    }
                    //if list has elements
                    else
                    {
                        //Checks idfthe current input has a lower number of ones, and thus implicants than the current lowest
                        if (this.numOnes(patricksTable.inputValues[i]) < this.numOnes(InputWithLowestImplicantCount[0]))
                        {
                            //Clear and add new lowest to list
                            InputWithLowestImplicantCount.Clear();
                            InputWithLowestImplicantCount.Add(patricksTable.inputValues[i]);
                        }
                    }
                }
            }

            //At this point you have a list with the input combinations with the fewest number of implicants

            // Now you need to convert all of those List of ints into Implicant representations again
            List <List <Implicant> > bestCombinations = new List <List <Implicant> >();

            for (int i = 0; i < InputWithLowestImplicantCount.Count; i++)
            {
                bestCombinations.Add(new List <Implicant>());
                //Each list is in the format 001 or 100 coresponding to some number of variables, in this example 3. The Most Significant bit is A, then B
                //And so on. A represents the first Implicant in the maybe essential Item 1 of The input tuple. So find that minterm by below
                for (int j = 0; j < InputWithLowestImplicantCount[i].Count; j++)
                {
                    if (InputWithLowestImplicantCount[i][j] == 1)
                    {
                        bestCombinations[i].Add(maybeEssential.Item1[j]);
                    }
                }
            }

            //If there is more than one best combination than the Truly best one will be the one with the lowest number of total variables
            int LowestNumVariables      = 0;
            int LowestNumVariablesIndex = 0;

            //Case where youve found the best combo, so return it
            if (bestCombinations.Count == 1)
            {
                return(bestCombinations[0]);
            }
            else
            {
                if (bestCombinations.Count != 1)
                {
                    for (int i = 0; i < bestCombinations.Count; i++)
                    {
                        int varCount = 0;
                        for (int j = 0; j < bestCombinations[i].Count; j++)
                        {
                            varCount += bestCombinations[i][j].numVariables();
                        }
                        if (varCount < LowestNumVariables)
                        {
                            LowestNumVariablesIndex = i;
                        }
                    }
                }

                return(bestCombinations[LowestNumVariablesIndex]);
            }
        }
Example #3
0
 //generates the truth table for this expression object
 private void GenerateTruthTable()
 {
     this.truthTable = new TruthTable(this);
 }