Algoritmo de Booth Java

March 7, 2017 | Author: Alan Josue Escobar | Category: N/A
Share Embed Donate


Short Description

Download Algoritmo de Booth Java...

Description

import java.util.Scanner;

public class Booth {

static Scanner sc = new Scanner(System.in); static String M, m, m1, Ax, ac, mm; static int ciclo = 0;

public static void main(String[] args) { System.out.println("Dame m"); m = sc.nextLine(); System.out.println("Dame M"); M = sc.nextLine(); m1 = "0"; Ax = "0000"; mm = m; System.out.println("i M ac Ax m m1"); System.out.println(0 + " " + M + " " + ac + " " + "VI" + " " + m + " " + m1 + " Valores iniciales"); int i = 0; desplaza(); System.out.println(i + 1 + " " + M + " " + ac + " " + Ax + " " + m + " " + m1); i++;

while (ciclo != 5) { desplaza(); if (mm.equals(m)) {

if (mm.equals(m) && M.equals("0001")) { System.out.println(i + 1 + " " + M + " " + ac + " " + Ax + " " + m + " " + m1); break; } else { break; } } else { System.out.println(i + 1 + " " + M + " " + ac + " " + Ax + " " + m + " " + m1); } i++; } }

public static void desplaza() { String cadena = "", newAx = ""; ciclo++; if (ac == null) { m1 = "0"; } else { m1 = m.charAt(3) + ""; } if (m.charAt(3) == '0' && m1.equals("0") || m.charAt(3) == '1' && m1.equals("1")) { newAx += Ax.charAt(0) + ""; newAx += Ax.charAt(0) + ""; newAx += Ax.charAt(1) + ""; newAx += Ax.charAt(2) + "";

cadena += Ax.charAt(3) + ""; cadena += m.charAt(0) + ""; cadena += m.charAt(1) + ""; cadena += m.charAt(2) + ""; m1 = m.charAt(3) + ""; m = cadena; Ax = newAx; ac = "01"; // ac = "0|1"; } if (m1.equals("1") && m.charAt(3) == '0') { suma(Ax); ac = "01"; } if (m1.equals("0") && m.charAt(3) == '1') { resta(Ax); ac = "10"; } }

public static void suma(String x) { int array[][] = new int[5][5]; for (int i = 0; i < M.length(); i++) { if (x.charAt(i) == '0') { array[0][i + 1] = 0; } else {

array[0][i + 1] = 1; } if (M.charAt(i) == '0') { array[1][i + 1] = 0; } else { array[1][i + 1] = 1; } } sumaP(array); Ax = array[4][1] + "" + array[4][2] + "" + array[4][3] + "" + array[4][4] + ""; }

public static void resta(String x) { int array[][] = new int[5][5]; for (int i = 0; i < M.length(); i++) { if (x.charAt(i) == '0') { array[0][i + 1] = 0; } else { array[0][i + 1] = 1; } if (M.charAt(i) == '0') { array[1][i + 1] = 1; } else { array[1][i + 1] = 0; }

} array[3][array.length - 1] = 1; sumaP(array); Ax = array[4][1] + "" + array[4][2] + "" + array[4][3] + "" + array[4][4] + ""; }

public static void sumaP(int array[][]) { int sum = 0, a = array.length - 1; for (int i = 0; i < array.length; i++) { sum = array[0][a] + array[1][a] + array[2][a] + array[3][a]; if (sum == 3) { array[4][a] = 1; a--; array[2][a] = 1; } if (sum == 2) { array[4][a] = 0; a--; array[2][a] = 1; } if (sum == 1) { array[4][a] = 1; a--; } if (sum == 0) { a--;

} } }

public static void print(int array[][]) { System.out.println("Entrada"); for (int i = 0; i < 2; i++) { for (int j = 1; j < array[0].length; j++) { System.out.print(array[i][j]); } System.out.println(""); } System.out.println("Salida"); for (int j = 1; j < array[0].length; j++) { System.out.print(array[4][j]); } System.out.println(""); } }

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF