Java BCD乘法中的错误
我的方法是取两个BCD,它们是由驱动程序构造的INT,然后将它们相乘:Java BCD乘法中的错误,java,bcd,Java,Bcd,我的方法是取两个BCD,它们是由驱动程序构造的INT,然后将它们相乘: public BCD multiplyBCDs(BCD other){ //to multiply two BCDs together BCD basebcd = new BCD(digit); BCD otherbasebcd = other; int base = 0; for(int y = 0; y<basebcd.numberOfDigits();y++){ b
public BCD multiplyBCDs(BCD other){ //to multiply two BCDs together
BCD basebcd = new BCD(digit);
BCD otherbasebcd = other;
int base = 0;
for(int y = 0; y<basebcd.numberOfDigits();y++){
base += digit[y];
int g = y;
while(g < basebcd.numberOfDigits()){
digit[g]*=10;
g++;
}
}
int baser = 0;
if(otherbasebcd.numberOfDigits() >= basebcd.numberOfDigits()){
for(int v = 0; v < otherbasebcd.numberOfDigits(); v++){
baser += base*otherbasebcd.nthDigit(v);
int j = v;
while(j < other.numberOfDigits()){
byten(otherbasebcd.nthDigit(j));
j++;
}
}
}
else{
for(int v = 0; v < basebcd.numberOfDigits(); v++){
baser += base*otherbasebcd.nthDigit(v);
int j = v;
while(j < other.numberOfDigits()){
byten(otherbasebcd.nthDigit(j));
j++;
}
}
}
BCD newBCD = new BCD(baser);
return newBCD;
}
将输入更改为整数
int a= Integer.parseInt(basebcd.toString(), 2);
int b = Integer.parseInt(otherbasebcd.toString(), 2);
用两个整数进行运算
res = i*j;
转换为BCD
String s = Integer.toBinaryString(res);
你能展示一下byten的实现吗?一个更简单的方法是将两个BCD都转换成
int
,进行正常乘法,然后在返回之前将乘积转换回来。你如何实现“BCD”类型?
String s = Integer.toBinaryString(res);