- 本篇來(lái)源于前段時(shí)間網(wǎng)申筆試時(shí)的一道編程題,其題意就是不調(diào)用已有的 API 捌显,自己設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)大整數(shù)的封裝以及加法茁彭、乘法運(yùn)算。
- 題主主學(xué) JAVA 知識(shí)扶歪,當(dāng)然知道 JAVA 提供了 BigInteger 和 BigDecimal 兩個(gè)類來(lái)實(shí)現(xiàn)大整數(shù)的加理肺、減、乘善镰、除等運(yùn)算妹萨,兩個(gè)類都位于 java.math 包下。項(xiàng)目中若需要直接調(diào)用即可炫欺。但是針對(duì)這道筆試題乎完,我當(dāng)時(shí)也認(rèn)真思考下,大概就是利用數(shù)組實(shí)現(xiàn)品洛。我就直接上代碼树姨,代碼中有注釋詳解摩桶。
- 值得閱讀者注意的就是,我代碼中是怎么將序列翻轉(zhuǎn)帽揪,再翻轉(zhuǎn)分別存儲(chǔ)在數(shù)組中硝清,且為啥從數(shù)組最后一個(gè)元素開(kāi)始輸出。還有就是代碼中引入了第三方的包 org.apache.commons.lang3.StringUtils下載地址為
import org.apache.commons.lang3.StringUtils;
public class BigInt {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInt bigInt1 = new BigInt("1231723333333131343453458938458937834234242534532");
BigInt bigInt2 = new BigInt("234242342342387492746278462783492748926742342424");
System.out.println(bigInt1.toString());
System.out.println(bigInt2.toString());
System.out.println(bigInt1.addBigInt(bigInt2));
System.out.println(bigInt1.multiply(bigInt2));
/*System.out.println(bigInt1.subtract(bigInt2));*/
}
public BigInt(String bigbigNum) {
splitToArray(bigbigNum);
}
private int[] arrayInt = new int[100];
//強(qiáng)數(shù)據(jù)拆分為數(shù)組
public void splitToArray(String bigNum){
StringBuffer sb = new StringBuffer();
//先將 字符串逆序
bigNum = new StringBuffer(bigNum).reverse().toString();
int j = 0;
for (int i = 0; i < bigNum.length(); i++) {
// 用一個(gè)整形數(shù)組來(lái)儲(chǔ)存转晰,且長(zhǎng)度不超過(guò) 4位
if (i%4 ==0) {
//再講sb逆序之后 存儲(chǔ)于整形數(shù)組第i和元素中
if(sb !=null && !sb.toString().equals("")){
arrayInt[j] = Integer.valueOf(sb.reverse().toString());
sb = new StringBuffer();
j++;
}
}
sb.append(bigNum.charAt(i));
}
//處理最后幾個(gè)不滿4位的數(shù)據(jù)
if( sb!=null ){
arrayInt[j] = Integer.valueOf(sb.reverse().toString());
}
}
//從數(shù)組后開(kāi)始打印輸出芦拿,才能得到原序列
public String printArrayInt(int[] array){
String str = "";
StringBuffer sb = new StringBuffer();
boolean isFirstArrayInt = true;
for (int i = array.length-1; i >=0; i--) {
if (array[i] != 0) {
System.out.println("第"+i+"個(gè)數(shù)據(jù):"+array[i]);
//判斷每個(gè)數(shù)組元素是否都為 4位,不足4位在左邊補(bǔ)0
if (!isFirstArrayInt && String.valueOf(array[i]).length()<4) {
sb.append(StringUtils.leftPad(String.valueOf(array[i]),4,"0"));
}else {
sb.append(array[i]);
if(isFirstArrayInt)
isFirstArrayInt = false;
}
}
}
return sb.toString();
}
//利用數(shù)組實(shí)現(xiàn)大整數(shù)的加法
public String addBigInt(BigInt bigInt){
int a[] = this.arrayInt;
int b[] = bigInt.arrayInt;
int result[] = new int[100];
for (int i = 0; i < a.length; i++) {
if (a[i] == 0 && b[i] != 0) {
result[i] = b[i];
}else if (a[i] != 0 && b[i] == 0) {
result = a;
}else if (a[i] != 0 && b[i] != 0) {
result[i] = a[i]+b[i];
}else {
result[i] = 0;
}
}
//處理result的進(jìn)位
for (int i = 0; i < result.length; i++) {
if (result[i] != 0 && String.valueOf(result[i]).length()>4) {
result[i] = Integer.valueOf(String.valueOf(result[i]).substring(1));
result[i+1] = result[i+1] +1;
}
}
return printArrayInt(result);
}
//實(shí)現(xiàn)大整數(shù)乘法
public String multiply(BigInt bigInt) {
int a[] = this.arrayInt;
int b[] = bigInt.arrayInt;
int result[] = new int[100];
for (int i = 0; i < a.length; i++) {
if (a[i] == 0 || b[i] == 0) {
result[i] = 0;
}else {
result[i] = a[i] * b[i];
}
}
//處理result的進(jìn)位
for (int i = 0; i < result.length; i++) {
if (result[i] != 0 && String.valueOf(result[i]).length()>4) {
result[i] = Integer.valueOf(String.valueOf(result[i]).substring(1));
result[i+1] = result[i+1] +1;
}
}
return printArrayInt(result);
}
@Override
public String toString(){
return printArrayInt(arrayInt);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者