java - Java中字符串数字的加法和减法

标签 java string addition subtraction

我应该创建一个程序,用户输入两个数字,可以是负数或正数,并且可以包含或不包含小数位。理论上,当您添加“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;
    }
}

最佳答案

将两个数字作为字符串,并将符号存储到符号字符串中相应的 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
 }
}

关于java - Java中字符串数字的加法和减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25699772/

相关文章:

java - Confluence 宏插件显示已安装,但宏不起作用

指针上的 C++ 子串

python 在没有正则表达式的情况下在多个分隔符上拆分字符串

java - 从构造函数填充 jPanel

python - 多维数组的递归矩阵元素加法

java - 随机输出图像

java - ParameterizedTypeReference 的正确用法

java - 在 LinkedHashMap 中插入值而不使用键

c++ - 在 C++ 中旋转数组的最后 n 个元素

c - 有条件地添加数字