简体   繁体   English

用字符串解析数学表达式

[英]Parsing Mathematical Expressions with Strings

I have been trying to find a solution for this for days now and I cannot figure this out. 几天来我一直在努力寻找解决方案,但我无法解决。 I am writing a program that (at one point) takes in mathematical expressions and displays the answer. 我正在编写一个程序(在某一时刻)采用数学表达式并显示答案。 I am using the beanshell parser to do this, but the evaluation isn't the problem. 我正在使用beanshell解析器来执行此操作,但是评估不是问题。 When the user presses the "undo" button, this method is supposed to undo the last input (either an operation (+ - * /) or a number. It gives me errors at the strangest time and I can't figure out why. Can anybody help? I would like to thank anybody who helps in advance! 当用户按下“撤消”按钮时,该方法应撤消最后的输入(一个操作(+-* /)或一个数字。它在最奇怪的时间给我错误,我不知道为什么)。任何人都可以帮忙吗?我要感谢任何事先提供帮助的人!

public void undo(View v) throws EvalError{
    Interpreter interpreter1 = new Interpreter(); // interpreter to evaluate user solution
    userExpressionList.remove(userExpressionList.size()-1); // remove last element of userExpressionList
    String tempExp3 = "";
    if (userExpressionList.size() != 0){
        for (String element:userExpressionList) {
            tempExp3 = tempExp3 + element;
            if (tempExp3.substring(tempExp3.length() - 1).equals("+") || tempExp3.substring(tempExp3.length() - 1).equals("-") ||
                tempExp3.substring(tempExp3.length() - 1).equals("*") || tempExp3.substring(tempExp3.length() - 1).equals("/")) {
                    displaySolution = (Double)interpreter1.eval(tempExp3.substring(0, tempExp3.length() - 2));
                    userSolution = tempExp3.substring(tempExp3.length() - 1);
                }
            else {
                displaySolution = (Double) interpreter1.eval(tempExp3.substring(0, tempExp3.length() - 1));
                userSolution = "";
                }
            Log.i("tempExp3", tempExp3);
            Log.i("displaySolution", displaySolution.toString());
        }
        textViewUserSolution.setText(displaySolution.toString());
    }
    else {
        clear(findViewById(R.id.clearButton));
        textViewUserSolution.setText("");
    }
    Log.i("isExpectingNumber before invert", String.valueOf(isExpectingNumber));
    isExpectingNumber = !isExpectingNumber;
    Log.i("isExpectingNumber after invert", String.valueOf(isExpectingNumber));
    textViewUserExpression.setText(tempExp3);
}

If you need any more information, please ask. 如果您需要更多信息,请询问。 I really appreciate any help you guys can offer. 非常感谢你们能提供的任何帮助。

The problem is very likely based on the assumption that "going back" character by character will always result in something that can be evaluated without a problem. 该问题很有可能基于这样的假设:一个字符一个字符地“返回”将始终导致可以毫无问题地进行评估的某些事物。

Consider: 考虑:

1+23 - you can undo '3' and eval "1+2"
1+2  - you can undo '2' but you can't eval "1+", just "1"
1+2+ - you can undo '+' and eval "1+2"

Therefore, 因此,

switch(exp3.charAt(exp3.length()-1)){
case '+': case '-': case '*': case '/':
    // 1+2+
    sol = int.eval( exp3.substring( 0, exp3.length()-1 ) ); // not -2!       
    usrSol = exp3.substring(exp3.length() - 1);
    break;
default:
    if( exp3.length() > 2 ){
        char d = exp3.charAt(exp3.length()-2);
        if( '0' <= d && d <= '9' ){
             // 1+23
             sol = int.eval(exp3.substring(0, exp3.length() - 1));
        } else {
             // 1+2
             sol = int.eval(exp3.substring(0, exp3.length() - 2));
        }
    } else {
        // "12"
        sol = int.eval(exp3.substring(0, exp3.length() - 1));
    }
    usrSol = "";
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM