Complement a multiplication algorithm Booth Java simple realization

This program is easy to achieve a complement multiplication, if there is an error code, you can point out, I will from time to time to modify.

Simple outline:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Code:

package self_make;

import java.util.Scanner;

// a complement multiplication (Booth algorithm: Booth algorithm)
public class booth_test {
static Scanner scanofX;
static Scanner scanofY;

static void main public (String [] args) {
// input area

// Type X value
the while (to true) {
of System.out.print ( "Please enter a given point of less than 7 X:");
scanofX = new new Scanner (the System.in);
String scanofX.nextLine X = ();
char X [] = x.toCharArray (); // string into character array
char Xn [] = x.toCharArray () ;

// check the entered value meets the
IF (Check (X-) == to false) {
Continue;
}
// Type Y value
System.out.print ( "Please enter a given point of less than 7 Y:");
scanofY new new = Scanner (the System.in);
String scanofY.nextLine Y = ();

and char [] = y.toCharArray ();
if (check (Y) == false) {
continue;
}

// initialization area
int ycount = 0; // y value for intercepting
int ccount = 0; // multiply operation for recording the number of shift
char getfA [] = new char [ 5];

// true value is negative
// [the -X-] Complement
IF (X-[0] == '-') {
Xn [0] = '0';
for (int I =. 1; I <= x.length -. 1 ; I ++) {
IF (Xn [I] == '. 1' & Xn [I] =) {! '.'
Xn [I] = '. 1';
} the else IF (Xn [I] == '0' & Xn [I] =) {! '.'
xn [I] = '0';
} the else {
xn [I] = '.';
}
}
// [X-] complement
X-[0] = '. 1';
int countx = 0; // record the first digit from right to left index value 1
for (int I = x.length - 1; I> 0; i--) {
IF (X-[I] == '1 ') {
X-[I] ='. 1 ';
countx = I;
BREAK;
}
}
// subscript left bitwise
for (int I =. 1; I <countx; I ++) {
IF (X-[I] = = '1' && X[i] != '.') {
X[i] = '0';
IF the else} (X-[I] == '0' X-&& [I] =! '.') {
X-[I] = '. 1';
} the else {
X-[I] = '.';
}
}
// the right side of the same subscript
for (int I = countx; I <= x.length -. 1; I ++) {
IF (X-[I] == '. 1') {
X-[I] = '. 1';
} the else IF ( X-[I] == '0') {
X-[I] = '0';
}
}
of System.out.print ( "[X-] complement is:");
for (int I = 0; I <x.length ; I ++) {
of System.out.print (X-[I]);
}
of System.out.print ( "\ T [the -X-] up as:");
for (int I = 0; I <Xn.length; I ++ ) {
of System.out.print (Xn [I]);
}
}
// if it is really is a positive number, n is the number itself complement
the else IF (X-[0] == '+') {
X-[0] = '0 ';
of System.out.print (' [X-] Complement is: ");
for (int I = 0; I <x.length; I ++) {
of System.out.print (X [I]);
}
// X true when the multiplier is positive, the operation of Xn
char Xn1 [] = char new new [Xn.length +. 1];
for (int I = Xn.length - 2; I> = 0; i--) {
Xn1 [I +. 1] Xn = [I];
}
Xn [0] = ' 1 ';
int countXn = 0; // record the first digit from right to left index value 1
for (int I = Xn.length - 1; I> 0; i--) {
IF (Xn [I] == '. 1' & Xn [I] =)! '.' {
Xn [I] = '. 1';
countXn = I;
BREAK;
}
}
// subscript bitwise left
for (int i = 1; I <countXn; I ++) {
IF (Xn [I] == '. 1' & Xn [I] =) {! '.'
Xn [I] = '0';
} the else IF (Xn [I] == ' 0 '& Xn [i]! ='. ') {
Xn[i] = '1';
} else {
Xn[i] = '.';
}
}
// 下标右边不变
for (int i = countXn; i <= Xn.length - 1; i++) {
if (Xn[i] == '1' & Xn[i] != '.') {
Xn[i] = '1';
} else if (Xn[i] == '0' & Xn[i] != '.') {
Xn[i] = '0';
}
}
System.out.print("\t[-X]补为:");
for (int i = 0; i < Xn.length; i++) {
System.out.print(Xn[i]);
}
} else if (X[0] == '0') {
String xtemp = String.valueOf(X);
xtemp = "0" + xtemp;
X = xtemp.toCharArray();
System.out.print("[X]补为:");
for (int i = 0; i < X.length; i++) {
System.out.print(X[i]);
}
// Xn
Xn = xtemp.toCharArray();
Xn[0] = '1';
int countXn = 0; // record the first digit from right to left index value of 1
for (int I = Xn.length - 1; I> 0; i--) {
IF (Xn [I] == ' . 1 '& Xn [I] =!' ') {.
Xn [I] ='. 1 ';
countXn = I;
BREAK;
}
}
// subscript bitwise left
for (int i = 1; i <countXn ; I ++) {
IF (Xn [I] == '. 1' & Xn [I] =) {! '.'
Xn [I] = '0';
} the else IF (Xn [I] == '0' & ! '.' xn [I] =) {
xn [I] = '. 1';
} the else {
xn [I] = '.';
}
}
// at the right side of the same subscript
for (int i = countXn; i < Xn.length = -. 1; I ++) {
IF (Xn [I] == '. 1' & Xn [I] =) {! '.'
Xn [I] = '. 1';
} the else IF (Xn [I] == '0'& Xn[i] != '.') {
Xn[i] = '0';
}
}
System.out.print("\t[-X]补为:");
for (int i = 0; i < Xn.length; i++) {
System.out.print(Xn[i]);
}
}
System.out.println();





























Y[i] = '.';
}
}
// 下标右边不变
for (int i = county; i <= ycount; i++) {
if (Y[i] == '1') {
Y[i] = '1';
} else if (Y[i] == '0') {
Y[i] = '0';
}
}
System.out.print("[Y]补为:");
for (int i = 0; i < Y.length; i++) {
System.out.print(Y[i]);
}
}
// 若真值为正数,正数补码为本身
else if (Y[0] == '0') {
ycount = Y.length;
System.out.print("[Y]补为:");
for (int i = 0; i < Y.length; i++) {
System.out.print(Y[i]);
}
} else if (Y[0] == '+') {
ycount = Y.length;
for (int i = 0; i < Y.length - 2; i++) {
Y[i] = Y[i + 1];
}
System.out.print("[Y]补为:");
for (int i = 0; i < Y.length; i++) {
System.out.print(Y[i]);
}

}

// calculation section
char A [] = { '0 ', '0',, '0', '0', '0', '0' '.'}; // number of accumulated
char C [] = new char [ycount + 1]; // number for CnCn + 1
for (int I = 0; I <YCOUNT; I ++) {
C [I] = the Y [I];
}
C [c.length - 1] = ' 0 ';
of System.out.print ( "\ tC is:");
for (int I = 0; I <c.length; I ++) {
of System.out.print (C [I]);
}
// determination, computing, accumulated
for (int I = c.length - 2; I> 0; i--) {
IF (C [I] == '0' & C [I +. 1] == '0' | C [I ] == '1' & C [ i + 1] == '1') {// CnCn + 1 where is 00,11
// right
getfA [ccount] = A [A.length -. 1];
for ( A.length J = int -. 1; J> 0; J,) {
A [J] = A [J -. 1];
}
A[3] = A[0];
A[2] = '.';
ccount++;
} else if (C[i] == '0' & C[i + 1] == '1') { // CnCn+1为01情况
sum(A, X);
getfA[ccount] = A[A.length - 1];
// 右移
for (int j = A.length - 1; j > 0; j--) {
A[j] = A[j - 1];
}
A[3] = A[0];
A[2] = '.';
ccount++;
} else if (C[i] == '1' & C[i + 1] == '0') { // CnCn+1为10情况
sum(A, Xn);
getfA[ccount] = A[A.length - 1];
// 右移
for (int j = A.length - 1; j > 0; j--) {
A[j] = A[j - 1];
}
A[3] = A[0];
A[2] = '.';
ccount++;
} else if (C[i] == '.') {
if (C[i - 1] == '0' & C[i + 1] == '1') {
sum(A, X);
ccount++;
break;
} else if (C[i - 1] == '1' & C[i + 1] == '0') {
sum(A, Xn);
ccount++;
break;
} else {
break;
}
}
}
System.out.print("\n结果为:");
for (int i = 0; i < A.length; i++) {
System.out.print(A[i]);
}
for (int i = 0; i < getfA.length; i++) {
System.out.print(getfA[i]);
}
System.out.print("\n");
}
}

