【问题标题】:Adding and subtracting string numbers in JavaJava中字符串数字的加减法
【发布时间】:2014-09-06 11:29:56
【问题描述】:

我应该创建一个程序,用户可以在其中输入两个数字,这两个数字可以是负数,也可以是正数,并且可以包含或不包含小数位。从理论上讲,当您添加说“256.78 + 78.6783”时,它应该像正常的添加问题一样携带一个并完成操作。

我已经想出了如何添加任何长度的数字,只有当它们是正数时才会让我永远,但是当我添加负数甚至减去数字时,我没有得到正确的结果。这应该适用于用户输入的任何两个数字的集合。

到目前为止,这是我的代码,有什么建议吗?
附言在操作之前,我不允许将这些数字转换为 intdouble,因此解析它们是不可能的。

public class Number {
    static Scanner kbd = new Scanner (System.in);
    private String sign; 
    private String whole;
    private String decimal;
    private String fraction;
    private static double firstNumber;
    private static double secondNumber;

    public static void main(String[] args) {

        System.out.println("Please enter the first number: ");
        firstNumber = kbd.nextDouble();
        System.out.println("Next, enter the second number: ");
        secondNumber = kbd.nextDouble();

        Number x = new Number (firstNumber);
        Number y = new Number (secondNumber);
        Number sum = x.add(y);
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("x + y = " + sum);
        Number subtract = x.subtract(y);
        System.out.println("x - y = " + subtract);
    }
    public Number() 
    {
        whole = "0";
        decimal = "0";
        sign = "+";
    }
    public String toString() 
    {
        return sign + whole + "." + decimal;
    }
    public Number (double n) 
    {
        whole = "0";
        decimal = "0";
        sign = "+";

        String numString = new Double(n).toString();
        if (numString.charAt(0) == '-') {
            sign ="-";
            numString = numString.substring(1);
        }
        int position = numString.indexOf(".");
        if (position == -1)
            whole = numString;
        else
        {
            whole = numString.substring(0,position);
            decimal = numString.substring(position+1);
            fraction = "";
        }
    }

    public Number add (Number RHS) {
        this.fixWhole (RHS);
        this.fixDecimal(RHS);
        return this.addNum (RHS);
    }
    public Number subtract (Number RHS) {
        this.fixWhole(RHS);
        this.fixDecimal(RHS);
        return this.subtractNum (RHS);
    }
    private void fixWhole (Number RHS) {
        int firstWholeNum = this.whole.length();
        int secondWholeNum = RHS.whole.length();
        int difference = firstWholeNum - secondWholeNum;
        if (difference > 0) {
            for (int i = 1; i <= difference; i++) 
                RHS.whole = "0" + RHS.whole;
        }
        else if (difference < 0 ) {
            difference = Math.abs(difference);
            for (int i = 1; i <= difference; i++)
                this.whole = "0" + this.whole;
        }
    }

    private void fixDecimal  (Number RHS ) {
        int firstDecimalNum = this.decimal.length();
        int secondDecimalNum = RHS.decimal.length();
        int difference = firstDecimalNum - secondDecimalNum;

        if (difference > 0) {
            for (int i = 1; i <=  difference; i++)
                RHS.decimal = RHS.decimal + "0";
        }
        else if (difference < 0 ) 
        {
            difference = Math.abs(difference);
            for (int i = 1; i <= difference; i ++)
                this.decimal = this.decimal + "0";
        }
    }

    private Number addNum (Number RHS ) {
        Number sum = new Number();
        sum.decimal = "";
        int carry = 0;
        int decimalNum = this.decimal.length();
        for (int i = decimalNum - 1; i >= 0; i --) {
            char c1 = this.decimal.charAt(i); 
            char c2 = RHS.decimal.charAt(i); 
            int tempSum= (c1 - 48) + (c2 - 48) + carry;
            carry =  tempSum/ 10;
            int sumDigit = tempSum % 10;
            sum.decimal = (char) (sumDigit + 48) + sum.decimal;
        }
        sum.whole = "";
        int wholeNum = this.whole.length();
        for (int i = wholeNum - 1; i >= 0; i --) {
            char c1 = this.whole.charAt(i);
            char c2 = RHS.whole.charAt(i);
            int tempSum = (c1 - 48) + (c2 - 48 ) + carry;
            carry = tempSum / 10;
            int sumDigit = tempSum % 10;
            sum.whole = (char) (sumDigit + 48) + sum.whole;
        }
        if (carry != 0) 
            sum.whole = "1" + sum.whole;
        return sum;
    }

    private Number subtractNum (Number RHS ) {
        Number sum = new Number();
        sum.decimal = "";
        int carry = 0;
        int decimalNum = this.decimal.length();
        for (int i = decimalNum - 1; i >= 0; i --) {
            char c1 = this.decimal.charAt(i); 
            char c2 = RHS.decimal.charAt(i); 
            int tempSum= (c1 - 48) - (c2 - 48) - carry; 
            carry =  tempSum/ 10;
            int sumDigit = tempSum % 10;
            sum.decimal = (char) (sumDigit - 48) + sum.decimal;
        }
        sum.whole = "";
        int wholeNum = this.whole.length();
        for (int i = wholeNum - 1; i >= 0; i --) {
            char c1 = this.whole.charAt(i);
            char c2 = RHS.whole.charAt(i);
            int tempSum = (c1 - 48) - (c2 - 48 ) + carry;
            carry = tempSum / 10;
            int sumDigit = tempSum % 10;
            sum.whole = (char) (sumDigit + 48) + sum.whole;
        }
        if (carry != 0) 
            sum.whole = "1" + sum.whole;
        return sum;
    }
}

【问题讨论】:

  • 我不确定你的意思是你不能解析它们。您需要以一种或另一种方式解析字符串,事实上在您当前的方法中您已经在解析数字。
  • @for3st:不完全正确。最终,是的,在 some 点,字符串输入需要转换为“数字”,如果只是因为需要添加/减去它。但是 OP 的任务是显然一次一个字符地这样做。批量转换为浮动时并非如此。这项任务的目的似乎是从头开始设计 BigNum 功能。
  • (char) (sumDigit - 48) + sum.decimal;+ 一起使用会更好:sumDigit + 48。 (为了清楚起见,您也可以使用sumDigit + '0')。您还可以提供减法算法英文(不是java)吗?您的减法实现显然有问题。
  • How did you do this in elementary school? 每个+- 操作都可以转换为2个正值的加法或2个正值的减法,其中被减数不小于减数,通过改变迹象。 (如果您更改符号,则需要确保结果具有正确的符号)

标签: java string add subtraction


【解决方案1】:

将两个数字都作为字符串,并将符号存储到符号字符串中相应的 Numbers 对象中,然后调用您的方法

System.out.println("Please enter the first number: ");
        firstNumber = kbd.nextString();
        System.out.println("Next, enter the second number: ");
        secondNumber = kbd.nextString();

        Number x = new Number (firstNumber.substring(1),firstNumber.charAt(0));
        Number y = new Number (secondNumber.substring(1),secondNumber.charAt(0));

/*convert the firstNumber.substring(1) and secondNumber.substring(1) to doubles using  Double.parseDouble()*/

public String doTheOperation(Number other){
 if(this.sign.equals(otherNumber.sign)){
  /*simply the double values and put the sign*/ in front of it and return it
 }

 else{
  do the simple double subtraction and by looking at your code i believe you can find out the bigger double among them
 }
}

【讨论】:

  • 嗨,我不应该使用 parse 来进行操作,我的任务是逐个字符地进行算术运算。另外,您的意思是输入检查是否用户输入负数我调用对负数进行操作的方法?
  • 如果你不能,你能找出哪个数字更大,哪个更小吗?您可以找出较大的符号,因此如果符号不同,您只需专注于符号,从较大的符号中减去较小的符号
  • 实际上,不,我不知道该怎么做。你用循环做点什么吗?我真的被困住了,任何帮助将不胜感激!
  • ok 将数字读取为字符串,正如我在答案中所示,所以现在要获得更大的数字,请按照步骤 1.let 说你有两个数字作为 n1 和 n2 所以现在检查位数在这一点之前,所以你会知道哪个数字的数字越少,如果数字相同,那么从头开始检查第一个数字是否相同,寻找第二个数字,依此类推。
  • 当我这样做时,我得到一个错误,说我不能子串一个 double,因为我的数字是双倍的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
相关资源
最近更新 更多