本文介绍了java中的数学表达式解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑像E这样的表达式:
123 * x + 456 * y * 12 - 200 / z遵循以下规则: 1.可能包含操作数:+, - ,*,/ 2.可能包含变量(如'x') 3.可能包含常量(如'123') 4.不包含括号。 应该充分利用OOP原则。 有人可以帮我写一套Java类来完成这个吗? 什么我试过了: 我尝试过使用ScriptEngine。但是我不能使用那些ScriptEngine。
ScriptEngine engine = new ScriptEngineManager ()。getEngineByName( JavaScript); System.out.println( result = + engine.eval(expr, new SimpleBindings(vars)));解决方案
这是一项任务:这样你就可以学习编程语言,OOP,数据结构的概念。 我不确定为什么要使用ScriptEngine,因为你需要制作解析器,而不是显示它的用法。 数学表达式解析简单指南 [ ^ ],这篇文章有很好的表达材料解析。
Consider an expression like E:
123 * x + 456 * y * 12 - 200 / zwhich follows the rules: 1. may contain operands: +,-,*,/ 2. may contain variables (like 'x') 3. may contain constants (like '123') 4. does not contain parenthesis. Focus should be put on good use of OOP principles. Can someone help me to write a set of Java classes to accomplish this? What I have tried: I have tried using ScriptEngine. However I cant use those ScriptEngine.
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); System.out.println("result = " + engine.eval(expr, new SimpleBindings(vars))); 解决方案 This is an assignment: so that you can learn concepts of programming language, OOP, data structures. I am unsure why ScriptEngine is being used since you are required to make the parser, not show its usage. Simple Guide to Mathematical Expression Parsing[^], this article has good material on expression parsing.更多推荐
java中的数学表达式解析器
发布评论