0

スタックを作成するために try ブロックの内部を取得できないようです。try ブロック内のスタックを使用して、1 桁の電卓を取得することを意図しています。try ブロックの内部で emptystackexception が発生しています

import java.util.EmptyStackException;
import java.util.*;

public class Infix
{
    public static void main(String[] args){

        System.out.println(evaluateInfix("1*2*3"));

    }
public static double evaluateInfix(String infix)
    {
        try
        {
            Stack<Character> valueStack = new Stack<Character> ();
            Stack<Character>  operatorStack = new Stack<Character> ();
            double result;
            for(char ch: infix.toCharArray()){
                if(ch >= 0 && ch <= 9){
                    valueStack.push(ch);
                } else if(ch == '('){
                    operatorStack.push(ch);
                } else if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '^' || ch == '='){
                    if(operatorStack.isEmpty()){
                        operatorStack.push(ch);
                    } else if(precedence(ch) > precedence(operatorStack.peek())){
                        operatorStack.push(ch);
                    } else {
                        while(!operatorStack.isEmpty() && precedence(ch) <= precedence(operatorStack.peek())){
                            result = compute(valueOf(valueStack.pop()), valueOf(valueStack.pop()), operatorStack.pop());
                            valueStack.push((char)result);
                        }
                        operatorStack.push(ch);
                    }
                } else if(ch == ')'){
                    while(operatorStack.peek() != '('){
                        result = compute(valueOf(valueStack.pop()), valueOf(valueStack.pop()), operatorStack.pop());
                        valueStack.push((char)result);
                    }
                    operatorStack.pop();
                }
            }

            while(!operatorStack.isEmpty()){
                result = compute(valueOf(valueStack.pop()), valueOf(valueStack.pop()), operatorStack.pop());
                valueStack.push((char)result);
            }
            result = valueStack.peek();

            System.out.println(result);


            /* **********
            Task 3
            complete this section to calculate the infix expression
            */        



        } //end try
        catch (EmptyStackException e)
        {
            /* **********
            Task 3
            complete this to return Double.NaN
            */            
        }
        catch (ArithmeticException e)
        {
            /* **********
            Task 3
            complete this to return Double.NEGATIVE_INFINITY
            */            
        }
        return 3.0;

    } // end evaluateInfix

    private static int precedence(char operator)
    {
        switch(operator){
            case '(': case ')': return 0;
            case '+': case '-': return 1;
            case '*': case '/': case '%': return 2;
            case '^': return 3;
            case '=': return 4;
        }
        return -1;
    }

    private static double valueOf(char variable)
    {
        switch (variable)
        {
            case '1': return 1.0;
            case '2': return 2.0;
            case '3': return 3.0;
            case '4': return 4.0;
            case '5': return 5.0;
            case '6': return 6.0;
            case '7': return 7.0;
            case '8': return 8.0;
            case '9': return 9.0;
            case '0': return 0.0;
        } // end switch

        return 0;
    } // end valueOf

    private static Double compute(Double operandOne, Double operandTwo, char operator)
    {
        if(operator == '+'){
            return operandOne + operandTwo;
        } else if(operator == '-'){
            return operandOne - operandTwo;
        } else if(operator == '*'){
            return operandOne * operandTwo;
        } else if(operator == '/'){
            return operandOne / operandTwo;        
        } else if(operator == '%'){
            return operandOne % operandTwo;
        } else if(operator == '^'){
            return Math.pow(operandOne, operandTwo);
        } else {
            return null;
        }
    } // end compute
} // end Infix
4

2 に答える 2

1

"}" と "{" の冗長なペアがあり、コードの大部分を初期化ブロック (基本的には Infix クラスのコンストラクター) に変換します。

(あなたのコメントに基づく) - メソッド宣言がありません。

最初の数行を次のように変更してみてください。

public class Infix {
  public static void main(String[] args){
    System.out.println(evaluateInfix("1*2*3"));
  }

  {
    try {
      ...

に:

public class Infix {
  public static void main(String[] args){
    System.out.println(evaluateInfix("1*2*3"));
  }

  public static double evaluateInfix(String infix) {
    try {
      ...
于 2013-02-11T12:01:25.570 に答える