rpn

How to calculate a RPN expression having two different data types in Scala?

老子叫甜甜 提交于 2019-12-08 11:24:34
问题 The following program is suppose to calculate an expression with the possibility of having two different data types , Float and RDD . I already created an RPN from the infix expression and now I am trying to perform calculations on them. Note: I have also overloaded :+,-,/,* for doing calculations on RDD and float . def calcRPN(s: String): RDD[(Int,Array[Float])] = (s.split(' ').toList.foldLeft(Nil: List[Either[Float, RDD[(Int,Array[Float])]]) {foldingFunction}).head def foldingFunction(list:

java rpn calculator

99封情书 提交于 2019-12-04 05:39:40
问题 i would like to include a simple RPN type calculator function in one of my projects. basically i need a method that can convert for example: "30 / ((1 + 4) * 3)" into "2" does anyone know of any pre-written libs that can do this? thanks. 回答1: You should implement Shunting Yard Algorithm also look : Reverse Polish notation You can use Shunting Yard (Jep API) I suggest you to write it in python if you don't have to implement it in Java because of it's built-in methods print eval("30 / ((1 + 4)

Pattern is not splitting as desired, fails to split by +

谁都会走 提交于 2019-12-02 12:54:43
问题 I have the following code : Pattern pattern = Pattern.compile("\\d+(?:\\.\\d+)?(\\s\\d+(?:\\.\\d+)?)*\\s*[-\\+\\*/\\$£]"); String input = "4.0 5.0 2.0 / + 7.0 - 11.0 34.0 2.0 / 3.0 / 1.0 * +"; Matcher matcher = pattern.matcher(input); List<String> output = new ArrayList<>(); while (matcher.find()) { output.add(matcher.group()); } The pattern that is compiled has gone through a number of iterations, in it's current iteration it adds to the list the following : [4.0 5.0 2.0 /, 7.0 -, 11.0 34.0

handling unary minus for shunting-yard algorithm

一笑奈何 提交于 2019-11-29 06:51:43
Is there a better way to handle unary "-" in converting a infix expression to a postfix one? The obvious one would be prefix every unary "-" with a 0. Does anyone know better implementation? Thanks! The way I did this years ago was invent a new operator for my postfix expression. So when I encountered a unary minus in the infix, I'd convert it to # . So my postfix for a + -b became ab#+ . And, of course, my evaluator had to know that # only popped one operand. Kind of depends on how you're using the postfix expression once it's built. If you want to display it then your special # operator

Can this Python postfix notation (reverse polish notation) interpreter be made more efficient and accurate?

余生长醉 提交于 2019-11-28 21:58:44
Here is a Python postfix notation interpreter which utilizes a stack to evaluate the expressions. Is it possible to make this function more efficient and accurate? #!/usr/bin/env python import operator import doctest class Stack: """A stack is a collection, meaning that it is a data structure that contains multiple elements. """ def __init__(self): """Initialize a new empty stack.""" self.items = [] def push(self, item): """Add a new item to the stack.""" self.items.append(item) def pop(self): """Remove and return an item from the stack. The item that is returned is always the last one that

handling unary minus for shunting-yard algorithm

时光毁灭记忆、已成空白 提交于 2019-11-28 00:22:13
问题 Is there a better way to handle unary "-" in converting a infix expression to a postfix one? The obvious one would be prefix every unary "-" with a 0. Does anyone know better implementation? Thanks! 回答1: The way I did this years ago was invent a new operator for my postfix expression. So when I encountered a unary minus in the infix, I'd convert it to # . So my postfix for a + -b became ab#+ . And, of course, my evaluator had to know that # only popped one operand. Kind of depends on how you

Can this Python postfix notation (reverse polish notation) interpreter be made more efficient and accurate?

懵懂的女人 提交于 2019-11-27 21:02:23
问题 Here is a Python postfix notation interpreter which utilizes a stack to evaluate the expressions. Is it possible to make this function more efficient and accurate? #!/usr/bin/env python import operator import doctest class Stack: """A stack is a collection, meaning that it is a data structure that contains multiple elements. """ def __init__(self): """Initialize a new empty stack.""" self.items = [] def push(self, item): """Add a new item to the stack.""" self.items.append(item) def pop(self)