Copy Code code as follows:
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
/**
* The simple realization of large number multiplication is not perfect at present
* Fix:
* 1. Delete 0 error conditions before and after the modification
* 2. Support for negative operations
* 3. Determines whether the input string conforms to the decimal definition and uses regular expressions to determine
* @author Icejoywoo
* @since 2012.2.16
* @version 0.1.1
*/
public class BigNumber {
public static void Main (string[] args) throws IOException {
System.out.println ("Input two large integers:");
BufferedReader buffer = new BufferedReader (new InputStreamReader (system.in));
string[] Strarray = Buffer.readline (). Split ("\\*");
System.out.println (Bignumbermultiply (strarray[0], strarray[1));
}
/**
* Calculates the product of two arbitrary size and precision numbers
* @param first parameter
* @param second second parameter
* @return The product of two numbers
*/
private static string Bignumbermultiply (string A, string second) {
Positive sign
Boolean flag = false;
if (First.charat (0) = = '-') {
flag =!flag;
The first.substring (1);
}
if (Second.charat (0) = = '-') {
flag =!flag;
Second = second.substring (1);
}
Position of decimal point
int apoints = First.length ()-First.indexof ('. ')-1;
int bpoints = Second.length ()-Second.indexof ('. ')-1;
int pointpos = apoints + bpoints; The decimal position of the result
Remove decimal point
StringBuffer abuffer = new StringBuffer (First.replaceall ("\", ""));
StringBuffer bbuffer = new StringBuffer (Second.replaceall ("\", ""));
Int[] A = String2intarray (abuffer.tostring ());
Int[] B = String2intarray (bbuffer.tostring ());
Int[] result = new Int[a.length + b.length-1]; An array of saved results
Calculation
for (int i = 0; i < a.length; i++) {
for (int j = 0; J < B.length; J + +) {
Result[i + j] + = a[i] * B[j];
}
}
If one of the result is greater than 9, it needs to carry
for (int i = result.length-1 i >= 0; i.) {
if (Result[i] > 9) {
Result[i-1] + = result[i]/10;
Result[i] = result[i]% 10;
}
}
StringBuffer buffer = new StringBuffer (); Converts a result array to a string
for (int i = 0; i < result.length; ++i) {
Add decimal point
if (result.length-i = = Pointpos) {
Buffer.append (".");
}
Buffer.append (string.valueof (result[i));
}
if (Buffer.indexof (".")!=-1)
{
Delete the start of the 0
int i = 0;
while (I < buffer.length ()) {
if (Buffer.length () > 2 && buffer.charat (i+1) = = '. ') {//Only a few 0 before the decimal point.
Break
else if (Buffer.charat (i) = = ' 0 ') {//delete the front 0
Buffer.deletecharat (i);
i = 0;
Continue
else {//when the first one is not 0
Break
}
}
Delete 0 at end
i = Buffer.length ()-1;
while (I >= 0) {
if (Buffer.length () > 2 && buffer.charat (i-1) = = '. ') {//number directly after the decimal point
Break
else if (Buffer.charat (i) = = ' 0 ') {//delete end of 0
Buffer.deletecharat (i);
i = Buffer.length ()-1;
Continue
else {//when the last one is not 0
Break
}
}
}
Returns the positive or negative sign of a value based on the sign bit
if (flag) {
Return "-" + buffer.tostring ();
} else {
return buffer.tostring ();
}
}
/**
* Replace the string into an array
* @param number
* @return
*/
private static int[] String2intarray (String number) {
Determine if input meets floating-point number requirements
Pattern pattern = Pattern.compile ("^ (-?\\d+|\\d*) \".? \\d*$ ");
Matcher Matcher = pattern.matcher (number);
if (!matcher.find ()) {
throw new IllegalArgumentException ("Incorrect number of inputs!");
}
Int[] result = new int[number.length ()];
for (int i = 0; i < number.length (); i++) {
Result[i] = (int) (Number.charat (i)-' 0 ');
}
return result;
}
}
The results of the operation are as follows:
1. Error input judgment
Copy Code code as follows:
Input two large integers:
1a*a22
Exception in thread "main" Java.lang.IllegalArgumentException: incorrect number entered!
At Bignumber.string2intarray (bignumber.java:132)
At Bignumber.bignumbermultiply (bignumber.java:54)
At Bignumber.main (bignumber.java:22)
2. With negative operation, with 0 of the case before and after
Copy Code code as follows:
Input two large integers:
-23424.2300*02345.23400000
-54935300.61982
The results of the calculations in Python are as follows
Copy Code code as follows:
Python 2.6.5
>>> -23424.2300*02345.23400000
-54935300.619819999
You can see that Python's results are distorted.