2014-02-25 68 views
0

我正在Java中为一个项目构建一个堆栈,到目前为止我已经做了简单的计算,但是我想添加括号以允许更复杂的计算。下面是目前的代码。请记住,我还没有为栈中的运算符设置优先级,并且代码还没有那么原始。任何你能给的帮助都会很棒。使用括号的堆栈计算器

import java.util.*; 

public class CalcEngine { 
String total = ""; 
int op1, op2, size, value1, value2; 
char operator; 
int displayValue, operand1; 
boolean done = false; 
Deque<Character> stack = new ArrayDeque<Character>(); 
ArrayList<Integer> numbers = new ArrayList<Integer>(); 

/** 
* Create a CalcEngine instance. Initialise its state so that it is ready 
* for use. 
*/ 
public CalcEngine() { 
    operator = ' '; 
    displayValue = 0; 
    operand1 = 0; 
} 

/** 
* Return the value that should currently be displayed on the calculator 
* display. 
*/ 
public String getDisplayValue() { 
    return (total); 
} 

/** 
* A number button was pressed. Do whatever you have to do to handle it. The 
* number value of the button is given as a parameter. 
*/ 
public void numberPressed(int number) { 
    displayValue = displayValue * 10 + number; 
    total += number; 
} 

/** 
* The 'plus' button was pressed. 
*/ 
public void plus() { 
    operand1 = displayValue; 
    displayValue = 0; 
    operator = '+'; 
    total += " + "; 
} 

/** 
* The 'minus' button was pressed. 
*/ 
public void minus() { 
    operand1 = displayValue; 
    displayValue = 0; 
    operator = '-'; 
    total += " - "; 
} 

public void multiply() { 
    operand1 = displayValue; 
    displayValue = 0; 
    operator = '*'; 
    total += " * "; 
} 

public void divide() { 
    operand1 = displayValue; 
    displayValue = 0; 
    operator = '/'; 
    total += "/"; 
} 

/** 
* The '=' button was pressed. 
*/ 
public void equals() { 

    stack(); 
} 

/** 
* The 'C' (clear) button was pressed. 
*/ 
public void clear() { 
    displayValue = 0; 
    operand1 = 0; 
    total = ""; 
} 

/** 
* Return the title of this calculation engine. 
*/ 
public String getTitle() { 
    return ("My Calculator"); 
} 

/** 
* Return the author of this engine. This string is displayed as it is, so 
* it should say something like "Written by H. Simpson". 
*/ 
public String getAuthor() { 
    return ("T.Tubbritt"); 
} 

/** 
* Return the version number of this engine. This string is displayed as it 
* is, so it should say something like "Version 1.1". 
*/ 
public String getVersion() { 
    return ("Ver. 1.0"); 
} 

public boolean isNumber(String total) { 
    try { 
     int y = Integer.parseInt(total); 
     return true; 
    } catch (NumberFormatException e) { 
     return false; 
    } 

} 

public void stack() { 
    String outputStream; 

    StringTokenizer st = new StringTokenizer(total); 

    while (st.hasMoreTokens()) { 
     String c = st.nextToken(); 

     if (isNumber(c)) { 
      numbers.add(Integer.parseInt(c)); 
     } else { 
      stack.addFirst(c.charAt(0)); 
     } 

    } 

    System.out.println(stack.getFirst()); 

    while (stack.size() != 0) { 

     switch (stack.getFirst()) { 
     case '*': 
      size = numbers.size(); 
      value1 = numbers.get(size - 1); 
      value2 = numbers.get(size - 2); 

      numbers.set(size - 2, value1 * value2); 
      stack.pop(); 
      numbers.remove(size - 1); 
      continue; 

     case '+': 
      size = numbers.size(); 
      value1 = numbers.get(size - 1); 
      value2 = numbers.get(size - 2); 
      numbers.set(size - 2, value1 + value2); 
      stack.pop(); 
      numbers.remove(size - 1); 
      continue; 

     case '-': 
      size = numbers.size(); 
      value1 = numbers.get(size - 1); 
      value2 = numbers.get(size - 2); 
      numbers.set(size - 2, value2 - value1); 
      stack.pop(); 
      numbers.remove(size - 1); 
      continue; 
     } 

    } 
    total += " = " + numbers.get(size - 2); 
} 
} 
+0

