The code copy is 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 implementation of large numbers multiplication is not very perfect yet
* Fix:
* 1. Some errors of deleting 0 before and after modification
* 2. Support negative number operation
* 3. To determine whether the input string meets the decimal definition, use regular expressions to judge
* @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]));
}
/**
* Calculate the product of two numbers of any size and precision
* @param first first parameter
* @param second second parameter
* @return Multiply of two numbers
*/
private static String bigNumberMultiply(String first, String second) {
// Sign of positive and negative sign
boolean flag = false;
if (first.charAt(0) == '-') {
flag = !flag;
first = first.substring(1);
}
if (second.charAt(0) == '-') {
flag = !flag;
second = second.substring(1);
}
// The position of the decimal point
int aPoints = first.length() - first.indexOf('.') - 1;
int bPoints = second.length() - second.indexOf('.') - 1;
int pointPos = aPoints + bPoints; // The decimal point position of the result
// Delete the 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]; // Array to save the result
// calculate
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
result[i + j] += a[i] * b[j];
}
}
// If a certain one in the result is greater than 9, it needs to be carried.
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(); // Convert result array to string
for (int i = 0; i < result.length; ++i) {
// Add decimal points
if(result.length - i == pointPos) {
buffer.append(".");
}
buffer.append(String.valueOf(result[i]));
}
if (buffer.indexOf(".") != -1)
{
// Delete the first 0
int i = 0;
while (i < buffer.length()) {
if (buffer.length() > 2 && buffer.charAt(i+1) == '.') { // There is only one number 0 before the decimal point.
break;
} else if (buffer.charAt(i) == '0') { // Delete the first 0
buffer.deleteCharAt(i);
i = 0;
continue;
} else { // When the first position is not 0
break;
}
}
// Delete the end 0
i = buffer.length() - 1;
while (i >= 0) {
if (buffer.length() > 2 && buffer.charAt(i-1) == '.') { // The decimal point is directly a number
break;
} else if (buffer.charAt(i) == '0') { // Delete the 0 at the end
buffer.deleteCharAt(i);
i = buffer.length() - 1;
continue;
} else { // When the last bit is not 0
break;
}
}
}
// According to the sign bit, the positive and negative flags of the return value
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 whether the input meets the requirements of floating point numbers
Pattern pattern = Pattern.compile("^(-?//d+|//d*)//.?//d*$");
Matcher matcher = pattern.matcher(number);
if (!matcher.find()) {
throw new IllegalArgumentException("The input number is incorrect!");
}
int[] result = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
result[i] = (int) (number.charAt(i) - '0');
}
return result;
}
}
The operation results are as follows:
1. Judgment of incorrect input
The code copy is as follows:
Input two large integers:
1a*a22
Exception in thread "main" java.lang.IllegalArgumentException: The input number is incorrect!
at BigNumber.string2IntArray(BigNumber.java:132)
at BigNumber.bigNumberMultiply(BigNumber.java:54)
at BigNumber.main(BigNumber.java:22)
2. Operation with negative numbers, with 0 before and after
The code copy is as follows:
Input two large integers:
-23424.2300*02345.234000000
-54935300.61982
The calculation results in python are as follows
The code copy is as follows:
Python 2.6.5
>>> -23424.2300*02345.23400000
-54935300.619819999
It can be seen that the results of python are distorted