补码一位乘法 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;
}
}