简体   繁体   English

在Java中拆分数学表达式

[英]Splitting a maths expression in Java

I'm trying to split a simple mathematics expression from it's parenthesis 我试图从它的括号中分割出一个简单的数学表达式

For example : (8+(3(2+3)(4-1))) to separate in to little expressions like (2+3), (4-1), (3*5*3), and finally (8+45) . 例如: (8+(3(2+3)(4-1)))分成小表达式,如(2+3), (4-1), (3*5*3), and finally (8+45)

I tried looking at here Splitting an expression , but since it's in python, I don't know how to implement it in Java. 我试着在这里查看分裂表达式 ,但由于它是在python中,我不知道如何在Java中实现它。

Can someone please help me.. 有人可以帮帮我吗..

Do you actually have to "Split" it or do you just need to evaluate it? 你真的必须“拆分”它还是只需要评估它? It's easier to evaluate it directly. 直接评估它更容易。

This was my answer to that... 这是我对此的回答......

Parsing an arithmetic expression and building a tree from it in Java 在Java中解析算术表达式并从中构建树

I think dijkstra's shunting yard algorithm might help you, its purpose is to solve expressions that are in infix notation. 我认为dijkstra的分流码算法可能对你有帮助,它的目的是解决中缀符号的表达式。 You can take intermediate results to get the expressions you are looking for. 您可以获取中间结果以获取您要查找的表达式。

http://en.wikipedia.org/wiki/Shunting-yard_algorithm http://en.wikipedia.org/wiki/Shunting-yard_algorithm

C example code is included, this should help you since it's similar to Java. 包含C示例代码,这应该对您有所帮助,因为它与Java类似。

Just a suggestion use arithmatic expression tree to solve this. 只是一个建议使用算术表达式树来解决这个问题。 I've done this using C#, if you are interested I can give the method 我用C#完成了这个,如果你有兴趣我可以给出这个方法

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

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