算法3:计算超大数字整数乘法

还不能处理负数和小数点

None.gif package  s1;
None.gif
None.gif
import  java.util.Stack;
None.gif
import  java.util.Vector;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * A multiply simulation
InBlock.gif * for example : 
InBlock.gif * 56 X 67 = 
InBlock.gif *
InBlock.gif *     56
InBlock.gif *   x 67
InBlock.gif * ----------
InBlock.gif *    392
InBlock.gif *   336
InBlock.gif *------------
InBlock.gif *   3752
InBlock.gif *
InBlock.gif * So ,in this way,We can calculation very very big number,for example: 299^133 etc. 
InBlock.gif * 
InBlock.gif * 
@author Urashima 
InBlock.gif *
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  BigNumberMultiply  extends  BaseNumberOperation dot.gif {
InBlock.gif    
InBlock.gif    
//each array indicates a param
InBlock.gif
    private Integer[] paramFirst,paramSecond;
InBlock.gif    
InBlock.gif    
//main method    
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public String calculate(String param1,String param2)dot.gif{
InBlock.gif        paramFirst  
= convert(param1);
InBlock.gif        paramSecond 
= convert(param2);
InBlock.gif        
//multiply each bit,from low to high
InBlock.gif
        int indexFirst = paramFirst.length - 1;
InBlock.gif        
int indexSecond = paramSecond.length - 1;
InBlock.gif        
//multiply results for each bit
InBlock.gif
        Vector<Integer[]> branchResults = new Vector<Integer[]>();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(;indexSecond > -1;indexSecond--)dot.gif{
InBlock.gif            branchResults.add(branchMultiply(paramFirst,paramSecond[indexSecond]));
ExpandedSubBlockEnd.gif        }

InBlock.gif        String finalResult 
= branchAddTogether(branchResults);
InBlock.gif        
return finalResult;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private Integer[] branchMultiply(Integer[] upper,Integer down)dot.gif{
InBlock.gif        Stack
<Integer> results = new Stack<Integer>();
InBlock.gif        
//todo : all core gose here
InBlock.gif
        int carryFlag = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int index = upper.length-1 ;index > -1;index--)dot.gif{
InBlock.gif            
int r1 = down;
InBlock.gif            
int r2 = upper[index];
InBlock.gif            
int r0 = r1 * r2 + carryFlag;
InBlock.gif            carryFlag 
= (int)(r0/10);
InBlock.gif            
int r3 = r0 - ( 10 * carryFlag );
InBlock.gif            
if(index!=0)
InBlock.gif                results.push(r3);
InBlock.gif            
else
InBlock.gif                  results.push(r0);    
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//sorts out and return
InBlock.gif
        Integer[] branchResult = new Integer[results.size()];
InBlock.gif        results.toArray(branchResult);
InBlock.gif        
//System.out.println (branchResult.toString());
InBlock.gif
        return branchResult;
ExpandedSubBlockEnd.gif    }
    
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private String branchAddTogether(Vector<Integer[]> v)dot.gif{
InBlock.gif        Vector
<String> params = new Vector<String>();
InBlock.gif        
//TODO: fix bug#001
InBlock.gif
        int bi = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(Integer[] pps : v)dot.gif{
InBlock.gif            
//revers pps
InBlock.gif
            Integer[] rpps = new Integer[pps.length];
InBlock.gif            System.arraycopy(pps,
0,rpps,0,pps.length);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for(int k = pps.length-1 ; k > -1 ; k-- )dot.gif{
InBlock.gif                rpps[pps.length
-1-k] = pps[k];
ExpandedSubBlockEnd.gif            }

InBlock.gif            v.set(bi
++,rpps);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//sort out data,add increamental 0 to each bit
ExpandedSubBlockStart.gifContractedSubBlock.gif
        for(Integer[] ii : v)dot.gif{
InBlock.gif            String pa 
= "";
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for(Integer i : ii)dot.gif{
InBlock.gif                pa 
+= i;
ExpandedSubBlockEnd.gif            }

InBlock.gif            params.add(pa);
ExpandedSubBlockEnd.gif        }
     
InBlock.gif        
int incr = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int idx = 0 ; idx < params.size(); idx ++)dot.gif{
InBlock.gif            String s 
= params.get(idx);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for(int i = 0 ; i < incr ; i ++)dot.gif{
InBlock.gif                s 
+= "0";
ExpandedSubBlockEnd.gif            }

InBlock.gif            params.set(idx,s);
InBlock.gif            
//System.out.println (s);
InBlock.gif
            incr++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//convert back to int[]
ExpandedSubBlockStart.gifContractedSubBlock.gif
        for(int i = 0 ; i < params.size();i++)dot.gif{
InBlock.gif            String ctt 
= params.get(i);
InBlock.gif            
//System.out.println (ctt);
InBlock.gif
            v.set(i,convert(ctt));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//add them together    
InBlock.gif
        Stack<Integer> result;
InBlock.gif        
//trim right and add
InBlock.gif
        result = trimRightAdd(v);
InBlock.gif        StringBuffer sb 
= new StringBuffer();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
trydot.gif{
InBlock.gif         
while(true)
InBlock.gif             sb.append(result.pop());
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e)dot.gif{
InBlock.gif            
//pass,ignore.
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
return sb.toString();    
ExpandedSubBlockEnd.gif    }
    
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private Stack<Integer> trimRightAdd(Vector<Integer[]> params)dot.gif{    
InBlock.gif        Stack
<Integer> result = new Stack<Integer>();
InBlock.gif        
int carry = 0;
InBlock.gif        
int maxBit = 0;
InBlock.gif        
InBlock.gif        
//todo:maxbit
ExpandedSubBlockStart.gifContractedSubBlock.gif
        for(Integer[] i : params)dot.gif{
InBlock.gif            
int il = i.length;
InBlock.gif            maxBit 
= il > maxBit?il:maxBit;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//bit moving , from low to higth
InBlock.gif
        int indexDecreaseCount = 1;
InBlock.gif        
int columnValue = 0;
InBlock.gif        
int bitValue = 0;    
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int k = 0 ; k < maxBit; k++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
if(k > 0)dot.gif{
InBlock.gif             result.push(bitValue);
InBlock.gif             columnValue 
= 0;
InBlock.gif             bitValue 
= 0;
ExpandedSubBlockEnd.gif         }

InBlock.gif         
//value of each column,including carry    
InBlock.gif
         int num = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif         
for(Integer[] param : params)dot.gif{
InBlock.gif               
int index = param.length - indexDecreaseCount;
ExpandedSubBlockStart.gifContractedSubBlock.gif             
trydot.gif{
InBlock.gif                 num 
= param[index];
ExpandedSubBlockStart.gifContractedSubBlock.gif             }
catch(Exception e)dot.gif{
InBlock.gif                 num 
= 0;
ExpandedSubBlockEnd.gif             }

InBlock.gif             
//TODO: may be simulation calculate is better here
InBlock.gif
             columnValue += num;
ExpandedSubBlockEnd.gif         }

InBlock.gif         
//first bit
ExpandedSubBlockStart.gifContractedSubBlock.gif
         if(k != maxBit-1 )dot.gif{
InBlock.gif             columnValue 
+= carry;
InBlock.gif             carry 
= (int)(columnValue/10);
InBlock.gif             bitValue 
= columnValue - ( 10 * carry );
InBlock.gif             indexDecreaseCount 
++;
ExpandedSubBlockStart.gifContractedSubBlock.gif         }
elsedot.gif{
InBlock.gif             columnValue 
+= carry;
InBlock.gif             result.push(columnValue);
ExpandedSubBlockEnd.gif         }

ExpandedSubBlockEnd.gif       }

InBlock.gif       
return result;
ExpandedSubBlockEnd.gif    }
    
InBlock.gif    
ExpandedBlockEnd.gif}
测试计算结果
None.gif package  s1;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Demo dot.gif {
InBlock.gif    
InBlock.gif    
private TwoNumberOperation operatorMultiply = new BigNumberMultiply();
InBlock.gif    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值