infix-notation

How to evaluate an infix expression in just one scan using stacks?

爷,独闯天下 提交于 2019-11-26 17:07:18
I want to know if there is a way to solve infix expressions in a single pass using 2 stacks? The stacks can be one for operator and the other for operands... The standard way to solve by shunt-yard algorithm is to convert the infix expression to postfix(reverse polish) and then solve. I don't want to convert the expression first to postfix. If the expression is like 2*3-(6+5)+8 , how to solve? Rohit Quite late, but here is the answer. Take two stacks: operator stack { for operators and parentheses }. operand stack . Algorithm If character exists to be read: If character is operand push on the

When to use parenthesis in Scala infix notation

非 Y 不嫁゛ 提交于 2019-11-26 08:26:06
问题 When programming in Scala, I do more and more functional stuff. However, when using infix notation it is hard to tell when you need parenthesis and when you don\'t. For example the following piece of code: def caesar(k:Int)(c:Char) = c match { case c if c isLower => (\'a\'+((c-\'a\'+k)%26)).toChar case c if c isUpper => (\'A\'+((c-\'A\'+k)%26)).toChar case _ => c } def encrypt(file:String,k:Int) = (fromFile(file) mkString) map caesar(k)_ The (fromFile(file) mkString) needs parenthesis in

Evaluating a string of simple mathematical expressions [closed]

泪湿孤枕 提交于 2019-11-26 07:54:03
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Challenge Here is the challenge (of my own invention, though I wouldn\'t be surprised if it has previously appeared elsewhere on the web)