Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package v3;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class hw2 {
- private static int inLength, numIn, numOut, numTrues;
- private static int[] onesCount, groups;
- private static int[][] ins, outs, holdOnes, ansOnes;
- private static boolean reduced;
- private static boolean[][] trues;
- public static void main(String[] args) {
- try {
- Scanner s = new Scanner(new File("input.txt"));
- numIn = s.nextInt();
- if(numIn > 100) {
- System.out.println("Sorry, that input amount is too high, The max is: 100");
- System.exit(0);
- }
- inLength = (int) Math.pow(2, numIn);
- numOut = s.nextInt();
- if(numOut > 26) {
- System.out.println("Sorry, that output amount is too high, The max is: 226");
- System.exit(0);
- }
- ins = new int[numIn][inLength];
- outs = new int[numOut][inLength];
- fillIn();
- fillOut(s);
- for(int i = 0; i < numOut; i++) {
- pullOnes(i);
- groupIt(holdOnes, numIn);
- System.out.println(((char)('A'+i))+" = "+printResult(i, ansOnes));
- }
- //printData();
- s.close();
- } catch (FileNotFoundException e) {
- System.out.println("Sorry, this file could not be found");
- }
- }
- private static void groupIt(int[][] hO, int nums) {
- int totalCount = 0;
- for(int i = 0; i < hO.length; i++) {
- int counter = 0;
- for(int j = 0; j < numIn; j++) {
- if(hO[i][j] == 1) {
- counter++;
- totalCount++;
- }
- }
- }
- ansOnes = new int[totalCount][numIn];
- int truePos = 0;
- int whatGroup = 0;
- for(int i = 0; i < hO.length; i++) {
- for(int j = i; j < hO.length; j++) {
- int counter = 0;
- for(int k = 0; k < numIn; k++) {
- if(hO[i][k] != hO[j][k]) {
- counter++;
- }
- }
- if(counter == 1) {
- ansOnes = mergeIt(hO, ansOnes, i, j, truePos);
- truePos++;
- }
- }
- }
- if(truePos < ansOnes.length) {
- for(int i = truePos; i < ansOnes.length; i++) {
- for(int j = truePos; j < numIn; j++) {
- ansOnes[i][j] = 3;
- }
- }
- }
- nums--;
- if(nums > 2) {
- groupIt(ansOnes, nums);
- }
- }
- private static int[][] mergeIt(int[][] hO, int[][] h1, int p1, int p2, int pos) {
- for(int i = 0; i < numIn; i++) {
- if(hO[p1][i] == hO[p2][i]) {
- h1[pos][i] = hO[p1][i];
- } else {
- h1[pos][i] = 2;
- }
- }
- return h1;
- }
- private static void pullOnes(int pos) {
- holdOnes = new int[onesCount[pos]][numIn];
- int counter = 0;
- for(int i = 0; i < inLength; i++) {
- if(outs[pos][i] == 1) {
- for(int j = 0; j < numIn; j++) {
- holdOnes[counter][j] = ins[j][i];
- }
- counter++;
- }
- }
- }
- private static String printResult(int pos, int[][] arr) {
- String result = "";
- boolean last = false;
- for(int i = 0; i < arr.length; i++) {
- result += "(";
- boolean first = true;
- for (int j = 0; j < arr[i].length; j++) {
- if (arr[i][j] == 1) {
- if (j < 10) {
- if (first) {
- result += "IN0" + j;
- first = false;
- } else {
- result += " * IN0" + j;
- }
- } else {
- if (first) {
- result += "IN" + j;
- first = false;
- } else {
- result += "* IN" + j;
- }
- }
- } else if (arr[i][j] == 0) {
- if (j < 10) {
- if (first) {
- result += "IN0" + j + "'";
- first = false;
- } else {
- result += " * IN0" + j + "'";
- }
- } else {
- if (first) {
- result += "IN" + j + "'";
- first = false;
- } else {
- result += "* IN" + j + "'";
- }
- }
- }
- if (i == arr.length - 1 && j == arr[i].length - 1) {
- last = true;
- }
- }
- result += ")";
- if (!last) {
- result += " + ";
- }
- }
- return result;
- }
- /*
- private static void trueReduce(int length) {
- reduced = false;
- for(int i = 0; i < length; i++) {
- for(int j = 0; j < length; j++) {
- int c1 = 0, c2 = 0;
- if(trues[i][j]) {
- for(int k = 0; k < length; k++) {
- if(trues[i][j] == trues[i][k] && j != k) {
- c1 = 1;
- }
- }
- for(int k = 0; k < length; k++) {
- if(trues[i][j] == trues[i][k] && i != k) {
- c2 = 1;
- }
- }
- if(c1 + c2 == 2) {
- reduced = true;
- trues[i][j] = false;
- numTrues--;
- }
- }
- }
- }
- }
- private static void duplicateReduce(int length) {
- System.out.println(numTrues + " " + numIn);
- ansOnes = new int[numTrues][numIn];
- int count = 0;
- for(int i = 0; i < length; i++) {
- for(int j = 0; j < length; j++) {
- if(trues[i][j]) {
- for(int k = 0; k < numIn; k++) {
- if (holdOnes[i][k] != holdOnes[j][k]) {
- ansOnes[count][k] = 2;
- } else {
- ansOnes[count][k] = holdOnes[i][k];
- }
- System.out.print(ansOnes[count][k] + " ");
- }
- count++;
- System.out.println();
- }
- }
- }
- }*/
- private static void placeTrue(int length) {
- trues = new boolean[20][30];
- for(int i = 0; i < length; i++) {
- for(int j = 0; j < length; j++) {
- trues[i][j] = false;
- }
- }
- numTrues = 0;
- for(int i = 0; i < length; i++) {
- for(int j = i; j < length; j++) {
- int numFalse = 0;
- for(int k = 0; k < numIn; k++) {
- if(holdOnes[i][k] != holdOnes[j][k]) {
- numFalse++;
- }
- }
- if(numFalse == 1) {
- trues[i][j] = true;
- numTrues++;
- }
- }
- }
- }
- private static void fillOut(Scanner s) {
- onesCount = new int[numOut];
- for (int i = 0; i < numOut; i++) {
- onesCount[i] = 0;
- }
- for (int j = 0; j < inLength; j++) {
- for (int i = 0; i < numOut; i++) {
- int num = s.nextInt();
- if (num == 1) {
- onesCount[i]++;
- }
- outs[i][j] = num;
- }
- }
- }
- private static void fillIn() {
- for(int i = 0; i < numIn; i++) {
- int fill = (int)(inLength/Math.pow(2, i+1));
- boolean tf = true;
- for(int j = 0; j < inLength; j++) {
- if(j%fill == 0 && j > 0) {
- tf = !tf;
- }
- if(tf) {
- ins[i][j] = 0;
- } else {
- ins[i][j] = 1;
- }
- }
- }
- }
- private static void printData() {
- System.out.println();
- System.out.println("Truth Table:");
- for(int j = 0; j < inLength; j++) {
- for(int i = 0; i < numIn; i++) {
- System.out.print(ins[i][j] + " ");
- }
- for(int i = 0; i < numOut; i++) {
- System.out.print(outs[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement