Thread: Simplest way to calculate the string content?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Simplest way to calculate the string content?

    Hi

    What is the simplest way to calculate string like:

    String^ numbers = "15+3*2";


    In php I used eval(), but I can't find a function like eval() in c++.

    Thx!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Simplest way is manually. Either that or find some library or copy/paste existing code. Theres no standard function to do something like that.

    You have to manually parse the string, converting the numbers to actual numbers (ints, floats, whatever your using). You also have to manually look for the char operators (+, -, etc) and do the operations manually. Its straightforward if you expect/force everything to be either left or right associative. If you follow normal operator precedence (i.e. BEDMAS) then things become more complicated.

    For the string you gave, assuming all operators are left-associative, you basically:
    - parse the string left to right
    - go until you see a non-number, so you read "1", "5", "+" and the characters up to the "+" is the left operand, the string "15" which you can convert to an int using "atoi"
    - you now already know the operator
    - continue looking for the next operand, "3", convert to int, perform the operation, and this value is the "new" left operand.

    So you perform 15 + 3 = 18, then 18*2 = 36. As mentioned, this assumes left associativity for the operators. Doing right-associativity is just as easy. Things are quite complicated when you follow something like BEDMAS.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could search for "shunting yard algorithm".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You could also go to the tutorial of Boost.Spirit and use that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread