Comment faire fonctionner une calculatrice de chaîne en Java avec des nombres négatifs?


Je fais actuellement une calculatrice de chaîne en Java. Maintenant, j'essaie de le faire fonctionner avec des nombres négatifs(par exemple en tapant -3+7, le résultat est 4). Actuellement, en tapant -3 + 7, il renverra -4, car il voit-être le premier opérateur, il effectuera donc une soustraction sur ces nombres. Dans la liste calcOperators, j'ajoute tous les opérateurs, tandis que dans calcOperands, j'ajoute tous les nombres et les stocke sous forme d'entiers. Ma question comment puis-je faire accepter le programme -1 ou -2 dans son ensemble nombre au lieu de prendre - en tant qu'opérateur et 2 en tant que nombre. J'ai essayé d'utiliser numberformatexception, mais ne fonctionne pas. Si quelqu'un peut aider, je serai reconnaissant.

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

}
}
Author: piet.t, 2013-12-18

2 answers

Correction simple et correction immédiate est, Changer le ParseInput pour accepter la position et passer la position char. et vérifiez la position avant d'ajouter aux calcOperators.

public boolean parseInput(char character,int position){ //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
        if (position !=0 ){
            calcOperators.add(character);
            System.out.println(calcOperators);
            System.out.println(character);
            string = string.substring(0, position) + " " + string.substring(position+1); //The new input now contains the operands, the operators are being removed
        }
        return true; //Return true if the character is an operator
    }
    return false; //If the character is not an operator, return false
}
 0
Author: Mani, 2013-12-18 14:34:57

Le tout repose sur la conception de votre langue. (On dirait que vous ne savez même pas que vous traitez une langue, mais croyez-moi, c'est l'un.)

Vous avez essentiellement 2 choix:

  1. Dans "-5", vous voyez la partie du nombre.
  2. Vous voyez le - comme opérateur unaire.

Dans les deux cas, vous devez savoir quoi chercher. Je vais assumer le deuxième scénario, car il vous permettra d'introduire d'autres opérateurs unaires. Donc, votre grammaire des langues serait regardez comme:

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

Maintenant, l'interprétation d'un " - " sera différent selon que vous regardez pour un opérateur unaire ou un binary_operator. Notez que l'exemple de grammaire est conçu de telle manière qu'aucune confusion n'est possible.

Par exemple, un " - "au début ne peut être qu'un opérateur unaire, mais un" - " qui s'affiche après un certain nombre ou une sous-expression doit être un opérateur binaire (et une autre expression à suivre après que).

 2
Author: Ingo, 2013-12-18 14:36:26