0

Calculate from string: 1+2-3+4+5+6. I divide numbers and operators to two stacks. What is the best way to the calculation? Or I shouldnt divide to two stacks?

[1,2,3,4,5,6]

[+,-,+,+,+]

juku
  • 73
  • 4
  • possible duplicate of [Equation (expression) parser with precedence?](http://stackoverflow.com/questions/28256/equation-expression-parser-with-precedence) – Dario Feb 16 '15 at 11:15
  • See http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form for a general answer. If you only want to calculate string with numbers and +/-, then separating into two lists is a possibility. Then loop through the the lists and update your result. – Adrian Leonhard Feb 16 '15 at 11:17
  • It's the same question as: http://stackoverflow.com/questions/28500031/evaluating-a-math-expression-given-in-string/28500133#28500133 .It works in case of binary + and -, but if it comes to considering the operation order (*, /, etc), the polish notation alg is prolly the one. – DeiAndrei Feb 16 '15 at 11:28

1 Answers1

-1

Try to use reverse polish notation algorithm, see here

Anto
  • 4,265
  • 14
  • 63
  • 113
andrew
  • 1
  • 2