欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java大数乘法的简单实现 浮点数乘法运算

程序员文章站 2024-02-23 08:22:10
复制代码 代码如下:import java.io.bufferedreader;import java.io.ioexception;import java.io.inpu...

复制代码 代码如下:

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.util.regex.matcher;
import java.util.regex.pattern;

/**
 * 大数乘法的简单实现, 目前还不是很完善
 * fix:
 * 1. 修改前后删除0的一些错误情况
 * 2. 支持负数运算
 * 3. 判断输入字符串是否符合小数定义, 用正则表达式判断
 * @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]));
    }

    /**
     * 计算两个任意大小和精度的数的乘积
     * @param first 第一个参数
     * @param second 第二个参数
     * @return 两个数的乘积
     */
    private static string bignumbermultiply(string first, string second) {
        // 正负号判断标志
        boolean flag = false;

        if (first.charat(0) == '-') {
            flag = !flag;
            first = first.substring(1);
        }

        if (second.charat(0) == '-') {
            flag = !flag;
            second = second.substring(1);
        }

        // 小数点的位置
        int apoints = first.length() - first.indexof('.') - 1;
        int bpoints = second.length() - second.indexof('.') - 1;
        int pointpos = apoints + bpoints; // 结果的小数点位置

        // 删除小数点
        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]; // 保存结果的数组

        // 计算
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b.length; j++) {
                result[i + j] += a[i] * b[j];
            }
        }

        // result中的某一位大于9的话需要进位
        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(); // 将result数组转换为字符串
        for (int i = 0; i < result.length; ++i) {
            // 添加小数点
            if(result.length - i == pointpos) {
                buffer.append(".");
            }
            buffer.append(string.valueof(result[i]));
        }

        if (buffer.indexof(".") != -1)
        {
            // 删除最开始的0
            int i = 0;
            while (i < buffer.length()) {
                if (buffer.length() > 2 && buffer.charat(i+1) == '.') { // 小数点前只有一个数 0.
                    break;
                } else if (buffer.charat(i) == '0') { // 删除最前边的0
                    buffer.deletecharat(i);
                    i = 0;
                    continue;
                } else { // 当第一位不是0的时候
                    break;
                }
            }

            // 删除末尾的0
            i = buffer.length() - 1;
            while (i >= 0) {
                if (buffer.length() > 2 && buffer.charat(i-1) == '.') { // 小数点后直接是数字
                    break;
                } else if (buffer.charat(i) == '0') { // 删除末尾的0
                    buffer.deletecharat(i);
                    i = buffer.length() - 1;
                    continue;
                } else { // 当最后一位不是0的时候
                    break;
                }
            }
        }

        // 根据符号位, 返回值的正负标志
        if (flag) {
            return "-" + buffer.tostring();
        } else {
            return buffer.tostring();
        }
    }

    /**
     * 将字符串装换为数组
     * @param number
     * @return
     */
    private static int[] string2intarray(string number) {
        // 判断输入是否符合浮点数的要求
        pattern pattern = pattern.compile("^(-?\\d+|\\d*)\\.?\\d*$");
        matcher matcher = pattern.matcher(number);
        if (!matcher.find()) {
            throw new illegalargumentexception("输入的数不正确!");
        }

        int[] result = new int[number.length()];
        for (int i = 0; i < number.length(); i++) {
            result[i] = (int) (number.charat(i) - '0');
        }
        return result;
    }
}

运行结果如下:

1. 错误输入的判断

复制代码 代码如下:

input two large integers:
1a*a22
exception in thread "main" java.lang.illegalargumentexception: 输入的数不正确!
at bignumber.string2intarray(bignumber.java:132)
at bignumber.bignumbermultiply(bignumber.java:54)
at bignumber.main(bignumber.java:22)


 2. 带负数的运算, 前后带有0的情况
复制代码 代码如下:

input two large integers:
-23424.2300*02345.23400000
-54935300.61982

 python中计算的结果如下
复制代码 代码如下:

python 2.6.5
>>> -23424.2300*02345.23400000
-54935300.619819999

 可以看出python的结果是有失真的