这里有关于添加括号的具体问题吗? – digitaljoel

+0

我只是寻找一些关于如何在代码中实现括号的建议。 – user2175076

回答

0

您的堆栈当前在整数。我相信你需要使它更通用。它将由操作数和操作符组成。一个操作数将有一个eval方法或将返回评估结果的东西。对于像5这样简单的操作数,它将返回值5。对于像(3 3 +)这样的复杂操作数,它将返回评估结果,该结果将为6。鉴于这样的信息:

(3×3 +)5 *

会被解析成:

Operand<3 3 +> Operand<5> Operator<*> 

,如果你愿意,你可以使用这将进一步被解析成

Operand<Operand<3> Operand<3> Operator<+>> Operand<5> Operator<*> 
0

我的代码,我已经实现它,而不使用堆栈或队列...我已经使用递归来解决它...检查下面的代码:

public class Calculator { 

    public static void main(String[] args) { 
     String input = "2*((2+4)+(5+4))"; 
     System.out.println(evaluate(input)); 
     //System.out.println(Character.digit('2', Character.MAX_RADIX)); 
    } 

    private static double evaluate(String expresion) { 
     double result = 0; 
     String operation = ""; 
     List<Character> openBrackets = new ArrayList<Character>(); 
     List<Character> closeBrackets = new ArrayList<Character>(); 
     StringBuilder innerInput =new StringBuilder(); 

     for (int i = 0; i < expresion.length(); i++) { 
      char inputChar = expresion.charAt(i); 
      if(openBrackets.isEmpty()){ 
       if (Character.isDigit(inputChar)) { 

        if (operation == "" && result == 0) { 
         result = Character.digit(inputChar, Character.MAX_RADIX); 
         continue; 
        } else if (operation != "") { 
         result = calculateWithOperation(operation, Character.digit(inputChar, Character.MAX_RADIX), result); 
         continue; 
        } 
       } 
       // if the input is operation then we must set the operation in order 
       // to be taken into consideration again .. 
       if (inputChar == '+' || inputChar == '-' || inputChar == '*' || inputChar == '/') { 
        operation = Character.toString(inputChar); 
        continue; 
       } 
      } 
      if (inputChar == '(') { 
       // set operation to be empty in order to calculate the 
       // operations inside the brackets .. 
       openBrackets.add(inputChar); 
       continue; 
      } 
      if(inputChar ==')'){ 
       closeBrackets.add(inputChar); 
       if(openBrackets.size() == closeBrackets.size()){ 
        openBrackets.remove((Character)'('); 
        closeBrackets.remove((Character)')'); 
        double evalResult = evaluate(innerInput.toString()); 
        result = calculateWithOperation(operation,evalResult,result); 
        innerInput.setLength(0); 
       } 
       if(openBrackets.size()> closeBrackets.size()){ 
        continue; 
       } 
       //break; 
      } 
      else{ 
       innerInput.append(inputChar); 
      } 
     } 
     return result; 
    } 

    /** 
    * this method to calculate the simple expressions 
    * @param operation 
    * @param inputChar 
    * @param output 
    * @return 
    */ 
    private static double calculateWithOperation(String operation, double inputChar, double output) { 
     switch (operation) { 
     case "+": 
      output = output + inputChar; 
      break; 

     case "-": 
      output = output - inputChar; 
      break; 

     case "*": 
      output = output * inputChar; 
      break; 

     case "/": 
      output = output/inputChar; 
      break; 

     default: 
      break; 
     } 
     return output; 
    } 
}