Thread: Parsing a String

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Parsing a String

    I am going to have to parse a polynomial string to break it down into its different elements.

    Lets say I have this polynomial: 5x^3+-23x^12

    What my thoughts were was to do something like this as an example:


    Code:
        string polyString = "5x^3+-23x^12";
        char thisChar;
        for( int count = 0; count < polyString.size(); count++ ){
             thisChar = polyString.at(count);
             //then some if statements in here to detect if it is a number
        }

    I am curious if anyone has a good solution getting the numbers out of the polynomial. All I need to get out is the coefficient and exponent.

    My idea is to take the each char and detect what it is, but I was wondering if anyone had a good way of doing this that did not involve if statements that would be incredibly long.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    use a for loop like you are and check each character, use the isdigit() function
    edit: search cplusplus.com reference if you dont know how to use it, its easy though

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    sscanf perhaps?
    Code:
    #include <cstdio>
    .
    .
        string polyString = "5x^3+-23x^12";
        int c[10], e[10];
    
        int n = sscanf(polyString.c_str(), "%dx^%d%+%dx^%d+%dx^%d",
        &c[0], &e[0], &c[1], &e[1], &c[2], &e[2]);
        for (int i=0; i<n/2; ++i)
        {
             cout << c[i] << " " << e[i] << endl;
        }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    I just used that polynomial as an example. The program needs to be able to handle a polynomial of any size. That would work great, but that above solution wont work in this case.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    are the coefficients only numerical? if so isdigit() and for loop should work.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >That would work great, but that above solution wont work in this case.
    Can you explain better why not? This code runs fine for me:
    Code:
    #include <iostream>
    #include <string>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
        string polyString = "5x^3+-23x^12";
        int c[10], e[10];
    
        int n = sscanf(polyString.c_str(),
        "%dx^%d%+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d",
        &c[0],&e[0],&c[1],&e[1],&c[2],&e[2],&c[3],&e[3],&c[4],&e[4],&c[5],&e[5],
        &c[6],&e[6],&c[7],&e[7],&c[8],&e[8],&c[9],&e[9]);
        for (int i=0; i<n/2; ++i)
        {
             cout << c[i] << " " << e[i] << endl;
        }
    
       cin.get();
       return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Quote Originally Posted by swoopy
    Can you explain better why not? This code runs fine for me:

    What happens if the polynomial is something like:

    "5x^3+-23x^12+3x^1+2x^1+5x^1"

    Would that code be able to be expandable for any length polynomial that the user could input?

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    can you answer my previous question?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Since you have a string you can also use the string's find functions. Another option is to place the string into a stringstream and parse it that way.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Quote Originally Posted by nadroj
    can you answer my previous question?

    That will work fine, but it will get somewhat sticky when I get a polynomial like:

    "345x^4"


    Code:
        string polyString = "345x^4";
        char thisChar;
        int temp;
    
        for( int count = 0; count < polyString.size(); count++ ){
             thisChar = polyString.at(count);
             if(thisChar.isDigit() == true){
                    temp = (int)thisChar;
                    //how would i take into account a number greater than one digit though?
             }
        }
    I need to get the whole number (345 in this case) as an int so I can use it, any idea how to get that?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you could read if a character is digit, if it is, scan ahead and see if the next one also is, and so on. if they are consecutive then it is the same number

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Quote Originally Posted by nadroj
    you could read if a character is digit, if it is, scan ahead and see if the next one also is, and so on. if they are consecutive then it is the same number

    Ok, I see what you are saying. Thanks for all the help everyone, I am going to try to get this working now.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by mas0nite
    What happens if the polynomial is something like:

    "5x^3+-23x^12+3x^1+2x^1+5x^1"

    Would that code be able to be expandable for any length polynomial that the user could input?
    The array has 10 elements, so it will work for a polynomial of up to 10 terms. However I get the feeling that you would be more comfortable using a loop to parse the expression. Or another option is to find a regular expression library. The Boost library may have something you could use.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And what about 3x+1? Will I see 1?

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    630
    Try boost

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String parsing
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-03-2008, 05:06 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM