美文网首页
2018-03-16

2018-03-16

作者: 0cc3ce89d8bc | 来源:发表于2018-03-16 21:08 被阅读0次

    JAVA - 长数据运算

    使用数组实现长数据加法、乘法的简单模拟


    使用数组,模拟类似竖式运算的模式,逐位进行运算与进位,实现长数据的加法、乘法


    import java.util.Scanner;
    
    public class Test {
    
        final static int NUMBER = 100;//这个值为第一个操作数的长度,理论上可以修改为其他值,实现对应的,但是没有测试过
    
        static void toInt(int a[], String raw) {
            for (int i = raw.length() - 1; i >= 0; i--) {
                a[NUMBER - raw.length() + i] = raw.charAt(i) - 48;// 将字符转化为数字,这里是对ASCII码进行的操作
            }
        }
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("please input the first number: ");
            String raw = sc.nextLine();
    
            int a[] = new int[NUMBER];
            toInt(a, raw);
    
            System.out.println("please input the second number: ");
            int ope = sc.nextInt();
    
            System.out.println("input 1 for addition, 2 for multiplication: ");
            int flag = sc.nextInt();
    
            int index = 0;
            switch (flag) {
            case 1:
                for (int i = NUMBER - 1; ope > 0; i--) {// 模拟加法算法,这里可以把第二个操作数也定义为数组类型,实现两个长数据的计算
                    a[i] += (ope % 10);
                    ope /= 10;
                    for (int j = i; j > 0; j--) {
                        if (a[j] >= 10) {// 进位操作
                            a[j] %= 10;
                            a[j - 1] += 1;
                        }
                    }
                }
                index = 0;
                for (; index < NUMBER; index++) {// 输出之前去掉无效的0
                    if (a[index] != 0) {
                        break;
                    }
                }
                for (; index < NUMBER; index++) {// 进行输出
                    System.out.print(a[index]);
                }
                break;
            case 2:
                int tail = NUMBER - 1;
                int temp[] = new int[NUMBER];
                for (int i = 0; i < NUMBER; i++) {
                    temp[i] = 0;// 初始化操作
                }
    
                for (int move = 0; ope > 0; ope /= 10, move++) {//模拟乘法算法
                    int nowOpe = ope % 10;//将乘数拆开,模拟竖式运算
                    if (0 == nowOpe) {//乘数拆出0则跳过
                        continue;
                    }
                    for (int local = tail; local > 0; local--) {
                        if ((local - move) < 0) {//移位后会出现下标为负的情况,无意义
                            continue;
                        }
                        temp[local - move] += a[local] * nowOpe;//逐位运算
                        for (int flag1 = local - move; temp[flag1] >= 10; flag1--) {//模拟进位
                            temp[flag1 - 1] += temp[flag1] / 10;
                            temp[flag1] %= 10;
                        }
                    }
                }
                index = 0;
                for (; index < NUMBER; index++) {// 输出之前去掉无效的0
                    if (temp[index] != 0) {
                        break;
                    }
                }
                for (; index < NUMBER; index++) {// 进行输出
                    System.out.print(temp[index]);
                }
                break;
            default:
                System.out.println("不会算,非法输入");
            }
    
            sc.close();
        }
    
    }
    

    相关文章

      网友评论

          本文标题:2018-03-16

          本文链接:https://www.haomeiwen.com/subject/gsirqftx.html