Advertisement
anas_harby

Untitled

May 2nd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.89 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.io.*;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.Scanner;
  6.  
  7. /**
  8.  * The Class QM Solver.
  9.  */
  10. public class Solver {
  11.  
  12.     /** The minterms (Decimal integers). */
  13.     static ArrayList<Integer> minterms = new ArrayList<Integer>();
  14.    
  15.     /** The dontcares (Decimal integers). */
  16.     static ArrayList<Integer> dontcares = new ArrayList<Integer>();
  17.    
  18.     /** The comparisons (3D arrayList, 1st Dimension holds comparison, 2nd holds groups
  19.      * ,3rd holds binary representation alongside the minterms it fulfills, e.x: 0--1 1 3 5 7).
  20.      * . */
  21.     static ArrayList<ArrayList<ArrayList<String>>> comparisons = new ArrayList<ArrayList<ArrayList<String>>>();
  22.    
  23.     /** The essential prime implicants. (same representation as the above) */
  24.     static ArrayList<String> essentialPrimeImplicantsContainingOne = new ArrayList<String>();
  25.    
  26.     /** The minterms apperancescount */
  27.     static int[][] mintermsApperancescount;
  28.    
  29.     /** The prime implicants (taken before coverage table). */
  30.     static ArrayList<String> primeImplicants = new ArrayList<String>();
  31.  
  32.     /** The combination (combination of terms generated in coverage table). */
  33.     static String[] combination = new String[26];
  34.  
  35.     /** number of variables/bits needed to represent each minterm. */
  36.     static int numberOfBits;
  37.    
  38.     /** checks if generateCombinations() method has found a suitable combination. */
  39.     static boolean foundCombination = false;
  40.  
  41.     /** The scanner. */
  42.     static Scanner in = new Scanner(System.in);
  43.  
  44.     /**
  45.      * Pre-fills array lists with empty data to enable accessing it with random indexes.
  46.      */
  47.     private static void fillArrayList() {
  48.         for (int i = 0; i < 26; i++) {
  49.             comparisons.add(new ArrayList()); // comparisons
  50.             for (int j = 0; j <= numberOfBits; j++)
  51.                 comparisons.get(i).add(new ArrayList()); // groups
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Differ binary.
  57.      *
  58.      * @param firstMinterm the first minterm in a binary string representation.
  59.      * @param secondMinterm the second minterm in a binary string representations.
  60.      * @return a string in a binary representation with difference represented as dashes. returns the number of ones in the end of the string.
  61.      */
  62.     private static String differBinary(String firstMinterm, String secondMinterm) {
  63.         String result = "";
  64.         int ones = 0;
  65.         for (int i = 0; i < firstMinterm.length(); i++) {
  66.             if (firstMinterm.charAt(i) != secondMinterm.charAt(i))
  67.                 result += "-";
  68.             else
  69.                 result += firstMinterm.charAt(i);
  70.             if (firstMinterm.charAt(i) == '1')
  71.                 ones++;
  72.         }
  73.         return result + " " + String.valueOf(ones);
  74.     }
  75.  
  76.     /**
  77.      * Converter from decimal to binary (needed to count ones, adds result immediately in the first comparison --> comparisons[0]).
  78.      */
  79.     private static void decimalToBinary() {
  80.  
  81.         for (int i = 0; i < minterms.size(); i++) {
  82.             int div = minterms.get(i), ones = 0;
  83.             String res = "";
  84.             while (div != 0) {
  85.                 if (div % 2 == 1) {
  86.                     ones++;
  87.                     res = "1" + res;
  88.                 } else
  89.                     res = "0" + res;
  90.                 div /= 2;
  91.             }
  92.             for (int j = 0, len = res.length(); j < numberOfBits - len; j++)
  93.                 res = "0" + res;
  94.             comparisons.get(0).get(ones).add(res + " " + Integer.parseInt(res, 2));
  95.         }
  96.         for (int i = 0; i < dontcares.size(); i++) {
  97.             int div = dontcares.get(i), ones = 0;
  98.             String res = "";
  99.             while (div != 0) {
  100.                 if (div % 2 == 1) {
  101.                     ones++;
  102.                     res = "1" + res;
  103.                 } else
  104.                     res = "0" + res;
  105.                 div /= 2;
  106.             }
  107.             for (int j = 0, len = res.length(); j < numberOfBits - len; j++)
  108.                 res = "0" + res;
  109.             comparisons.get(0).get(ones).add(res + " " + Integer.parseInt(res, 2));
  110.         }
  111.         for (int i = 0; i < comparisons.get(0).size(); i++)
  112.             Collections.sort(comparisons.get(0).get(i));
  113.     }
  114.  
  115.     /**
  116.      * Do comparisons.
  117.      */
  118.     private static void doComparisons() {
  119.         int k;
  120.         for (k = 0; k < comparisons.size() - 1; k++) { // Loop through comparisons
  121.             boolean[][] takenToNextComparison = new boolean[10][100];
  122.             for (int l = 0; l < comparisons.get(k).size(); l++) { // Loop through groups
  123.                 if (comparisons.get(k).get(l).isEmpty()) //continue if group is empty.
  124.                     continue;
  125.                 int m = l + 1;
  126.  
  127.                 while (m < comparisons.get(k).size() && comparisons.get(k).get(m).isEmpty()) //find a next group that isn't empty.
  128.                     m++;
  129.                 if (m == comparisons.get(k).size()) { //if this is the last group, add terms that were not taken before into the prime implicants.
  130.                     for (int i = 0; i < comparisons.get(k).get(l).size(); i++) {
  131.                         if (!takenToNextComparison[l][i]) {
  132.                             ArrayList<String> iData = new ArrayList<String>();
  133.                             for (String retval : comparisons.get(k).get(l).get(i).split(" "))
  134.                                 iData.add(retval);
  135.                             String info = iData.get(0);
  136.                             for (int x = 1; x < iData.size(); x++)
  137.                                 info += " " + iData.get(x);
  138.                             primeImplicants.add(info);
  139.                         }
  140.                     }
  141.                     break;
  142.                 }
  143.                 for (int i = 0; i < comparisons.get(k).get(l).size(); i++) {
  144.                     boolean fPrime = true;
  145.                     ArrayList<String> iData = new ArrayList<String>();
  146.                     for (String retval : comparisons.get(k).get(l).get(i).split(" ")) //get data inside comparisons arraylist for the 1st term.
  147.                         iData.add(retval);
  148.                     for (int j = 0; j < comparisons.get(k).get(m).size(); j++) {
  149.                         ArrayList<String> jData = new ArrayList<String>();
  150.                         for (String retval : comparisons.get(k).get(m).get(j).split(" "))
  151.                             jData.add(retval);
  152.                         float exponent = 0;
  153.                         int difference = 0;
  154.                         boolean fAccept = true;
  155.                         for (int x = 1; x < iData.size(); x++) {
  156.                             int iDifference = Integer.parseInt(iData.get(x));
  157.                             int jDifference = Integer.parseInt(jData.get(x));
  158.                             int tempDifference = jDifference - iDifference;
  159.                             float tempExponent = (float) (Math.log(tempDifference) / Math.log(2)); //difference between the two is 2^n.
  160.                             if (x == 1) {
  161.                                 difference = tempDifference;
  162.                                 exponent = tempExponent;
  163.                                 if (difference < 0 || exponent - (int) exponent != 0) {
  164.                                     fAccept = false;
  165.                                     break;
  166.                                 }
  167.                             } else {
  168.                                 if (tempDifference != difference || tempExponent != exponent) {
  169.                                     fAccept = false;
  170.                                     break;
  171.                                 }
  172.                             }
  173.                         }
  174.                         if (fAccept) {
  175.                             fPrime = false;
  176.                             String iBinary = iData.get(0);
  177.                             String jBinary = jData.get(0);
  178.                             takenToNextComparison[l][i] = true; //the implicant is taken.
  179.                             takenToNextComparison[m][j] = true;
  180.                             ArrayList<String> dashesAndOnes = new ArrayList<String>();
  181.                             for (String retval : differBinary(iBinary, jBinary).split(" "))
  182.                                 dashesAndOnes.add(retval);
  183.                             String differenceBinary = dashesAndOnes.get(0);
  184.                             int ones = Integer.parseInt(dashesAndOnes.get(1));
  185.                             boolean fDuplicate = false;
  186.                             for (int x = 0; x < comparisons.get(k + 1).size(); x++) { //checks for duplicates.
  187.                                 for (int y = 0; y < comparisons.get(k + 1).get(x).size(); y++) {
  188.                                     String duplicate = comparisons.get(k + 1).get(x).get(y).split(" ")[0];
  189.                                     if (duplicate.equals(differenceBinary)) {
  190.                                         fDuplicate = true;
  191.                                         break;
  192.                                     }
  193.                                 }
  194.                             }
  195.                             if (!fDuplicate) {
  196.                                 String info = differenceBinary; //assign data for the next comparison.
  197.                                 for (int x = 1; x < iData.size(); x++) {
  198.                                     info += " " + iData.get(x);
  199.                                 }
  200.                                 for (int x = 1; x < jData.size(); x++) {
  201.                                     info += " " + jData.get(x);
  202.                                 }
  203.                                 comparisons.get(k + 1).get(ones).add(info);
  204.                             } else
  205.                                 continue;
  206.  
  207.                         }
  208.                     }
  209.                     if (fPrime && !takenToNextComparison[l][i]) { //add to prime implicants.
  210.                         String info = iData.get(0);
  211.                         for (int x = 1; x < iData.size(); x++)
  212.                             info += " " + iData.get(x);
  213.                         primeImplicants.add(info);
  214.                     }
  215.  
  216.                 }
  217.             }
  218.         }
  219.         for (int i = 0; i < comparisons.get(k).size(); i++) // add all the implicants in the last comparison to prime implicants.
  220.             for (int j = 0; j < comparisons.get(k).get(i).size(); j++) {
  221.                 ArrayList<String> data = new ArrayList<String>();
  222.                 for (String retval : comparisons.get(k).get(i).get(j).split(" "))
  223.                     data.add(retval);
  224.                 String info = data.get(0);
  225.                 for (int x = 1; x < data.size(); x++)
  226.                     info += " " + data.get(x);
  227.                 primeImplicants.add(info);
  228.             }
  229.     }
  230.  
  231.     /**
  232.      * Returns an alphabetical representation of a binary number with dashes.
  233.      *
  234.      * @param arrayList required
  235.      * @return string expression
  236.      */
  237.     private static String findExpression(ArrayList required) {
  238.         String expression = new String();
  239.         ;
  240.         StringBuilder dashesExpression = null;
  241.         StringBuilder expressionBuilder = new StringBuilder();
  242.  
  243.         for (int i = 0; i < required.size(); i++) {
  244.             for (String retval : ((String) required.get(i)).split(" ")) {
  245.                 dashesExpression = new StringBuilder();
  246.                 dashesExpression.append(retval);
  247.                 break;
  248.             }
  249.  
  250.             for (int j = 0; j < dashesExpression.length(); j++) {
  251.                 if (dashesExpression.charAt(j) == '1') {
  252.                     char character = (char) ((int) ('A') + j);
  253.                     expressionBuilder.append(character);
  254.                 } else if (dashesExpression.charAt(j) == '0') {
  255.                     char character = (char) ((int) ('A') + j);
  256.                     String str = new String();
  257.                     str += character + "'";
  258.                     expressionBuilder.append(str);
  259.  
  260.                 }
  261.  
  262.             }
  263.             if (i != (required.size() - 1))
  264.                 expressionBuilder.append(" + ");
  265.         }
  266.  
  267.         expression = expressionBuilder.toString();
  268.         return expression;
  269.     }
  270.    
  271.     /**
  272.      * Checks whether shifting to getting reduced prime implicants entering the combinations generating process is needed or not.
  273.      *
  274.      * @return true, if successful
  275.      */
  276.     private static boolean containsOne() {
  277.         for (int i = 0; i < mintermsApperancescount.length; i++) {
  278.             if ((mintermsApperancescount[i][1]) == 1)
  279.                 return true;
  280.         }
  281.         return false;
  282.     }
  283.  
  284.     /**
  285.      * Counts the appearances of each minterm in each combined group.
  286.      * NOTE:  Combined groups arise from the method doComparisons()
  287.      */
  288.     private static void countAppearances() {
  289.         StringBuilder str2;
  290.         int foundFlag = 0;
  291.         mintermsApperancescount = new int[minterms.size()][2];
  292.         for (int i = 0; i < minterms.size(); i++) {
  293.             mintermsApperancescount[i][0] = minterms.get(i);
  294.  
  295.         }
  296.         for (int i = 0; i < primeImplicants.size(); i++) {
  297.             String str = primeImplicants.get(i).split(" ", 2)[1];
  298.             str2 = new StringBuilder();
  299.             for (int j = 0; j <= str.length(); j++) {
  300.                 foundFlag = 0;
  301.                 if ((j != str.length()) && (str.charAt(j) != ' ')) {
  302.                     str2.append(str.charAt(j));
  303.  
  304.                 } else {
  305.                     int vaL = Integer.parseInt(str2.toString());
  306.                     for (int k = 0; k < mintermsApperancescount.length; k++) {
  307.                         if (vaL == mintermsApperancescount[k][0]) {
  308.                             foundFlag = 1;
  309.                             mintermsApperancescount[k][1]++;
  310.                             str2 = new StringBuilder();
  311.                             break;
  312.  
  313.                         }
  314.                     }
  315.                     if (foundFlag == 0)
  316.                         str2 = new StringBuilder();
  317.  
  318.                 }
  319.             }
  320.         }
  321.  
  322.     }
  323.  
  324.  
  325.  
  326.     /**
  327.      * Finds minterms that appeared only once in a certain column and gets a corresponding expreesion using an array.
  328.      * Removes each minterm taken from consideration as they're not needed in the next step in the combination generating.
  329.      */
  330.     private static void getReducedEssentialImplicants() {
  331.         int uniqueMinterm, foundFlag = 0;
  332.         StringBuilder str2;
  333.  
  334.         for (int i = 0; i < mintermsApperancescount.length; i++) {
  335.             if (mintermsApperancescount[i][1] == 1) {
  336.                 uniqueMinterm = mintermsApperancescount[i][0];
  337.                 for (int j = 0; j < primeImplicants.size(); j++) {
  338.                     String str = primeImplicants.get(j).split(" ", 2)[1];
  339.                     str2 = new StringBuilder();
  340.                     for (int k = 0; k <= str.length(); k++) {
  341.                         if ((k != str.length()) && (str.charAt(k) != ' ')) {
  342.                             str2.append(str.charAt(k));
  343.  
  344.                         } else if (((k != str.length()) && (str.charAt(k) == ' ')) || (k == (str.length()))) {
  345.                             int value = Integer.parseInt(str2.toString());
  346.                             if (value == uniqueMinterm) {
  347.                                 essentialPrimeImplicantsContainingOne.add(primeImplicants.get(j));
  348.                                 /*
  349.                                  * the implicant that matches certain conditions should be
  350.                                  * promoted to be essential and removed from the prime implicants arraylist.
  351.                                  */
  352.                                 primeImplicants.remove(j);
  353.                                 str2 = new StringBuilder();
  354.                                 for (int u = 0; u <= str.length(); u++) {
  355.                                     foundFlag = 0;
  356.                                     if ((u != str.length()) && (str.charAt(u) != ' ')) {
  357.                                         str2.append(str.charAt(u));
  358.  
  359.                                     } else {
  360.                                         int vaL = Integer.parseInt(str2.toString());
  361.                                         for (int m = 0; m < mintermsApperancescount.length; m++) {
  362.                                             if (vaL == mintermsApperancescount[m][0]) {
  363.                                                 foundFlag = 1;
  364.                                                 mintermsApperancescount[m][1] = 0;
  365.                                                 /*
  366.                                                  * Cancels out all the minterms in the row and the columns
  367.                                                  * that matches the taken row "expression".
  368.                                                  */
  369.                                                 str2 = new StringBuilder();
  370.                                                 ;
  371.                                                 break;
  372.  
  373.                                             }
  374.                                         }
  375.                                         if (foundFlag == 0)
  376.                                             str2 = new StringBuilder();
  377.  
  378.                                     }
  379.  
  380.                                 }
  381.                             } else {
  382.                                 str2 = new StringBuilder();
  383.                             }
  384.                         }
  385.                     }
  386.                 }
  387.             }
  388.         }
  389.  
  390.     }
  391.    
  392.     /**
  393.      * Checks whether the essential prime implicants already covered all the minterms before trying to generate combinations.
  394.      *
  395.      * @return true, if successful
  396.      */
  397.     private static boolean needsCombinations() {
  398.         int countZeros = 0;
  399.  
  400.         for (int i = 0; i < mintermsApperancescount.length; i++) {
  401.             if (mintermsApperancescount[i][1] == 0) {
  402.                 for (int j = 0; j < minterms.size(); j++) {
  403.                     if (mintermsApperancescount[i][0] == minterms.get(j)) {
  404.                         minterms.remove(j);
  405.                         break;
  406.                     }
  407.  
  408.                 }
  409.                 countZeros++;
  410.             }
  411.         }
  412.         if (countZeros == mintermsApperancescount.length) {  
  413.             /*
  414.             if all the terms have the zero value in appearance cell therefore
  415.             it's not needed to proceed to another level of computations using the combinations generator
  416.             as it's already done.
  417.            */
  418.             return true;
  419.         } else
  420.             return false;
  421.     }
  422.  
  423.     /**
  424.      * Check combination.
  425.      *
  426.      * @param len length of the combination.
  427.      * @return true, if combination covered all the remaining minterms.
  428.      */
  429.     private static boolean checkCombination(int len) {
  430.         ArrayList<Integer> checkedMinterms = new ArrayList<Integer>();
  431.  
  432.         for (int i = 0; i < len; i++) {
  433.             ArrayList<String> data = new ArrayList<String>();
  434.             for (String retval : combination[i].split(" "))
  435.                 data.add(retval);
  436.             for (int j = 1; j < data.size(); j++)
  437.                 checkedMinterms.add(Integer.parseInt(data.get(j)));
  438.         }
  439.         if (checkedMinterms.containsAll(minterms))
  440.             return true;
  441.         else
  442.             return false;
  443.     }
  444.    
  445.     /**
  446.      * Generate combinations.
  447.      *
  448.      * @param element the element
  449.      * @param m the m
  450.      * @param len the len
  451.      */
  452.     private static void generateCombinations(int element, int m, int len) { // (1,0,size)
  453.         if (m == len) {
  454.             if (checkCombination(len)) {
  455.                 foundCombination = true;
  456.                 fillEssentialPrime(len);
  457.                 Arrays.fill(combination, "");
  458.             }
  459.             return;
  460.         }
  461.         if (element == primeImplicants.size() + 1)
  462.             return;
  463.         combination[m] = primeImplicants.get(element - 1);
  464.         generateCombinations(element + 1, m + 1, len);
  465.         generateCombinations(element + 1, m, len);
  466.     }
  467.  
  468.     /**
  469.      * Takes the combination and adds it to a local list (since a global one won't be needed taken into consideration the probability of
  470.      * getting more than one minimal expression). prints the implicants immediately.
  471.      * Prints the essential prime implicants immediately.
  472.      * Generates an alphabetical expression and prints it.
  473.      * NOTE: (if needed to separate the printing process, a new 2D array list can be added globally and with a proper string representation
  474.      *        minimal expressions can be added, ex: ( 1 >> [BC' + AB' + ABC', 1 2, 4 5, 3 5 6, ...etc] ) and dealt with later with split()  
  475.      * @param combinationLength the number of essential prime implicants inside combination.
  476.      */
  477.     private static void fillEssentialPrime(int combinationLength) {
  478.         ArrayList<String> combinationEssential = new ArrayList<String>();
  479.         for (int i = 0; i < combinationLength; i++) {
  480.             String binaryRepresentation = combination[i].split(" ",2)[0];
  481.             String numericRepresentation = combination[i].split(" ",2)[1];
  482.             System.out.println(binaryRepresentation + "\t" + "(" + numericRepresentation + ")");
  483.             combinationEssential.add(combination[i]);
  484.         }
  485.         for(int i=0; i<essentialPrimeImplicantsContainingOne.size(); i++) {
  486.             String binaryRepresentation = essentialPrimeImplicantsContainingOne.get(i).split(" ",2)[0];
  487.             String numericRepresentation = essentialPrimeImplicantsContainingOne.get(i).split(" ",2)[0];
  488.             System.out.println(binaryRepresentation + "\t" + "(" + numericRepresentation + ")");
  489.         }
  490.         System.out.println();
  491.         System.out.println("Expression:");
  492.         String expression = findExpression(essentialPrimeImplicantsContainingOne);
  493.         if(!expression.equals(""))
  494.             expression+= " + ";
  495.         expression += findExpression(combinationEssential);
  496.         System.out.println(expression);
  497.         System.out.println("---------------");
  498.     }
  499.    
  500.     public static void printCoverageTable() {
  501.         for(int i=0; i<numberOfBits; i++)
  502.             System.out.print(" ");
  503.         System.out.print("\t");
  504.         for(int i=0; i<minterms.size(); i++)
  505.             System.out.print(minterms.get(i) + "\t");
  506.         System.out.println();System.out.println();
  507.         for(int i=0; i<primeImplicants.size(); i++) {
  508.             ArrayList<String> data = new ArrayList<String>();
  509.             for (String retval : primeImplicants.get(i).split(" "))
  510.                 data.add(retval);
  511.             System.out.print(data.get(0) + "\t");
  512.             for(int j=0; j<minterms.size(); j++) {
  513.                 if(data.contains(Integer.toString(minterms.get(j))))
  514.                     System.out.print("X" + "\t");
  515.                 else
  516.                     System.out.print(" " + "\t");
  517.             }
  518.             System.out.println();
  519.         }
  520.     }
  521.  
  522.     private static void readFile () throws IOException {
  523.         FileReader inFile = null;
  524.         try {
  525.             System.out.println("Enter input file location:");
  526.             String location = in.nextLine();
  527.             inFile = new FileReader(location);
  528.             int c;
  529.             while((c = inFile.read())!= -1) {
  530.                 if(c==',' || c==' ')
  531.                     continue;
  532.                 else if(c=='\n' || c=='\r')
  533.                     break;
  534.                 minterms.add(c - (int)'0');
  535.             }
  536.             while((c = inFile.read())!=-1) {
  537.                 if(c==',' || c==' ' || c=='\n')
  538.                     continue;
  539.                 dontcares.add(c - (int)'0');
  540.             }
  541.         } finally {
  542.             if(inFile != null)
  543.                 inFile.close();
  544.        
  545.         }      
  546.     }
  547.    
  548.  
  549.     /**
  550.      * The main method.
  551.      *
  552.      * @param args the arguments
  553.      * @throws IOException
  554.      */
  555.     public static void main(String[] args) throws IOException {
  556.         System.out.println("Enter number of variables:");
  557.         numberOfBits = in.nextInt();
  558.         in.nextLine();
  559.         System.out.println("Read from a file? (y/n)");
  560.         String r = in.nextLine();
  561.         if(r.equals("y"))
  562.             readFile();
  563.         else {
  564.        
  565.             System.out.println("Enter minterms in decimal <-1 to finish>:");
  566.             while (true) {
  567.                 int input;
  568.                 input = in.nextInt();
  569.                 if (input == -1)
  570.                     break;
  571.                 minterms.add(input);
  572.             }
  573.             System.out.println("Enter don'tcares in decimal <-1 to finish>:");
  574.             while (true) {
  575.                 int input;
  576.                 input = in.nextInt();
  577.                 if (input == -1)
  578.                     break;
  579.                 dontcares.add(input);
  580.             }
  581.         }
  582.        
  583.         System.out.println("Write to a file? (y/n)");
  584.         String w = in.nextLine();
  585.             if(w.equals("y"))
  586.                 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt")),true));
  587.            
  588.         fillArrayList();
  589.         decimalToBinary();
  590.         doComparisons();
  591.        
  592.         System.out.println("________________________________________________________________");
  593.         System.out.println();
  594.         System.out.println("Grouping:");
  595.         System.out.println();
  596.         for (int k = 0; k < comparisons.size(); k++) {
  597.             boolean comparisonHasElements = false;
  598.  
  599.             for (int i = 0; i < comparisons.get(k).size(); i++) {
  600.                 if (comparisons.get(k).get(i).isEmpty())
  601.                     continue;
  602.                 for (int j = 0; j < comparisons.get(k).get(i).size(); j++) {
  603.                     String binaryRepresentation = comparisons.get(k).get(i).get(j).split(" ", 2)[0];
  604.                     String numericRepresentation = comparisons.get(k).get(i).get(j).split(" ", 2)[1];
  605.                     System.out.println(binaryRepresentation + "\t" + "(" + numericRepresentation + ")");
  606.                 }
  607.                 comparisonHasElements = true;
  608.                 System.out.println("-----------------");
  609.             }
  610.             if (comparisonHasElements) {
  611.                 System.out.println("_________________" );
  612.                 System.out.println();
  613.             }
  614.         }
  615.         if (minterms.isEmpty()==true)
  616.         {
  617.             System.out.println("Expression = 0");
  618.             System.exit(0);
  619.         }
  620.        
  621.         System.out.println("Prime Implicants:");
  622.         System.out.println();
  623.         for (int i = 0; i < primeImplicants.size(); i++) {
  624.             String binaryRepresentation = primeImplicants.get(i).split(" ", 2)[0];
  625.             String numericRepresentation =  primeImplicants.get(i).split(" ", 2)[1];
  626.             System.out.println(binaryRepresentation + "\t" + "(" + numericRepresentation + ")");
  627.         }
  628.         System.out.println();
  629.         System.out.println("Expression:");
  630.         System.out.println(findExpression(primeImplicants));
  631.         System.out.println("_________________");
  632.         System.out.println();
  633.         System.out.println("Coverage Table:");
  634.         System.out.println();
  635.         printCoverageTable();
  636.         System.out.println("_________________");
  637.         System.out.println();
  638.         System.out.println("Essential Prime Implicants:");
  639.         System.out.println();
  640.         countAppearances();
  641.         if (containsOne()) {
  642.             getReducedEssentialImplicants();
  643.             if (!needsCombinations()) {
  644.                 for (int i = 1; i <= primeImplicants.size() && !foundCombination; i++)
  645.                     generateCombinations(1, 0, i);
  646.             }
  647.             else {
  648.                 for(int i=0; i<essentialPrimeImplicantsContainingOne.size(); i++) {
  649.                     String binaryRepresentation = essentialPrimeImplicantsContainingOne.get(i).split(" ",2)[0];
  650.                     String numericRepresentation = essentialPrimeImplicantsContainingOne.get(i).split(" ",2)[0];
  651.                     System.out.println(binaryRepresentation + "\t" + "(" + numericRepresentation + ")");
  652.                 }
  653.                 System.out.println();
  654.                 System.out.println("Expression:");
  655.                 System.out.println(findExpression(essentialPrimeImplicantsContainingOne));
  656.                
  657.             }
  658.         }
  659.         else {
  660.             for (int i = 1; i <= primeImplicants.size() && !foundCombination; i++)
  661.                 generateCombinations(1, 0, i);
  662.         }
  663.  
  664.     }
  665.  
  666. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement