还不能处理负数和小数点
packages1;
importjava.util.Stack;
importjava.util.Vector;
/** *//** * A multiply simulation
* for example :
* 56 X 67 =
*
* 56
* x 67
* ----------
* 392
* 336
*------------
* 3752
*
* So ,in this way,We can calculation very very big number,for example: 299^133 etc.
*
*@authorUrashima
*
*/
publicclassBigNumberMultiplyextendsBaseNumberOperation{
//each array indicates a paramprivateInteger[] paramFirst,paramSecond;
//main method
publicString calculate(String param1,String param2){
paramFirst=convert(param1);
paramSecond=convert(param2);
//multiply each bit,from low to highintindexFirst=paramFirst.length-1;
intindexSecond=paramSecond.length-1;
//multiply results for each bitVectorbranchResults=newVector();
for(;indexSecond>-1;indexSecond--){
branchResults.add(branchMultiply(paramFirst,paramSecond[indexSecond]));
} String finalResult=branchAddTogether(branchResults);
returnfinalResult;
}
privateInteger[] branchMultiply(Integer[] upper,Integer down){
Stackresults=newStack();
//todo : all core gose hereintcarryFlag=0;
for(intindex=upper.length-1;index>-1;index--){
intr1=down;
intr2=upper[index];
intr0=r1*r2+carryFlag;
carryFlag=(int)(r0/10);
intr3=r0-(10*carryFlag );
if(index!=0)
results.push(r3);
else results.push(r0);
}//sorts out and returnInteger[] branchResult=newInteger[results.size()];
results.toArray(branchResult);
//System.out.println (branchResult.toString());returnbranchResult;
}
privateString branchAddTogether(Vectorv){
Vectorparams=newVector();
//TODO: fix bug#001intbi=0;
for(Integer[] pps : v){
//revers ppsInteger[] rpps=newInteger[pps.length];
System.arraycopy(pps,0,rpps,0,pps.length);
for(intk=pps.length-1; k>-1; k--){
rpps[pps.length-1-k]=pps[k];
} v.set(bi++,rpps);
}//sort out data,add increamental 0 to each bit
for(Integer[] ii : v){
String pa="";
for(Integer i : ii){
pa+=i;
} params.add(pa);
}intincr=0;
for(intidx=0; idx{
String s=params.get(idx);
for(inti=0; i{
s+="0";
} params.set(idx,s);
//System.out.println (s);incr++;
}//convert back to int[]
for(inti=0; i{
String ctt=params.get(i);
//System.out.println (ctt);v.set(i,convert(ctt));
}//add them togetherStackresult;
//trim right and addresult=trimRightAdd(v);
StringBuffer sb=newStringBuffer();
try{
while(true)
sb.append(result.pop());
}catch(Exception e){
//pass,ignore.}returnsb.toString();
}
privateStacktrimRightAdd(Vectorparams){
Stackresult=newStack();
intcarry=0;
intmaxBit=0;
//todo:maxbit
for(Integer[] i : params){
intil=i.length;
maxBit=il>maxBit?il:maxBit;
}//bit moving , from low to higthintindexDecreaseCount=1;
intcolumnValue=0;
intbitValue=0;
for(intk=0; k{
if(k>0){
result.push(bitValue);
columnValue=0;
bitValue=0;
}//value of each column,including carryintnum=0;
for(Integer[] param : params){
intindex=param.length-indexDecreaseCount;
try{
num=param[index];
}catch(Exception e){
num=0;
}//TODO: may be simulation calculate is better herecolumnValue+=num;
}//first bit
if(k!=maxBit-1){
columnValue+=carry;
carry=(int)(columnValue/10);
bitValue=columnValue-(10*carry );
indexDecreaseCount++;
}else{
columnValue+=carry;
result.push(columnValue);
} }returnresult;
}
}测试计算结果
packages1;
publicclassDemo{
privateTwoNumberOperation operatorMultiply=newBigNumberMultiply();
publicvoidmultiplyDemo(String param1,String param2){
String result=operatorMultiply.calculate(param1,param2);
System.out.println (param1+"x"+param2+"="+result);
}
publicstaticvoidmain (String[] args){
Demo demo=newDemo();
demo.multiplyDemo("12","12");
demo.multiplyDemo("56","67");
demo.multiplyDemo("3459398","11667278");
demo.multiplyDemo("9283736253829832323432342342543654576343534564353734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553745645545",
"23470926947345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537863053758734076407083760987207345345345345345345537693709479");
}
}
--------------------Configuration: --------------------
12 x 12 = 144
56 x 67 = 3752
3459398 x 11667278 = 40361758178644
9283736253829832323432342342543654576343534564353734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553745645545 x 23470926947345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537863053758734076407083760987207345345345345345345537693709479 = 217897895412061538535213749538424917178427642790309526149197050342787946552594940694811082625513148107368458788645637103672066743646902872289253339664141494118086813947265391829794894470256056436951017797373234350763998817946444452982381515332165838866556971112151531240855136817304521889160157930869290050844235614446072972714978233006835557537630008993034139267140464023218920328137852716504553853724234899138660678581541565601300563516326950021051186577024411324340905167953542819936071934540621055
Process completed.