0

Is there any function in C++ (included in some library or header file), that would count an expression from string?

Let's say we have a string, which equals 2 + 3 * 8 - 5 (but it's taken from user's keyboard, so we don't know what expression exactly it will be while writing the code), and we want this function co count it, but of course in the correct order (1. power / root 2. times / divide 3. increase / decrease).

I've tried to take all the numbers to an array of ints and operators to an array of chars (okay, actually vectors, because I don't know how many numbers and operators it's going to contain), but I'm not sure what to do next.

Note, that I'm asking if there's any function already written for that, if not I'm going to just try it again.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Fiodor
  • 796
  • 2
  • 7
  • 18
  • This thread [Solving a Variable Equation defined by the User](http://stackoverflow.com/questions/16577756/solving-a-variable-equation-defined-by-the-user) may be helpful. – Shafik Yaghmour Dec 09 '13 at 01:25

3 Answers3

1

By count, I'm taking that to mean "evaluate the expression".

You'll have to use a parser generator like boost::spirit to do this properly. If you try hand-writing this I guarantee pain and misery for all involved.

Try looking on here for calculator apps, there are several:

http://boost-spirit.com/repository/applications/show_contents.php

There are also some simple calculator-style grammars in the boost::spirit examples.

  • Instead of linking to that old sample page, you could link to a relevant existing question :) http://stackoverflow.com/questions/15123412/how-to-verify-algebraic-statements-using-boostspirit – sehe Dec 08 '13 at 22:11
  • Coding a 4-bang calculator is straightforward, and a worthwhile exercise. – Pete Becker Dec 08 '13 at 22:48
1

Quite surprisingly, others before me have not redirected you to this particular algorithm which is easy to implement and converts your string into this special thing called Reverse Polish Notation (RPN). Computing expressions in RPN is easy, the hard part is implementing Shunting Yard but it is something done many times before and you may find many tutorials on the subject.

Quick overview of the algorithms:

  1. PRN - RPN is a way to write expressions that eliminates the need for parentheses, thus it allows easier computation. Practically speaking, to compute one such expression you walk the string from left to right while keeping a stack of operands. Whenever you meet an operand, you push it onto the stack. Whenever you meet an operation token, you calculate its result on the last 2 operands (if the operation is binary ofcourse, on the last only if it is unary) and push it onto the stack. Rinse and repeat until the end of string.

  2. Shunting Yard really is much harder to simply overview and if I do try to accomplish this task, this answer will end up looking much like the wikipedia article I linked above so I'll save that trouble to both of us.

Tl;DR; Go read the links in the first sentence.

Nikola Dimitroff
  • 6,127
  • 2
  • 25
  • 31
  • I'm gonna try this, but since I'm quite new to programming and not very good at maths (junior highschool), I'm not sure if I'm going to understand this. Yet as I said - I'm going to try this :). – Fiodor Dec 08 '13 at 22:14
0

You will have to do this yourself as there is no standard library that handles infix equation resolution

If you do, please include the code for what you are trying and we can help you

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
  • If you have no concerns about performance, calling external application to do that for you is a good option. Check popen() and 'bc' as an option. – nyrl Dec 08 '13 at 22:02
  • @nyrl that is quite clever - I was simply answering if anything **standard** exists and as yet I am pretty sure there is not. As the question is possibly vague - propose that as a solution if you want to explain popen – Glenn Teitelbaum Dec 08 '13 at 22:04