Você está na página 1de 4

import java.util.Scanner; //import java.util.

StringTokenizer; public class bin2dec { public static String[] getInputVals(String inStr) //put each comma separated value in an array { // StringTokenizer strTokenizer = new StringTokenizer(inStr,","); String [] inVals = inStr.split(","); return inVals; } public static boolean checkLength(String[] inVals) //return true if all values in string array are of same length { int len; boolean bool = true; int testLen; len=inVals[0].length(); for (int ndx = 1; ndx < inVals.length; ndx++) { testLen = inVals[ndx].length(); if (len != testLen) { bool = false; return bool; } } return bool; } public static char[][] putNarray(String [] inVals, char[][] storArray) //put binary value in 2D array, one bit per cell { // int bit; for (int ndx = 0; ndx < inVals.length; ndx++) { // for (int row=0; row < storArray.length; row++) // { for (int col=0; col < storArray[ndx].length; col++) { // // ol); // l)); dx].charAt(col)); //System.out.println(b); // } } } for (int ndx = 0; ndx < binVal.length(); ndx++) { storArray[ndx][col]=inVals[ndx].charAt(c System.out.println(inVals[ndx].charAt(co //boolean b = Character.isDigit(inVals[n

return storArray; } public static void print2dArray(char [][] storArray) { //print values of 2d array for (int row=0; row < storArray.length; row++) { for (int col=0; col < storArray[row].length; col++) { System.out.println(storArray[row][col]); } } } public static void print2dArray(int [][] storArray) { //print values of 2d array for (int row=0; row < storArray.length; row++) { for (int col=0; col < storArray[row].length; col++) { System.out.println(storArray[row][col]); } } } public static void print1dArray(char [] inArray) { for (int ndx=0; ndx < inArray.length; ndx++) { System.out.println(inArray[ndx]); } } public static char[] getSubArray(char[] inArray) //returns a subarray of inArray with the left most element removed //there is an easy way to make this unnecessary { int newSize = (inArray.length-1); char[] subArray = new char[newSize]; int subArrayNdx = 0; for (int ndx = 1; ndx < inArray.length; ndx++) { subArray[subArrayNdx] = inArray[ndx]; subArrayNdx++; } return subArray; } public static int[] copyArray(int[] inArray, int[] outArray) // copies one array to another { // int[] newArray = new int[inArray.length];

for (int ndx = 0; ndx < inArray.length; ndx++) { outArray[ndx] = inArray[ndx]; } return outArray; } public static int convert(char [] inArray) { if (inArray.length ==1) { if (inArray[0]=='0') return 0; if (inArray[0]=='1') return 1; } if (inArray.length > 1) { char [] subArray; subArray = getSubArray(inArray); if (inArray[0]=='0') return 0 * (int)Math.pow(2,(inArray.length-1)) + convert(subArray); if (inArray[0]=='1') return 1 * (int)Math.pow(2,(inArray.length-1)) + convert(subArray); } return -1; } //-1 means there is a problem

public static void main(String[] args) { String inList; String [] inVals; boolean bool; //Read user input Scanner keyboard = new Scanner(System.in); System.out.print("Enter a list of comma-separated binary values: "); inList = keyboard.nextLine(); //Print input list // System.out.println("Input: " + inList); //Store each binary value in an array inVals = getInputVals(inList); //Check length of input values bool = checkLength(inVals); if (bool == false) { System.out.println("Please re-run the program with strin gs of equal length"); } // storage array dimensions-based on input int ROWS = inVals.length; // number of input values int COLS = inVals[0].length(); // length of each value

// 2d array to store input chars char[][] storArray = new char[ROWS][COLS]; //Get each input value individually // // // // { //Print each input value System.out.println("Input: " + inVals[ndx]); //Get length of first input value len = inVals[ndx]

//stores the input values in a 2d array storArray=putNarray(inVals, storArray); int[][] outArray = new int[2][COLS]; //output array, only needs 2 rows because it only stores row and column (2) sets of conversions int[] rowTemp = new int[ROWS]; for (int row=0; row < storArray.length; row++) { int out= convert(storArray[row]); rowTemp[row] = out; // char[] colTemp = new char[COLS]; int[] colOutTemp = new int[COLS]; for (int col=0; col < storArray[row].length; col++) { colTemp[col]=storArray[col][row]; //puts each co } int out2= convert(colTemp); outArray[1][row] = out2; } copyArray(rowTemp, outArray[0]); /* for (int col=0; col < storArray[row].length; col++) { System.out.println(storArray[row][col]); } */ // // // print2dArray(storArray); System.out.println(out); print1dArray(SA);

lumn bit into a 1d array

//print elements of 2D array print2dArray(outArray);

} }

Você também pode gostar