Java实现高精度加减法-数据结构综合设计实验一

目录

测试类(主类)

双向链表类


测试类(主类)

import java.util.Scanner;

public class BigInt {

    /**
     * 处理用户输入的字符串,并将字符串以链表形式倒序存储
     */
    public static DoublyListNode<Integer> createListFromString(String str) {
        DoublyListNode<Integer> list = new DoublyListNode<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int num = Character.getNumericValue(c);
            list.addFirst(num);
        }
        return list;
    }

    /**
     * 比较两个链表的绝对值大小
     */
    public static int compare(DoublyListNode list11, DoublyListNode list22) {//0是相等  1是list1大  2是 list2大
        int len1 = list11.size();
        int len2 = list22.size();
        DoublyListNode list1 = list11;
        DoublyListNode list2 = list22;
        if (list1.size() > list2.size()) {
            return 1;
        } else if (list1.size() < list2.size()) {
            return 2;
        } else  {
            DoublyListNode.Node head1 = list1.find(0);
            DoublyListNode.Node head2 = list2.find(0);
            while (len1 > 0 && len2 > 0) {
                if (head1.val > head2.val) {
                    return 1;
                } else if (head1.val < head2.val) {
                    return 2;
                } else {
                    head1 = head1.next;
                    head2 = head2.next;
                    len1--;
                    len2--;
                }
            }
        }
        return 0;
    }

    /**
     * 给长度短的链表添0
     */
    public static Object addZero(DoublyListNode list, int max) {
        int rnd = max - list.size();
        while (rnd-- > 0) {
            list.addLast(0);
        }
        return list;
    }

    /**
     *将两个链表的长度对齐
     */
    public static void alignList(DoublyListNode list1, DoublyListNode list2) {
        if (list1.size() != list2.size()) {
            int max = list1.size() > list2.size() ? list1.size() : list2.size();
            if (max == list1.size()) {
                addZero(list2, max);
            } else if (max == list2.size()) {
                addZero(list1, max);
            }
        }
    }


    /**
     * 加法的进位运算
     */
    public static void add(DoublyListNode list1, DoublyListNode list2) {
        DoublyListNode result = new DoublyListNode();
        int carry = 0;
        alignList(list1,list2);
        DoublyListNode.Node head1 = list1.find(0);
        DoublyListNode.Node head2 = list2.find(0);

        while (head2.next != null) {
            int res = head1.val + head2.val + carry;
            if (res >= 10) {
                carry = 1;
                res = res % 10;
                result.addFirst(res);
            } else {
                carry = 0;
                result.addFirst(res);
            }
            head1 = head1.next;
            head2 = head2.next;
        }
        if (carry == 1) result.addFirst(1);
        result.printLink();
        //result.printLinkWithComma();
    }

    /**
     * 减法的借位运算
     */
    public static void sub(DoublyListNode list1, DoublyListNode list2) {
        DoublyListNode result = new DoublyListNode();
        alignList(list1,list2);
        DoublyListNode.Node head1 = list1.find(0);
        DoublyListNode.Node head2 = list2.find(0);
        while (head2.next != null) {
            int borrow = 10;
            if (head1.val > head2.val) {
                int res = head1.val - head2.val;
                result.addFirst(res);
            } else if (head1.val < head2.val) {
                int res = head1.val + borrow - head2.val;
                head1.next.val -= 1;
                result.addFirst(res);
            } else if (head1.val == head2.val) {
                result.addFirst(0);
            }
            head1 = head1.next;
            head2 = head2.next;
        }
        DoublyListNode.Node head = result.find(0);
        if (head.val == 0) {
            while (head.val == 0) {
                result.removeFirst();
                head = head.next;
            }
        }
        result.printLink();
        //result.printLinkWithComma();
    }

    /**
     *处理'+'(加号)运算,涉及|绝对值|与正负号
     */
    public static void performAddition(int sign1, int sign2, DoublyListNode list1, DoublyListNode list2) {
        if(sign1 == 0 && sign2 == 0) {//a+b
            add(list1, list2);
        } else if(sign1 == 0 && sign2 == 1) {//a-b
            if(compare(list1, list2) == 1) {  //|a|>|b|
                sub(list1, list2);
            } else if(compare(list1, list2) == 2) {// |b| > |a|
                System.out.print("-");
                sub(list2, list1);
            } else if(compare(list1, list2) == 0) {
                System.out.println("0");
            }
        } else if(sign1 == 1 && sign2 == 0) {//-a +b ; b - a
            if(compare(list1, list2) == 2) {//|b| > |a|
                sub(list2, list1);
            } else if(compare(list1, list2) == 1) {//|a| > |b|
                System.out.print("-");
                sub(list1, list2);
            } else if(compare(list1, list2) == 0) {
                System.out.println("0");
            }
        } else if(sign1 == 1 && sign2 == 1) {//-a + (-b)   -(a+b)
            System.out.print("-");
            add(list1, list2);
        }
    }

    /**
     *处理'-'(减号)运算,涉及|绝对值|与正负号
     */
    public static void performSubtraction(int sign1, int sign2, DoublyListNode list1, DoublyListNode list2) {
        if(sign1 == 0 && sign2 == 0) {//a-b
            if(compare(list1, list2) == 1) {  //|a|>|b|
                sub(list1, list2);
            } else if(compare(list1, list2) == 2) {  // b > a
                System.out.print("-");
                sub(list2, list1);
            } else if(compare(list1, list2) == 0) {
                System.out.println("0");
            }
        } else if(sign1 == 0 && sign2 == 1) {//a - -b;  a+b
            add(list1, list2);
        } else if(sign1 == 1 && sign2 == 0) {//-a - b ; -(a+b)
            System.out.print("-");
            add(list1, list2);
        } else if(sign1 == 1 && sign2 == 1) {//a-b
            if(compare(list1, list2) == 2) {//|b| > |a|
                sub(list2, list1);
            } else if(compare(list1, list2) == 1) {//|a| > |b|
                System.out.print("-");
                sub(list1, list2);
            } else if(compare(list1, list2) == 0) {
                System.out.println("0");
            }
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个运算数:");
        String str1 = sc.nextLine();
        System.out.println("请输入第二个运算数:");
        String str2 = sc.nextLine();
        System.out.println("请输入运算符:");
        String opr = sc.nextLine();

        char li1 = str1.charAt(0), li2 = str2.charAt(0);
        int sign1 = 0, sign2=0;
        if (li1 == '-') {
            sign1 = 1;
            str1 = str1.substring(1);
        }
        if (li2 == '-') {
            sign2 = 1;
            str2 = str2.substring(1);
        }

        DoublyListNode<Integer> list1 = createListFromString(str1);
        DoublyListNode<Integer> list2 = createListFromString(str2);

        switch(opr) {
            case "+":
                performAddition(sign1, sign2, list1, list2);
                break;
            case "-":
                performSubtraction(sign1, sign2, list1, list2);
                break;
        }
       
    }
}

