Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. package v3;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.Scanner;
  6.  
  7. public class hw2 {
  8. private static int inLength, numIn, numOut, numTrues;
  9. private static int[] onesCount, groups;
  10. private static int[][] ins, outs, holdOnes, ansOnes;
  11. private static boolean reduced;
  12. private static boolean[][] trues;
  13. public static void main(String[] args) {
  14. try {
  15. Scanner s = new Scanner(new File("input.txt"));
  16. numIn = s.nextInt();
  17. if(numIn > 100) {
  18. System.out.println("Sorry, that input amount is too high, The max is: 100");
  19. System.exit(0);
  20. }
  21. inLength = (int) Math.pow(2, numIn);
  22. numOut = s.nextInt();
  23. if(numOut > 26) {
  24. System.out.println("Sorry, that output amount is too high, The max is: 226");
  25. System.exit(0);
  26. }
  27. ins = new int[numIn][inLength];
  28. outs = new int[numOut][inLength];
  29. fillIn();
  30. fillOut(s);
  31. for(int i = 0; i < numOut; i++) {
  32. pullOnes(i);
  33. groupIt(holdOnes, numIn);
  34. System.out.println(((char)('A'+i))+" = "+printResult(i, ansOnes));
  35. }
  36. //printData();
  37. s.close();
  38. } catch (FileNotFoundException e) {
  39. System.out.println("Sorry, this file could not be found");
  40. }
  41. }
  42.  
  43. private static void groupIt(int[][] hO, int nums) {
  44. int totalCount = 0;
  45. for(int i = 0; i < hO.length; i++) {
  46. int counter = 0;
  47. for(int j = 0; j < numIn; j++) {
  48. if(hO[i][j] == 1) {
  49. counter++;
  50. totalCount++;
  51. }
  52. }
  53. }
  54. ansOnes = new int[totalCount][numIn];
  55. int truePos = 0;
  56. int whatGroup = 0;
  57.  
  58. for(int i = 0; i < hO.length; i++) {
  59. for(int j = i; j < hO.length; j++) {
  60. int counter = 0;
  61. for(int k = 0; k < numIn; k++) {
  62. if(hO[i][k] != hO[j][k]) {
  63. counter++;
  64. }
  65. }
  66. if(counter == 1) {
  67. ansOnes = mergeIt(hO, ansOnes, i, j, truePos);
  68. truePos++;
  69. }
  70. }
  71. }
  72. if(truePos < ansOnes.length) {
  73. for(int i = truePos; i < ansOnes.length; i++) {
  74. for(int j = truePos; j < numIn; j++) {
  75. ansOnes[i][j] = 3;
  76. }
  77. }
  78. }
  79. nums--;
  80. if(nums > 2) {
  81. groupIt(ansOnes, nums);
  82. }
  83. }
  84.  
  85. private static int[][] mergeIt(int[][] hO, int[][] h1, int p1, int p2, int pos) {
  86. for(int i = 0; i < numIn; i++) {
  87. if(hO[p1][i] == hO[p2][i]) {
  88. h1[pos][i] = hO[p1][i];
  89. } else {
  90. h1[pos][i] = 2;
  91. }
  92. }
  93. return h1;
  94. }
  95.  
  96. private static void pullOnes(int pos) {
  97. holdOnes = new int[onesCount[pos]][numIn];
  98. int counter = 0;
  99. for(int i = 0; i < inLength; i++) {
  100. if(outs[pos][i] == 1) {
  101. for(int j = 0; j < numIn; j++) {
  102. holdOnes[counter][j] = ins[j][i];
  103. }
  104. counter++;
  105. }
  106. }
  107. }
  108.  
  109. private static String printResult(int pos, int[][] arr) {
  110. String result = "";
  111. boolean last = false;
  112. for(int i = 0; i < arr.length; i++) {
  113. result += "(";
  114. boolean first = true;
  115. for (int j = 0; j < arr[i].length; j++) {
  116. if (arr[i][j] == 1) {
  117. if (j < 10) {
  118. if (first) {
  119. result += "IN0" + j;
  120. first = false;
  121. } else {
  122. result += " * IN0" + j;
  123. }
  124. } else {
  125. if (first) {
  126. result += "IN" + j;
  127. first = false;
  128. } else {
  129. result += "* IN" + j;
  130. }
  131. }
  132. } else if (arr[i][j] == 0) {
  133. if (j < 10) {
  134. if (first) {
  135. result += "IN0" + j + "'";
  136. first = false;
  137. } else {
  138. result += " * IN0" + j + "'";
  139. }
  140. } else {
  141. if (first) {
  142. result += "IN" + j + "'";
  143. first = false;
  144. } else {
  145. result += "* IN" + j + "'";
  146. }
  147. }
  148. }
  149. if (i == arr.length - 1 && j == arr[i].length - 1) {
  150. last = true;
  151. }
  152. }
  153. result += ")";
  154. if (!last) {
  155. result += " + ";
  156. }
  157. }
  158. return result;
  159. }
  160. /*
  161. private static void trueReduce(int length) {
  162. reduced = false;
  163. for(int i = 0; i < length; i++) {
  164. for(int j = 0; j < length; j++) {
  165. int c1 = 0, c2 = 0;
  166. if(trues[i][j]) {
  167. for(int k = 0; k < length; k++) {
  168. if(trues[i][j] == trues[i][k] && j != k) {
  169. c1 = 1;
  170. }
  171. }
  172. for(int k = 0; k < length; k++) {
  173. if(trues[i][j] == trues[i][k] && i != k) {
  174. c2 = 1;
  175. }
  176. }
  177. if(c1 + c2 == 2) {
  178. reduced = true;
  179. trues[i][j] = false;
  180. numTrues--;
  181. }
  182. }
  183. }
  184. }
  185. }
  186.  
  187. private static void duplicateReduce(int length) {
  188. System.out.println(numTrues + " " + numIn);
  189. ansOnes = new int[numTrues][numIn];
  190. int count = 0;
  191. for(int i = 0; i < length; i++) {
  192. for(int j = 0; j < length; j++) {
  193. if(trues[i][j]) {
  194. for(int k = 0; k < numIn; k++) {
  195. if (holdOnes[i][k] != holdOnes[j][k]) {
  196. ansOnes[count][k] = 2;
  197. } else {
  198. ansOnes[count][k] = holdOnes[i][k];
  199. }
  200. System.out.print(ansOnes[count][k] + " ");
  201. }
  202. count++;
  203. System.out.println();
  204. }
  205. }
  206. }
  207. }*/
  208.  
  209. private static void placeTrue(int length) {
  210. trues = new boolean[20][30];
  211. for(int i = 0; i < length; i++) {
  212. for(int j = 0; j < length; j++) {
  213. trues[i][j] = false;
  214. }
  215. }
  216. numTrues = 0;
  217. for(int i = 0; i < length; i++) {
  218. for(int j = i; j < length; j++) {
  219. int numFalse = 0;
  220. for(int k = 0; k < numIn; k++) {
  221. if(holdOnes[i][k] != holdOnes[j][k]) {
  222. numFalse++;
  223. }
  224. }
  225. if(numFalse == 1) {
  226. trues[i][j] = true;
  227. numTrues++;
  228. }
  229. }
  230. }
  231.  
  232. }
  233.  
  234. private static void fillOut(Scanner s) {
  235. onesCount = new int[numOut];
  236. for (int i = 0; i < numOut; i++) {
  237. onesCount[i] = 0;
  238. }
  239. for (int j = 0; j < inLength; j++) {
  240. for (int i = 0; i < numOut; i++) {
  241. int num = s.nextInt();
  242. if (num == 1) {
  243. onesCount[i]++;
  244. }
  245. outs[i][j] = num;
  246. }
  247. }
  248. }
  249.  
  250. private static void fillIn() {
  251. for(int i = 0; i < numIn; i++) {
  252. int fill = (int)(inLength/Math.pow(2, i+1));
  253. boolean tf = true;
  254. for(int j = 0; j < inLength; j++) {
  255. if(j%fill == 0 && j > 0) {
  256. tf = !tf;
  257. }
  258. if(tf) {
  259. ins[i][j] = 0;
  260. } else {
  261. ins[i][j] = 1;
  262. }
  263. }
  264. }
  265. }
  266.  
  267. private static void printData() {
  268. System.out.println();
  269. System.out.println("Truth Table:");
  270. for(int j = 0; j < inLength; j++) {
  271. for(int i = 0; i < numIn; i++) {
  272. System.out.print(ins[i][j] + " ");
  273. }
  274. for(int i = 0; i < numOut; i++) {
  275. System.out.print(outs[i][j] + " ");
  276. }
  277. System.out.println();
  278. }
  279. }
  280.  
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement