2013-12-18 29 views
0

我目前正在用Java編寫一個字符串計算器。現在我試圖使它與負數一起工作(例如,輸入-3 + 7時,結果爲4)。目前,當輸入-3 + 7時,它將返回-4,因爲它看到 - 是第一個運算符,所以它將對這些數字執行減法。在calcOperators列表中,我添加了所有操作符,而在calcOperands中,我添加了所有數字並將它們存儲爲整數。我的問題如何讓程序接受-1或-2作爲一個整數,而不是以 - 作爲運算符,2作爲數字。我試圖使用numberformatexception,但不起作用。如果有人能幫助,我會感激的。如何在Java中使用負數來創建字符串計算器?

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 

} 
} 
+0

它將對兩個給定數字的操作是的,但我所試圖做的是,當我在編號前的負號,接受它作爲一個整體,而不是作爲一個經營者和一個數字。 – Philip

+0

'( - ?)(\\ d +)(+ | - | * | /)( - ?)(\\ d +)'這可能是一個有用的正則表達式 – Cruncher

回答

0

簡單修復和立即修復是,更改ParseInput接受位置並傳遞字符位置。並在添加到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 
} 
2

整個事情依賴於您的語言設計。 (它看起來像你甚至不知道你處理的語言,但請相信我,這是一個。)

你已經基本上2種選擇:

  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 

,的解釋「 - 」會有所不同,這取決於您是否尋找一個一元運算符或binary_operator。請注意,示例語法是以這種方式製作的,不會造成混淆。例如,一開始的' - '只能是一元運算符,但在一個數字或一個完整的子表達式之後出現的' - '必須是二元運算符(並且在該運算符之後必須遵循另一個表達式)。

+0

我的語言的定義是什麼意思?我正在使用java/ – Philip

+1

@Philip不是編程語言。語言由一組有效的輸入及其輸出定義。就你而言,你需要一個明確定義的語言,說明每個符號在何處以及如何表示 – Cruncher

+0

@Philip http://en.wikipedia.org/wiki/Formal_language – Cruncher

相關問題