自己用JAVA實現的大數相乘——稻草人
代碼:
import java.util.Scanner;
public class bigNumMultiply {
/**
* 實現兩個大數相乘
* @param args
*/
public static final int MAX_LEN = 10000000;
public static int[] tempArray1 = new int[MAX_LEN];
public static int[] tempArray2 = new int[MAX_LEN];//這兩個數組用來暫時存儲局部結果
//初始化數組函數
public static void Init()
{
for(int i=0;i<MAX_LEN;i++)
{
tempArray1[i] = 0;
tempArray2[i] = 0;
}
}
//實現大數相乘的函數
public static String mul(String num1,String num2)
{
String result = "";
int len1 = num1.length();
int len2 = num2.length();
int tempNum1 = 0,tempNum2 = 0;//num1和num2裏拆出來相乘的單個數
int pre = 0;//進位,初始化爲0
int mulRes = 0;//乘積
for(int i=0;i<len2;i++)
{
int loc_begin = 0;//往數組裏存放一輪計算結果的開始位置
tempNum2 = num2.charAt(i)-'0';
for(int j=0;j<len1;j++)
{
tempNum1 = num1.charAt(j)-'0';
mulRes = tempNum1*tempNum2+pre;
if(i==0)
{
if(j==len1-1){//一輪計算結束
tempArray1[loc_begin] = mulRes;
pre = 0;//一輪存儲完畢,進位置0
}
else//一輪計算沒有結束
{
pre = mulRes/10;//進位
tempArray1[loc_begin] = mulRes%10;//進位後保存結果
}
loc_begin++;
}
else
{
if(j==len1-1){//一輪計算結束
tempArray2[loc_begin] = mulRes;
pre = 0;//一輪存儲完畢,進位置0
}
else//一輪計算沒有結束
{
pre = mulRes/10;//進位
tempArray2[loc_begin] = mulRes%10;//進位後保存結果
}
loc_begin++;
}
}
if(i>0)
{
posSum(i,loc_begin-1);//錯位求和,結果存放在tempArray1中,i是錯位數
}
}
boolean boo = true;
for(int k=tempArray1.length-1;k>=0;k--)
{
if(tempArray1[k]==0 && boo)
{
continue;
}
else
{
boo = false;
result = result+tempArray1[k];
}
}
if(result=="")
result = "0";
return result;
}
//錯位求和
public static void posSum(int n,int len)
{
int pre = 0;
int sum = 0;
int i=0;
int j=0;
while(true)
{
if(i>=n)
{
sum = tempArray1[i] + tempArray2[j] + pre;
if(j<len)
{
tempArray1[i] = sum%10;
pre = sum/10;
}
if(j==len)
{
tempArray1[i] = sum;
break;
}
j++;
}
i++;
}
}
public String getMulRes(String num1,String num2)
{
boolean boo = true;//標記結果是否是正數,初始化假設是正數
String result = null;//存放兩個大數相乘的結果。
//判斷兩個大數均爲負數
if(num1.charAt(0)=='-' && num2.charAt(0)=='-')
{
num1 = (String)num1.subSequence(1, num1.length());
num2 = (String)num2.subSequence(1, num2.length());
}
else if(num1.charAt(0)=='-' || num2.charAt(0)=='-')//其中一個爲負數
{
boo = false;//一正一負,結果爲負數。
if(num1.charAt(0)=='-')
{
num1 = (String)num1.subSequence(1, num1.length());
}
else if(num2.charAt(0)=='-')
{
num2 = (String)num2.subSequence(1, num2.length());
}
}
num1 = reverse(num1);
num2 = reverse(num2);
if(num1.length()<=num2.length())
result = mul(num2,num1);
else
result = mul(num1,num2);
if(!boo&&!result.equals("0"))//如果結果爲負數,則在前面加‘-’號
result = "-"+result;
return result;
}
//字符串取反,如:(123-->321),方便之後求積
public static String reverse(String str)
{
char[] strarray = str.toCharArray();
StringBuffer sbf = new StringBuffer();
for (int i = strarray.length - 1; i >= 0; i--)
sbf.append(strarray[i]);
return sbf.toString();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
bigNumMultiply multiply = new bigNumMultiply();
System.out.println("請輸入第一個大數:");
while(in.hasNext())
{
Init();//初始化,數組置0
String strNum1 = in.next();
System.out.println("請輸入第二個大數:");
String strNum2 = in.next();
System.out.println(strNum1+"*"+strNum2+" = "+multiply.getMulRes(strNum1,strNum2));
System.out.println("請輸入第一個大數:");
}
}
}
測試數據:
請輸入第一個大數:
123456789
請輸入第二個大數:
987654321
123456789*987654321 = 121932631112635269
請輸入第一個大數:
10000
請輸入第二個大數:
10000
10000*10000 = 100000000
請輸入第一個大數:
87654321
請輸入第二個大數:
123455666677
87654321*123455666677 = 10821422636174761317
請輸入第一個大數:
4566000
請輸入第二個大數:
10001
4566000*10001 = 45664566000
請輸入第一個大數:
-78655443122
請輸入第二個大數:
98765411222
-78655443122*98765411222 = -7768437184792961515084
結果是0的測試數據:
請輸入第一個大數:
0
請輸入第二個大數:
0
0*0 = 0
請輸入第一個大數:
-99
請輸入第二個大數:
0
-99*0 = 0
請輸入第一個大數:
0
請輸入第二個大數:
788
0*788 = 0
請輸入第一個大數:
* 限於個人水平,代碼比較複雜,同求效率較高的代碼。