补码一位乘法 Booth算法 Java简易实现

本程序为简易实现补码一位乘法,若代码中存在错误,可指出,本人会不定期修改。

简易大纲:

补码一位乘法 Booth算法 Java简易实现

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

代码实现:

package self_make;

import java.util.Scanner;

//补码一位乘法(布斯算法:booth algorithm)
public class booth_test {
static Scanner scanofX;
static Scanner scanofY;

public static void main(String[] args) {
//输入区

// 键入X值
while (true) {
System.out.print("请输入小于7位的定点数X:");
scanofX = new Scanner(System.in);
String x = scanofX.nextLine();
char X[] = x.toCharArray(); // 将字符串变成字符数组
char Xn[] = x.toCharArray();

// 检查输入的值是否符合
if (check(X) == false) {
continue;
}
// 键入Y值
System.out.print("请输入小于7位的定点数Y:");
scanofY = new Scanner(System.in);
String y = scanofY.nextLine();

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

//初始化区
int ycount = 0; // 用于截取y值
int ccount = 0; // 用于记录移位乘法操作次数
char getfA[] = new char[5];

// 真值为负数
// [-X]补
if (X[0] == '-') {
Xn[0] = '0';
for (int i = 1; i <= X.length - 1; i++) {
if (Xn[i] == '1' & Xn[i] != '.') {
Xn[i] = '1';
} else if (Xn[i] == '0' & Xn[i] != '.') {
Xn[i] = '0';
} else {
Xn[i] = '.';
}
}
// [X]补
X[0] = '1';
int countx = 0; // 记录从右至左第一位数值为1的下标
for (int i = X.length - 1; i > 0; i--) {
if (X[i] == '1') {
X[i] = '1';
countx = i;
break;
}
}
// 下标左边按位取反
for (int i = 1; i < countx; i++) {
if (X[i] == '1' && X[i] != '.') {
X[i] = '0';
} else if (X[i] == '0' && X[i] != '.') {
X[i] = '1';
} else {
X[i] = '.';
}
}
// 下标右边不变
for (int i = countx; i <= X.length - 1; i++) {
if (X[i] == '1') {
X[i] = '1';
} else if (X[i] == '0') {
X[i] = '0';
}
}
System.out.print("[X]补为:");
for (int i = 0; i < X.length; i++) {
System.out.print(X[i]);
}
System.out.print("\t[-X]补为:");
for (int i = 0; i < Xn.length; i++) {
System.out.print(Xn[i]);
}
}
// 若真值为正数,正数补码为本身
else if (X[0] == '+') {
X[0] = '0';
System.out.print("[X]补为:");
for (int i = 0; i < X.length; i++) {
System.out.print(X[i]);
}
// 当乘数X真值为正时,对Xn进行操作
char Xn1[] = new char[Xn.length + 1];
for (int i = Xn.length - 2; i >= 0; i--) {
Xn1[i + 1] = Xn[i];
}
Xn[0] = '1';
int countXn = 0; // 记录从右至左第一位数值为1的下标
for (int i = Xn.length - 1; i > 0; i--) {
if (Xn[i] == '1' & Xn[i] != '.') {
Xn[i] = '1';
countXn = i;
break;
}
}
// 下标左边按位取反
for (int i = 1; i < countXn; i++) {
if (Xn[i] == '1' & Xn[i] != '.') {
Xn[i] = '0';
} 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; // 记录从右至左第一位数值为1的下标
for (int i = Xn.length - 1; i > 0; i--) {
if (Xn[i] == '1' & Xn[i] != '.') {
Xn[i] = '1';
countXn = i;
break;
}
}
// 下标左边按位取反
for (int i = 1; i < countXn; i++) {
if (Xn[i] == '1' & Xn[i] != '.') {
Xn[i] = '0';
} 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]);
}
}
System.out.println();

// Y值部分
if (Y[0] == '-') {
// 左移覆盖
for (int i = 0; i < Y.length - 2; i++) {
Y[i] = Y[i + 1];
}
Y[Y.length - 1] = 0;
Y[0] = '1';
for (int i = 0; i < Y.length; i++) {
if (Y[i] == 0) {
ycount = i;
}
}
int county = 0; // 记录从右至左第一位数值为1的下标
for (int i = ycount; i > 0; i--) {
if (Y[i] == '1') {
Y[i] = '1';
county = i;
break;
}
}
// 下标左边按位取反
for (int i = 1; i < county; i++) {
if (Y[i] == '1' & Y[i] != '.') {
Y[i] = '0';
} else if (Y[i] == '0' & Y[i] != '.') {
Y[i] = '1';
} else {
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]);
}

}

//计算区
char A[] = { '0', '0', '.', '0', '0', '0', '0' }; // 累加数
char C[] = new char[ycount + 1];//用于CnCn+1的数
for (int i = 0; i < ycount; i++) {
C[i] = Y[i];
}
C[C.length - 1] = '0';
System.out.print("\tC为:");
for (int i = 0; i < C.length; i++) {
System.out.print(C[i]);
}
//判断,计算,累加
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为00,11情况
// 右移
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] == '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;
}

//检查函数,查看输入字符是否符合格式
static public boolean check(char[] a) {
boolean flag = true;// 用于记录字符是否合法
int countc = 0; // 记录'+' '-'号个数
int countd = 0; // 记录'.'
if (a.length >= 8) {
System.out.print("格式错误,请重新输入\n");
flag = false;
}
if (a[0] != '0' & a[0] != '+' & a[0] != '-') {
System.out.print("格式错误,请重新输入\n");
flag = false;
}
if (a[0] == '0') {
if (a[1] != '.') {
System.out.print("格式错误,请重新输入\n");
flag = false;
}
for (int i = 2; i < a.length; i++) {
if (a[i] != '0' & a[i] != '1') {
System.out.print("格式错误,请重新输入\n");
flag = false;
break;
}
}
}
if (a[0] == '+' | a[0] == '-') {
if (a[2] != '.') {
System.out.print("格式错误,请重新输入\n");
flag = false;
}
if (a[1] != '0' & a[1] != '1') {
System.out.print("格式错误,请重新输入\n");
flag = false;
}
for (int i = 3; i < a.length; i++) {
if (a[i] != '0' & a[i] != '1') {
System.out.print("格式错误,请重新输入\n");
flag = false;
break;
}
}
}
for (int i = 0; i < a.length; i++) {
if (a[i] == '+' & a[i] == '-') {
countc++;
}
}
if (countc > 1) {
System.out.print("格式错误,请重新输入\n");
flag = false;
}
for (int i = 0; i < a.length; i++) {
if (a[i] == '.') {
countd++;
}
}
if (countd > 1) {
System.out.print("格式错误,请重新输入\n");
flag = false;
}
return flag;
}
}

 

上一篇:【语音去噪】基于matlab GUI LMS+RLS语音去噪【含Matlab源码 528期】


下一篇:【语音去噪】基于matlab GUI LMS+RLS语音去噪【含Matlab源码 528期】