private static List <List <LogicState> > getRowsWithTrueOutput(TruthTable input) { /* picks the rows with output equal to true * e.g.: * 000|0 * 001|1 * 010|1 * would return * 001|1 * 010|1 */ List <List <LogicState> > output = new List <List <LogicState> >(); for (int i = 0; i < input.OutputStates.Count; i++) { if (input.OutputStates[i] == LogicState.True) { output.Add(input.InputStates[i].Clone()); } } return(output); }
public void ProcessTruthTableString(string truthTable) { try { TruthTable newTruthTable = stringToTruthTable(truthTable); List <List <LogicState> > minimized = QuineMcCluskeyAlgorithm.MinimizeTruthTable(newTruthTable); newTruthTable.SetInputStates(minimized); newTruthTable.SetOutputStatesToTrue(); mainWindow.SetOutputLabelText(newTruthTable.ToString()); mainWindow.SetOutputEquationLabelText(BooleanAlgebra.TruthTableToEquation(newTruthTable)); } catch (Exception e) { MessageBox.Show("An error occured while processing the truth table. \n\n" + e.Message); } }
public static string TruthTableToEquation(TruthTable table) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < table.InputStates.Count; i++) { string rowEquation = TruthTableRowToEquation(table.Titles, table.InputStates[i]); sb.Append(rowEquation); if (i < table.InputStates.Count - 1 && rowEquation.Length > 0) { sb.Append(" " + Or + " "); } } if (sb.Length == 0 && table.InputStates.Count == 0) { sb.Append("0"); } return(table.Titles[table.Titles.Length - 1] + " = " + sb.ToString()); }
public static List <List <LogicState> > MinimizeTruthTable(TruthTable input) { if (input.OutputStates.Count != input.InputStates.Count) { throw new ArgumentException("Number of output and input states is not equal."); } else { List <List <LogicState> > trueRows = getRowsWithTrueOutput(input); bool minimized = false; while (!minimized && trueRows.Count > 0) { List <List <List <LogicState> > > sortedTrueRows = sortByNumberOfTruesOccuring(trueRows); List <List <bool> > handled = new List <List <bool> >(); for (int i = 0; i < sortedTrueRows.Count; i++) { handled.Add(new List <bool>()); for (int j = 0; j < sortedTrueRows[i].Count; j++) { // initialize with false handled[i].Add(false); } } List <List <LogicState> > newTrueRows = new List <List <LogicState> >(); // go through each block of rows (every block has the same amount of "trues") for (int i = 0; i < sortedTrueRows.Count - 1; i++) { // iterate through single block for (int j = 0; j < sortedTrueRows[i].Count; j++) { // rows in the next block for (int k = 0; k < sortedTrueRows[i + 1].Count; k++) { // only one field is different if (numberOfDifferentFields(sortedTrueRows[i][j], sortedTrueRows[i + 1][k]) == 1) { handled[i][j] = true; handled[i + 1][k] = true; newTrueRows.Add(sortedTrueRows[i][j].Clone()); newTrueRows[newTrueRows.Count - 1][indexOfOnlyDifferentField(sortedTrueRows[i][j], sortedTrueRows[i + 1][k])] = LogicState.DontCare; } } } } bool allNotHandled = true; for (int i = 0; i < handled.Count; i++) { for (int j = 0; j < handled[i].Count; j++) { if (!handled[i][j]) { newTrueRows.Add(sortedTrueRows[i][j].Clone()); } else { allNotHandled = false; } } } if (allNotHandled) { minimized = true; } trueRows = newTrueRows; } removeDoubles(trueRows); return(PetricksMethod.RemoveNonEssentialPrimeImplicants(trueRows)); } }