Advertisement
Guest User

Untitled

a guest
May 1st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.lang.Object;
  4. public class Minimization {
  5.  
  6.  
  7. ArrayList <String> minTerms = new ArrayList();
  8. ArrayList<String> commonElements = new ArrayList();
  9.  
  10.  
  11.  
  12.  
  13. int n , index ;
  14. char[] variables;
  15. String[] compVariables;
  16.  
  17. public void setVariables(String minTerm) {
  18. Scanner read = new Scanner(minTerm);
  19. String word = minTerm ;
  20. Scanner scan = new Scanner(word);
  21. int max = 0;
  22. n=0 ;
  23. int k = 0 ;
  24.  
  25.  
  26. while (scan.hasNext()) {
  27. int y = scan.nextInt();
  28. if (y > max)
  29. max = y;
  30.  
  31. }
  32. n = (int) Math.ceil(Math.log(max + 1) / Math.log(2));
  33.  
  34. while (read.hasNext()) {
  35. int x = read.nextInt();
  36. commonElements.add(String.valueOf(x));
  37. String number = convertBinary(x);
  38. minTerms.add(number);
  39.  
  40. }
  41.  
  42. // variables = new char[n];
  43. // compVariables = new String[n];
  44. // for (int i = 0; i < n; i++) {
  45. // variables[i] = (char) ('A' + i);
  46. // compVariables[i] = variables[i] + "'";
  47. // }
  48. }
  49.  
  50. // public int countOccurences(String binary) {
  51. // return (binary.length() - binary.replace("1", "").length());
  52. // }
  53.  
  54. public String convertBinary(int minterm) {
  55. String result = "" ;
  56. for (int j = n - 1; j >= 0; j--) {
  57. if (minterm < Math.pow(2, j))
  58. result += "0";
  59. else {
  60. result += "1";
  61. minterm = (int) (minterm - Math.pow(2, j));
  62. }
  63. }
  64. return result ;
  65. }
  66.  
  67. public void minimization (){
  68. boolean flag = true ;
  69. int Size = minTerms.size();
  70. while (flag) {
  71. for (int i =0 ; i < Size ; i++ ) {
  72. flag = false ;
  73. for (int j =i+1 ; j < Size; j++ ) {
  74. if (ComparingTerms(minTerms.get(i),minTerms.get(j))) {
  75. SetDashes(minTerms.get(i), index);
  76. String s = new String();
  77. s += commonElements.get(i);
  78. s += ",";
  79. s += commonElements.get(j);
  80. commonElements.add(s);
  81. flag = true ;
  82. }
  83. }
  84.  
  85. }
  86. }
  87. for(int l = 0 ; l< Size ; l++) // print minTerms >> just for testing
  88. System.out.println(minTerms.get(l));
  89. for(int l = 0 ; l< commonElements.size() ; l++)
  90. System.out.println(commonElements.get(l));
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. public boolean ComparingTerms (String a , String b) {
  100. index = 0 ;
  101. int k = 0 ;
  102. int count = 0 ;
  103. for (int i = 0 ; i< a.length() ; i++)
  104. {
  105. if (a.charAt(i)!= b.charAt(i)){
  106. count++ ;
  107. k = i ;
  108. }
  109. }
  110. if (count == 1){
  111. index = k ;
  112. return true ;
  113. }
  114. return false ;
  115. }
  116.  
  117. public void SetDashes (String a , int x) {
  118. String s = a ;
  119. StringBuilder myBuilder = new StringBuilder(s);
  120. myBuilder.setCharAt(x, '-');
  121. s = myBuilder.toString();
  122. minTerms.add(s);
  123.  
  124.  
  125. }
  126.  
  127.  
  128. public static void main(String[] args)
  129. {
  130. String min = "1 2 4 5 6 12 13 14";
  131. Minimization l = new Minimization() ;
  132. l.setVariables(min);
  133. l.minimization();
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. }
  142.  
  143.  
  144.  
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement