-1

I have seen similar questions on this forum but i am still a bit confused....how can i turn a string that looks like this :

          MyString = "5+4-3*2";

into a simple mathematical operation that can be stored into an int? (looking for most efficient solution).

Ps. I can only use the following libraries:

    <cstdio>,<vector>,<cstring>,<string>,<cmath>

Can somebody please help me?

SpiderLinked
  • 363
  • 1
  • 6
  • 14
  • a good example can be found in Kernighan and Ritchie, just adapt to C++ where necessary – Bathsheba Nov 06 '13 at 16:29
  • @Bathsheba, that's a **C++** book I never heard about :) – StoryTeller - Unslander Monica Nov 06 '13 at 16:30
  • No it's C but the concepts are very relevant – Bathsheba Nov 06 '13 at 16:30
  • 1
    @Bathsheba, Such recommendations usually help produce "c++" programmers that just compile `c` code with a `c++` compiler. – StoryTeller - Unslander Monica Nov 06 '13 at 16:31
  • I don't think there is a built-in feature for this. You're going to need to use a library of some sort. – Kakalokia Nov 06 '13 at 16:32
  • Yeah but the library restrictions address that hazard – Bathsheba Nov 06 '13 at 16:32
  • 1
    Also it's a perfectly valid question, no need for downvotes really. – Kakalokia Nov 06 '13 at 16:34
  • i know that i can just check each character and make a switch for each case (+/-/*..etc) but that is very slow... – SpiderLinked Nov 06 '13 at 16:34
  • 1
    You can use [recursive descent](http://en.wikipedia.org/wiki/Recursive_descent_parser) – anatolyg Nov 06 '13 at 16:35
  • @StoryTeller: This isn't really a language specific question, despite the tags. It is an algorithm question. Any language should do, even pseudocode, as long as it's readable. And C should be readable to competent C++ developers. – Benjamin Lindley Nov 06 '13 at 16:38
  • @SpiderLinked 'that is very slow' but if that's the only way then that's what you have to do. It's what your compiler does when it compiles your code. – john Nov 06 '13 at 16:39
  • @BenjaminLindley, It's not as language agnostic as you suggest, the language affects the methodology and the approach. Contributed mostly by the toys that come with each standard library. If someone needs to write C++, we should help them do it the C++ way, and not just cop out of it because it's an "algorithm question". People seem much more prone to do this when C++ is involved for some reason. Nobody would offer to read up on K&R if it was tagged a Python question. – StoryTeller - Unslander Monica Nov 06 '13 at 16:47

2 Answers2

4

This will help you:

Algorithms for Parsing Arithmetic Expressions

It is in C but you can convert it to C++ as an extra exercise :)

You can also look at this question's answers - seems to cover what you're asking.

Community
  • 1
  • 1
slashmais
  • 7,069
  • 9
  • 54
  • 80
2

There is no functionality built-in to C++ to do this, like an eval type function in other languages. You'll need to do all the heavy lifting yourself.

This will involve:

  1. Parsing the string, breaking it in to individual tokens. Some tokens here would be: 5, +, 4, etc...
  2. Inrterpreting the parsed tokens as operators and operands. Operators would be +, -, etc and the operands are 5, 4 etc.
  3. Somehow executing a block of code for each operand and accumulating the results. This will likely involve a big switch or if-else block somewhere, with one condition per operator you support. You might also try to get fancy with templates or inheritence somehow, but I'd prefer to keep it simple to begin with.
John Dibling
  • 99,718
  • 31
  • 186
  • 324