Java 中缀计算器逻辑

Java infix calculator logic

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

这是我的流程

import java.util.StringTokenizer;

public static int calculate(字符串输入) { int lhs = 0; 整数 rhs = 0; 总计 = 0; 字符运算 = ' ';

    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;
}

我能得到帮助解决我的逻辑错误吗?

计算超过 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;