I have a lot of problems trying to do this for some reason. I have a class that wants me to evaluate a complex Java expression such as (3 + 5 [3 * 2-4]) using recursion. I think I have an idea about how I want to approach it, but I can’t understand how to solve something really simple - how
5-2 * 10
I do not know how to do that. They do not allow you to import external scripts, and you cannot convert them to a postfix expression.
I don’t expect anyone to write me the code, but if someone can send me in the right direction or give me some psuedocode, I would really appreciate it - I spent two hours to no avail trying to figure out how I can use string tokenizers and other things to solve it, but I always run into a wall in which I don't know how to get around. Thank you very much in advance!
You can sequentially reduce subexpressions (so-called "redexes") until more reductions are possible.
This replacement of internal expressions can be done using regular expressions:
. . "", "", "".
, .
.
, , . , , . , , , top-down , , , , . , , , , , , , .
, -up . , , , , - , , " " , .
, , OCaml, .
, , !
, . , . .
, "5-2 * 10", :
import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException;
, "", , 5-2 * 10 TextField:
ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); String r = jTextField1.getText(); try { jTextField1.setText(engine.eval(r).toString()); } catch (ScriptException ex) { Logger.getLogger(MegaCal.class.getName()).log(Level.SEVERE, null, ex); }