Java中缀计算器逻辑

时间:2015-04-08 22:00:15

标签: java logic calculator

我无法弄清楚动态中缀计算器的逻辑。我能够容纳5个元素的字符串值,例如" 1 + 1"但是我无法计算超过5个元素的字符串(即:" 1 + 2 + 3 + 4&#34 ;)

这是我的过程

import java.util.StringTokenizer;

public static int calculate(String input)     {         int lhs = 0;         int rhs = 0;         int total = 0;         char操作=' &#39 ;;

    int intOne, intTwo;

    StringTokenizer st = new StringTokenizer(input);

    /*
     * this block is chosen if there are no operations
     */

    // block of if statement code for inputs less than or equal to
    // 5 characters.

        /*
         * this block generates the correct number if there is more than
         * one operator in the equation.
         */

    }else if(input.length() > 5){
        int firstValue = 0;
        int latterValue = 0;

        while(st.hasMoreTokens()){
            /*
             * method that assigns the left and right sides
             */

            //assigns values to the first equation
            int firstToken = Integer.parseInt(st.nextToken());
            String opToken = st.nextToken();
            int latterToken = Integer.parseInt(st.nextToken());

            //returns a value for the first equation
            firstValue = assignSides(firstToken, opToken, latterToken);

            // takes in the next operator
            if(st.nextToken().equals("+")){
                operation = '+';
            }else if(st.nextToken().equals("-")){
                operation = '-';
            }else if(st.nextToken().equals("*")){
                operation = '*';
            }else if(st.nextToken().equals("/")){
                operation = '/';
            }

            // assigns values to the latter equation
            firstToken = Integer.parseInt(st.nextToken());
            opToken = st.nextToken();
            latterToken = Integer.parseInt(st.nextToken());

            //returns a value for the latter equation
            latterValue = assignSides(firstToken, opToken, latterToken);

            /*
             * decides how to add the two equations
             */
            switch(operation){
            case '+': total = firstValue + latterValue;
            break;
            case '-': total = firstValue - latterValue;
            break;
            case '*': total = firstValue * latterValue;
            break;
            case '/': total = firstValue / latterValue;
            break;
            default: System.out.println("cannot compute");
            break;
            }

            if(st.hasMoreTokens()){
                //makes the total the first value
                total = firstValue; 

                if(st.nextToken().equals("+")){
                    operation = '+';
                }else if(st.nextToken().equals("-")){
                    operation = '-';
                }else if(st.nextToken().equals("*")){
                    operation = '*';
                }else if(st.nextToken().equals("/")){
                    operation = '/';
                }
            }
        }
    }
    return total;
}

public static int assignSides(int firstToken, String opToken, int latterToken)
{
    int lhs=0;
    int rhs = 0;
    int sum = 0;
    char operation = ' ';

    /*
     * converts the string into a character
     */
    if(opToken.equals("+")){
        operation = '+';
    }else if(opToken.equals("-")){
        operation = '-';
    }else if(opToken.equals("*")){
        operation = '*';
    }else if(opToken.equals("/")){
        operation = '/';
    }

    rhs = latterToken;

    /*
     * interprates the character as a function
     */
    switch(operation){
    case '+': sum = lhs + rhs;
    break;
    case '-': sum = lhs - rhs;
    break;
    case '*': sum = lhs * rhs;
    break;
    case '/': sum = lhs / rhs;
    break;
    default: System.out.println("cannot compute");
    break;
    }

    return sum;
}

我可以帮助我解决逻辑中的错误吗?

1 个答案:

答案 0 :(得分:1)

当计算超过3个符号(不计算空格)时,如“1 + 2 + 3”,
你必须按这个顺序计算:1 +(2 + 3)。

您必须拆分前1个和剩余部分“2 + 3”,然后再将剩余部分传递给calculate方法。类似的东西:

int firstPart = ...; // evaluation of "1"
int secondPart = calculate("2 + 3");

int total = firstPart + secondPart;