java - 如何使Java中的字符串计算器能够处理负数?

标签 java regex numbers calculator

我目前正在用 Java 制作一个字符串计算器。现在我正在尝试使其适用于负数(例如,当输入 -3+7 时,结果为 4)。目前,当输入 -3+7 时,它将返回 -4,因为它认为 - 是第一个运算符,因此它将对这些数字执行减法。在 calcOperators 列表中,我添加所有运算符,而在 calcOperands 中,我添加所有数字并将它们存储为整数。我的问题是如何让程序接受 -1 或 -2 作为一个整数,而不是将 - 作为运算符,将 2 作为数字。我尝试使用 numberformatexception,但不起作用。如果有人能提供帮助,我将不胜感激。

import java.util.ArrayList; //Importing the ArrayList module for creating arrays in the class
import java.util.regex.Pattern;

public class stringCalculator {

private String string; //Declaring the string 'string' as a field in order to be able to use it
private ArrayList<Integer> calcOperands; //The list that will hold the operands as Integers
private ArrayList<Character> calcOperators; //The list that will hold the operators as characters
private ArrayList<Character> priorityList; //This list holds characters, 1 for multiplication and division sign and 2 for addition and substraction respectively

private int result;//An integer variable, that will be the result from the expression

public stringCalculator(String string){ //A constructor
    this.string = string; //this. is used for updating the variable
    /**
     * Creating the two lists for holding the operands and operators
     */
    calcOperands = new ArrayList<Integer>();
    calcOperators = new ArrayList<Character>();


}

public boolean checkInput(){ //This method checks whether the input of the user contains integers and/or operators

    for(int i =0; i<string.length();i++){ //A for loop that will check each individual character from the expression
        if(!validCharacter(string.charAt(i)) && !parseInput(string.charAt(i))){// If one of the characters is not integer, white space or operator, return false
            return false;
            }
    }
    String[] items = string.split(" "); // Creating a new list items, that will store strings from the input
    System.out.println(string);
    //System.out.println(items[0]);
    for(int count2 = 0;count2<items.length;count2++){ //For loop for adding the operands in the items list

        if(!items[count2].equals("") || items[count2].contains("-")){ //If the element from the input is not an empty string, add it to the items list
            calcOperands.add(Integer.parseInt(items[count2]));
            System.out.println(items[count2]);
            System.out.println(calcOperands);
        }
        //else if(items[count2-1]=="-" &&){

        //}
    }
    return true; //Return true if all the elements in the input are either integers, white spaces or operators
}

private boolean validCharacter(char character){ //Checks the validity of every character in the user input
    if(Character.isWhitespace(character)){ //If there is a white space character, return true
        return true;
    }
    if(Character.isDigit(character)){ //If the character is a digit(integer), return true as well
        return true;
    }

    //String num = "-2";
    //if(string[0]=="-")){

    //}
    return false; //If there is a character in the input that is neither an integer nor a white space, return false
}
public boolean parseInput(char character){ //The parseInput method splits the input of the user and puts operands in calcOperands list and operators in calcOperators list

    if(character == '+' || character == '-' || character == '*' || character == '/'|| character == '=' ) { //If there is a character that is equal to one of the operators, add it to the calcOperators list
        calcOperators.add(character);
        System.out.println(calcOperators);
        System.out.println(character);
        string = string.substring(0, string.indexOf(character)) + " " + string.substring(string.indexOf(character)+1); //The new input now contains the operands, the operators are being removed

    return true; //Return true if the character is an operator
    }
    else if(character == '-' && Character.isDigit(character+1)){ //This is where I try to make the programme accept -1 as one number
        calcOperands.add(character+1);
        System.out.println(calcOperands);
        string = string.substring(0, string.indexOf(character+1)) + " " + string.substring(string.indexOf(character)+1);
    }

return false; //If the character is not an operator, return false 
}


public void calculatePriority(){ //This method is used to calculate the priority operators, i.e. do the multiplication and division before addition and substraction
    priorityList = new ArrayList<Character>(calcOperators); //Creating the priorityList which will store the characters 1 and 2. It has the same length as the calcOperators list and is the same as it initially

    char i = 0; //A counter variable, that will be used to inspect each element in the prirorityList
    for(char j : priorityList){ //A for loop for inspection each character in the priorityList
    if(j == '*' || j == '/'){ //If there is a multiplicaiton or division operator, replace it with 1 in the priorityList
        priorityList.set(i, (char)'1');
    }
    else if(j == '+' || j =='-'){ //If the operators are addition or substraction, replace them with 2 in the priorityList
        priorityList.set(i, (char)'2');
    }
    i++; //Increment the counter i every time in order to circulate through each element in the list
    }
}

public int calculateResult(){ //This method calculates the result from the user's input and returns the result as an integer
    /**
     * Calling each of the previous methods in this one, so I can just call calculateResult() in the main class. This makes the programme more robust
     */
    checkInput();
    calculatePriority();


    try{
    while(priorityList.contains('1')){ //If the priorityList contains '1' in it
    int k = priorityList.indexOf('1'); //A new integer variable k, that will be equivalent to the first position, at which '1' is found in priorityList 
        if(calcOperators.get(k) == '*' ){ //If the element at position k in calcOperators list is multiplication
            calcOperands.set(k, calcOperands.get(k) * calcOperands.get(k+1)); //Replace the operator sign with the product of the operands between the sign itself

            calcOperands.remove(k+1); //Remove the element after k in calcOperands in order to have just the product in the list
            calcOperators.remove(k); //From calcOperators, remove the "*" for the numbers, on which the operation is performed
            priorityList.remove(k); //In priorityList, remove all the 1s


            result = calcOperands.get(k); //The result variable is now equal to the product of the number
        }
        else if(priorityList.indexOf('1')>=0 && calcOperators.get(k) == '/' ){ //Perform the same operations for the division operation, as we did the multiplication
            calcOperands.set(k, calcOperands.get(k) / calcOperands.get(k+1));
            calcOperands.remove(k+1);
            calcOperators.remove(k);
            priorityList.remove(k);



            result = calcOperands.get(k);
        }
    }

    while(priorityList.contains('2')){ //A loop that circulates when the priorityList contains a '2' in it, that means either an addition or substraction operation
        int g = priorityList.indexOf('2'); //The integer variable g is equivalent to the first position, at which the character '2' is found in the priorityList
        if(calcOperators.get(g) == '+' ){ //If the element in calcOperators is the "+" sign
            calcOperands.set(g, calcOperands.get(g) + calcOperands.get(g+1)); //Replace the operator g with the sum of the numbers between the operator
            calcOperands.remove(g+1); //Remove the element g+1, which is the second number for the operation, in order just the sum of the two numbers to be left in the calcOperands list
            calcOperators.remove(g); //Remove the + operator in the calcOperators list
            priorityList.remove(g); // Remove the '2' character in priorityList

            result = calcOperands.get(g); //The result is now equal to the sum of the numbers

        }
        else if(calcOperators.get(g) == '-'){ //Perform the same operations for the substraction opeartion as we did on the addition. They are both at the same priority, so it does not matter which operation goes fist
            calcOperands.set(g, calcOperands.get(g) - calcOperands.get(g+1));
            calcOperands.remove(g+1);
            calcOperators.remove(g);
            priorityList.remove(g);

            result = calcOperands.get(g);


        }
    }
}
    catch(RuntimeException r){
        //int k = priorityList.indexOf('1');
        //if((((CharSequence) calcOperands).charAt(0))=='-'){
            //calcOperands.set(k, -calcOperands.get(k) * calcOperands.get(k+1));
        //}
    }


    return result; //Return the final result of the input as an integer

}
}

最佳答案

整个事情取决于你的语言的设计。 (看起来你甚至不知道你正在处理一种语言,但相信我,它是一种。)

你基本上有两个选择:

  1. 在“-5”中,您会看到 - 作为数字的一部分。
  2. 您将看到 - 作为一元运算符。

在这两种情况下,您都应该知道要寻找什么。我将假设第二种情况,因为它将允许您引入其他一元运算符。所以你的语言语法将如下所示:

number: ... // a sequence of digits
unary_operator: '-'
binary_operator: '-' | '+' | '*' | '/'
term: number | '(' expr ')'
unaryex: unary_operator term | term
expr: unaryex | unaryex binary_operator expr

现在,“-”的解释会有所不同,具体取决于您查找的是一元运算符还是二元运算符。请注意,示例语法的设计方式不会造成混淆。

例如,开头的 '-' 只能是一元运算符,但出现在数字或完整子表达式之后的 '-' 必须是二元运算符(其后必须跟有另一个表达式) .

关于java - 如何使Java中的字符串计算器能够处理负数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660319/

相关文章:

java - 为什么 While (rs.next()) 语句在第一次迭代后结束?

regex - 使用 grep 访问文件中的单词

regex - "four alphanumerics, but not all digits"的 XSD 正则表达式?

JavaScript 生成不重复的随机数

java - 确定字符串是否为数字并在Java中转换?

java - Selenium 中的线程 "main"java.lang.NullPointerException 中出现异常

java - 如何在OpenCV中获取由四个点定义的四边形内部的像素?

java - Android MapView - 扩展 OverlayItem 以存储用于在 onTap 中访问的 URL

regex - 寻找匹配 `' 时意外的 EOF '

Python 多文件内容