實現參與運算的數或者運算結果中,長度最大的乙個數的位數,不超過string型別最大長度的大數的四則運算。
加法:模擬筆算的過程,逐位運算;
減法:先比較大小,讓較大的數作為被減數(被減數-減數=差),再仿照加法運算;
乘法:和加法類似,逐位運算;
除法(整除):除法比較複雜一點,是用連續相減實現。不過用被除數逐個減除數顯然太慢了,我們首先得到被除數和除數相差的位數n,令temp為除數乘以10的n次方,使滿足被除數×0.1≤temp≤被除數,用被除數減去temp,然後n--,重新得到temp,重複上面過程,直至n為0;
求餘:和整除的思路一樣,只是最後返回的值不一樣。
public class bignumber
// 字串轉陣列
for (int i = len2 - 1, j = 0; i >= 0; i--, j++)
// 求和
for (int i = 0; i < sumlen; i++)
sum[i] = temp;
} stringbuilder result = new stringbuilder();
boolean flag = true;
for (int i = sumlen - 1; i >= 0; i--) else
}} return result.tostring();
} /**
* sub
*/public string sub(string string1, string string2) else if (relation == 0)
len1 = string1.length();
len2 = string2.length();
int sumlen = math.max(len1, len2);
int array1 = new int[sumlen];
int array2 = new int[sumlen];
int sum = new int[sumlen];
int temp = 0;
// 字串轉陣列
for (int i = len1 - 1, j = 0; i >= 0; i--, j++)
// 字串轉陣列
for (int i = len2 - 1, j = 0; i >= 0; i--, j++)
// 求差
for (int i = 0; i < sumlen; i++)
sum[i] = temp;
} boolean flag = true;
for (int i = sumlen - 1; i >= 0; i--) else
}} return result.tostring();
} /**
* mul
*/public string mul(string string1, string string2)
// 字串轉陣列
for (int i = len2 - 1, j = 0; i >= 0; i--, j++)
// 求積
for (int i = 0; i < len2; i++)
} string result = null;
stringbuilder midresult = new stringbuilder();
stringbuilder tempresult;
for (int i = len1; i >= 0; i--)
result = midresult.tostring();
if (len2 == 1)
for (int i = 1; i < len2; i++)
for (int k = 0; k < i; k++)
result = self.add(result, tempresult.tostring());
} return result;
} /**
* div
*/public string div(string string1, string string2) throws exception
int len1 = string1.length();
int len2 = string2.length();
int relation = self.getthen(string1, string2);
if (relation == -1) else if (relation == 0)
int distance = len1 - len2;
int count = 0;
stringbuilder midnum;
string midstring;
string midresult;
string result = "0";
while (distance >= 0)
midnum = new stringbuilder();
for (int i = 0; i < distance; i++)
midstring = midnum.tostring(); // 減數
if (self.getthen(midstring, string1) == 1)
count = 0;
while (self.getthen(midstring, string1) < 1)
midresult = self.mul(coe.tostring(), string.valueof(count));
result = self.add(result, midresult);
distance--;
} return result;
} /**
* mod
*/public string mod(string string1, string string2) throws exception
int len1 = string1.length();
int len2 = string2.length();
int relation = self.getthen(string1, string2);
if (relation == -1) else if (relation == 0)
int distance = len1 - len2;
stringbuilder midnum;
string midstring;
string result = "0";
while (distance >= 0)
midnum = new stringbuilder();
for (int i = 0; i < distance; i++)
midstring = midnum.tostring(); // 減數
if (self.getthen(midstring, string1) == 1)
while (self.getthen(midstring, string1) < 1)
result = string1;
distance--;
} return result;
} /**
* get relation ; str1 > str2 -> 1 ; str1 = str2 -> 0 ; str1 < str2 -> -1 ;
*/private int getthen(string string1, string string2) else if (len1 > len2) else if (len1 == len2) else if (string1.charat(i) > string2.charat(i)) else
return -1;
}} return 0;
} /**
* return standard string
*/private string getstandardstring(string string)
} string = stringbuilder.tostring();
stringbuilder = new stringbuilder();
// 剝離開頭的數字0
for (int index = 0; index < string.length(); index++) else
}} string result = stringbuilder.tostring();
if (result == null || result.length() == 0)
return result;
}}
大數四則運算
include include includeusing namespace std define m 1000000000000000 大數加法 將數字以字串的形式傳入add加法函式,在函式內部完成字串 陣列的轉換,然後在陣列中逐位進行相加,再判斷該位相加後是否需要進製,為了方便計算,我們將數字的...
大數的四則運算
演算法與思路 大數的加,減,乘演算法比較簡單,模擬列豎式的計算過程就好 除法模擬實現比較困難,採用減法的形式實現,例如對於大數a b,如果a b,則直接輸出商為0,否則,將a的最後strlen b 位重複減去b,直到得數小於b,將執行減法的次數賦給商的個位,然後將b的最後添上乙個0,相當於乘以10 ...
大數的四則運算
1.加法 輸入採用字元陣列儲存,然後將輸入存在整形陣列裡,然後逐位相加即可,同時注意進製處理。cpp view plain copy include include intmain b 555 c 555 scanf s m len m strlen m for i 0 i len m 1 i a ...