【问题标题】:Adding two array objects together将两个数组对象相加
【发布时间】:2018-04-15 02:57:56
【问题描述】:

我正在尝试在 add(DNumber b) 方法中添加两个 DNumber 对象。关键是能够进行二进制算术。元素正常存储。我应该如何处理不均匀的 ArrayLists?用 0 填充元素较少的那个?那么我应该如何检索每个元素?还有什么是在不使用转换为十进制方法的情况下转换为十进制的好方法:

public class DNumber{
    ArrayList<Digit> binary = new ArrayList<Digit>();
    /**
     * Constructor for objects of class DNumber
     */
    public DNumber()
    {
        Digit num = new Digit(0);
        binary.add(num);
    }

    public DNumber(int val){
        int num = val;
        if(num > 0){
            while (num > 0){
                Digit bin = new Digit(num%2);
                num /= 2;
                binary.add(0, bin);
            }
        }
        else{
            Digit bin = new Digit(0);
            binary.add(0,bin);
        }
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public String toString(){
        String s = "";
        for(Digit d : binary){
            s = s + d.toString();
        }
        return s;
    }

    public void add(DNumber b){
        int ArraySize1 = binary.size() -1;
        int ArraySize2 = b.binary.size() -1;


    }

    public void toDecimal(){
        /**
         *
         *  String s = "";
         int result = 0;
         int power = 0;
         for(Digit d : binary){
         s = s + d.toString();
         result = Integer.parseInt(s);
         }
         */
    }
}

public class Digit {
    int x = 0;

    /**
     * Constructor for objects of class Digit
     */
    public Digit(int val) {
        if (val != 0 && val != 1) {
            System.out.println("Error Must be either 1 or 0");
            x = 0;
        } else {
            x = val;
        }
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param y a sample parameter for a method
     * @return the sum of x and y
     */
    public int getValue() {
        return x;
    }

    public void setValue(int num) {
        if (num != 0 && num != 1) {
            System.out.println("Error Must be either 1 or 0");
            System.out.println("Old Value Retained");
        } else {
            x = num;
        }
    }

    public String toString() {
        return Integer.toString(x);
    }

    public Digit add(Digit b) {
        int returnInt = getValue() + b.getValue();
        Digit carry = new Digit(0);
        if (returnInt == 2) {
            carry = new Digit(1);
            setValue(0);
        } else if (returnInt == 1) {
            carry = new Digit(0);
            setValue(1);
        } else if (returnInt == 0) {
            carry = new Digit(0);
            setValue(0);
        }
        return carry;
    }

    public Digit add(Digit b, Digit c) {
        int returnInt = getValue() + b.getValue() + c.getValue();
        Digit carry = new Digit(0);
        if (returnInt == 2) {
            carry = new Digit(1);
            setValue(0);
        } else if (returnInt == 1) {
            carry = new Digit(0);
            setValue(1);
        } else if (returnInt == 0) {
            carry = new Digit(0);
            setValue(0);
        } else if (returnInt == 3) {
            carry = new Digit(1);
            setValue(1);
        }
        return carry;
    }
}

【问题讨论】:

  • 拥有 Digit 类来强制输入 1 和 0 不一定是坏事,但是在 Digit 类上添加方法并没有多大意义,因为你真的不能处理进位位。我将专注于仅在 DNumber 级别处理 add 并将第 0 个元素设为 0 位可能会使事情变得更容易

标签: java math arraylist binary


【解决方案1】:

考虑一下对你的构造函数的这个小改动。

while (num > 0){
    Digit bin = new Digit(num%2);
    num /= 2;
    binary.add(bin);
}

起初这可能看起来很奇怪,new DNumber(6) 为您提供了一个列表 (0,1,1),它看起来是倒数,但使用起来更容易。

您可以轻松地将其转换为小数:

    public int toDecimal() {
        int total = 0;
        int power = 1;
        for (int i = 0; i < binary.size(); i++) {
            total += binary.get(i).getValue() * power;
            power *= 2;
        }
        return total;
    }

当您从第 0 个进位为 0 的元素开始添加时,如果一个数组比另一个数组长,则更容易处理。

考虑添加 6 和 4

carry = 0
a = (0,1,1)
b = (0,0,1)
answer = ()

一开始是0+0+0 = 0,进位0

carry=0
a = (1,1)
b = (0,1)
answer = (0)

下一个交互,0+1+0 = 1,进位0

carry=0
a = (1)
b = (1)
answer = (0,1)

下一次迭代,0+1+1 = 0,进位1

carry=1
a = ()
b = ()
answer = (0,1,0)

下一次迭代两个输入列表都是空的,所以我们只添加进位位

answer = (0,1,0,1) 如果你通过 toDecimal 是 10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    相关资源
    最近更新 更多