Please help me with parsing numbers form a file

This is a discussion on Please help me with parsing numbers form a file within the C++ Programming forums, part of the General Programming Boards category; Hi, I am doing my assignment, I need to parse the coefficient and exponent of each term from the polynomials ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Please help me with parsing numbers form a file

    Hi,

    I am doing my assignment, I need to parse the coefficient and exponent of each term from the polynomials in a file.

    A polynomial in the input file will end with a semicolon ;. In the following example there are 4 polynomials:
    x-1;x^8+x^7+x^6+x^5+x^4+x^3+x^2+x+1;2x^2-x+1;-x^3+x;

    I am not allowed to use string to read the input, and I have to get them on the fly when I read the file.

    Here is my attempt, but get bad results.......
    Code:
        ifstream file;
        file.open("1160_term_project_data.txt");
        
        int coef;
        int power;
        char ch;
        
            
        while(!file.eof())
        {
            coef = 0;
            power = 0;
            
            if(isdigit(file.peek()))
                file >> coef;
                    
            ch = file.get();
            
            if(ch == '-' && isdigit(file.peek()))
            {
                file >> coef;
                coef *= -1;
                ch = file.get();
            }
                        
            if(ch == '+' && file.peek() == 'x')
                coef = 1;
            else if(ch == '-' && file.peek() == 'x')
                coef = -1;
            else if(ch == 'x')
                {
                    if (coef == 0)
                        coef = 1;
                    
                    if(file.peek() == '^')
                    {
                        ch = file.get();
                        file >> power;
                    }
                    else
                    {
                        power = 1;
                    }
                }
            
            if(coef != 0)
                cout << "Coefficient: " << coef << " Power: " << power << endl;
            
        }
        
        file.close();
    I already tried .get(), .unget(), .peek() in the fstrem library, but still can't get what I want.

    Am I on the right track, or is there any other ways to do it? Thx.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Assuming you don't need to check for data validity and are always presented with a well-formed file, you can iterate through ";" characters and extract exponents by reading the character after "^" and coefficients by reading every character between the last exponent and the next "^".

    Or so I seem to think from a diagonal reading of your post.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    You should use boost spirit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Parsing a flat file to a SQl Server database?
    By Michael71 in forum C Programming
    Replies: 0
    Last Post: 01-16-2007, 11:45 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21