双向链表类

public class DoublyListNode<T> {
    static class Node {
        Node prev;
        int val;
        Node next;

        public Node(Node prev, int val, Node next) {
            this.prev = prev;
            this.val = val;
            this.next = next;
        }
    }

    public Node head;
    public Node tail;

    public DoublyListNode() {
        head = new Node(null, 0, null);
        tail = new Node(null, 0, null);
        head.next = tail;
        tail.prev = head;
    }

    /**
     * 返回链表长度
     */
    public int size() {
        Node head = find(0);
        int i = 0;
        while (head.next != null) {
            i++;
            head = head.next;
        }
        return i;
    }

    /**
     *查找第index个节点
     */
    public Node find(int index) {
        int i = -1;
        for(Node n = head; n != tail; n = n.next, i++) {
            if(i == index) {
                return n;
            }
        }
        return null;
    }

    /**
     *插入节点
     */
    public void insert(int index, int val) {
        Node prev = find(index - 1);
        Node next = prev.next;
        Node inserted = new Node(prev, val, next);
        prev.next = inserted;
        next.prev = inserted;
    }

    /**
     *头插法
     */
    public void addFirst(int val) {
        insert(0, val);
    }

    /**
     *尾插法
     */
    public void addLast(int val) {
        Node last = tail.prev;
        Node added = new Node(last, val, tail);
        last.next = added;
        tail.prev = added;
    }

    /**
     *把链表中第index号元素删除
     */
    public void remove(int index) {
        Node prev = find(index - 1);
        Node removed = prev.next;
        Node next = removed.next;
        prev.next = next;
        next.prev = prev;
    }

    /**
     * 把链表中第一个元素删除
     */
    public void removeFirst() {
        remove(0);
    }


    /**
     * 反转链表
     */
    public void reverse() {
        Node current = head.next;
        Node previous = null;

        while (current != null) {
            Node next = current.next;
            current.next = previous;
            current.prev = next;
            previous = current;
            current = next;
        }
        Node temp = head;
        head = tail;
        tail = temp;
    }


    /**
     * 打印链表
     */
    public void printLink() {
        Node head = find(0);
        if(head == null) {
            System.out.println("链表不存在");
        }
        else {
            while(head.next != null) {
                System.out.print(head.val);
                head = head.next;
            }
        }
    }

    /**
     * 打印链表,每三位输出一个逗号
     */
    public void printLinkWithComma() {
        Node head = find(0);
        if (head == null) {
            System.out.println("链表不存在");
        } else {
            int index = 0;
            while (head.next != null) {
                head = head.next;
                index++;
            }
            head = find(0);
            int cou =  index % 3;
            int i = 0;
            switch (cou) {
                case 0:
                    while (head.next != null) {
                        System.out.print(head.val);
                        head = head.next;
                        i++;
                        if(i == 3 && head.next!=null) {
                            System.out.print(",");
                            i = 0;
                        }
                    }
                    break;
                case 1:
                    int case1 = 1;
                    while (head.next != null) {
                        System.out.print(head.val);
                        head = head.next;
                        i++;
                        if(i == case1 && head.next!=null) {
                            System.out.print(",");
                            case1 = -1;
                            i = 0;
                        }
                        if(i == 3 && head.next!=null) {
                            System.out.print(",");
                            i = 0;
                        }
                    }
                    break;
                case 2:
                    int case2 = 2;
                    while (head.next != null) {
                        System.out.print(head.val);
                        head = head.next;
                        i++;
                        if(i == case2 && head.next!=null) {
                            System.out.print(",");
                            case2 = -1;
                            i = 0;
                        }
                        if(i == 3 && head.next!=null) {
                            System.out.print(",");
                            i = 0;
                        }
                    }
                    break;
            }
        }
    }

}

实验结果:

如果您想让结果每三位用逗号分隔,将测试类中的add和sub方法中 printLinkWithComma的注释删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值