//累加函数,二进制加法
static public char[] sum(char a[], char b[]) {
char[] result = a;
int c = 0;//进位C
for (int i = b.length - 1; i >= 0; i--) {
if ('.' == (b[i])) {
result[i] = '.';
} else if (c == 0) {
if (a[i] == '1' & b[i] == '1') {
result[i] = '0';
c = 1;
} else if (a[i] == '1' & b[i] == '0') {
result[i] = '1';
c = 0;
} else if (a[i] == '0' & b[i] == '1') {
result[i] = '1';
c = 0;
} else if (a[i] == '0' & b[i] == '0') {
result[i] = '0';
c = 0;
}
} else if (c == 1) {
if (a[i] == '1' & b[i] == '1') {
result[i] = '1';
c = 1;
} else if (a[i] == '1' & b[i] == '0') {
result[i] = '0';
c = 1;
} else if (a[i] == '0' & b[i] == '1') {
result[i] = '0';
c = 1;
} else if (a[i] == '0' & b[i] == '0') {
result[i] = '1';
c = 0;
}
}
}
return result;
}

// check function, to see whether the input character format
static public Boolean Check (char [] A) {
Boolean = In Flag to true; // record the character whether valid
int countc = 0; // record '+' '-' No. The number
int countd = 0; '.' // record
IF (a.length> =. 8) {
of System.out.print ( "format error, please re-enter \ n-");
In Flag = to false;
}
IF (A [ !!! 0] = '0' & A [0] = '+' & A [0] = '-') {
of System.out.print ( "format error, please re-enter \ n-");
In Flag to false = ;
}
IF (A [0] == '0') {
IF (A [. 1] =! '.') {
of System.out.print ( "format error, please re-enter \ n-");
In Flag to false =;
}
for (int I = 2; I <a.length; I ++) {
IF (!! A [I] = '0' & A [I] = '. 1') {
the System.out.print ( "format error, please re-enter \ n-");
In Flag to false =;
BREAK;
}
}
}
IF (A [0] == '+' | A [0] == '-') {
(! '.' A [2] =) {IF
of System.out.print ( "format error, please re-enter \ n ");
In Flag = to false;
}
! IF (A [. 1] = '0' & A [. 1] = '. 1') {!
of System.out.print (" format error, please re-enter \ n ") ;
In Flag = to false;
}
for (int I =. 3; I <a.length; I ++) {
(!! A [I] = '0' & A [I] = '. 1') {IF
of System.out.print ( "format error, please re-enter \ n-");
In Flag to false =;
BREAK;
}
}
}
for (int I = 0; I <a.length; I ++) {
IF (A [I] == '+' & A [I] == '-') {
countc ++;
}
}
IF (countc>. 1) {
of System.out.print ( "Format error, please re-enter \ n-");
In Flag = to false;
}
for (int I = 0; I <a.length; I ++) {
IF ( '.' A [I] ==) {
countd ++;
}
}
IF (countd>. 1) {
of System.out.print ( "malformed, please re-enter \ n-");
In Flag = to false;
}
return In Flag;
}
}

 

Guess you like

Origin www.cnblogs.com/xiaohu12138/p/11955619.html