开发者

Reading integers from file, with a string in between

开发者 https://www.devze.com 2023-04-10 10:14 出处:网络
I have an input file that looks like this: 3 2 5 1 3 0 XXX 2 1 3 0 I need to read each integer separately, putting it into a polynomial. The \"XXX\" represents where the second polynomial will star

I have an input file that looks like this:

3 2
5 1
3 0
XXX
2 1
3 0

I need to read each integer separately, putting it into a polynomial. The "XXX" represents where the second polynomial will start. Based on the above example, the first polynomial will be 3x^2 + 5x^1 + 3x^0 and the second would be 2x^1 + 3x^0.

#include <iostream>
#include <iomanip>
#include <fstream>
#include "PolytermsP.h"

using namespace std;

int main()
{
    // This will be an int
    coefType coef;

    // This will be an int
    exponentType exponent;

    // Polynomials
    Poly a,b,remainder;

    // After "XXX", I want this to be true
    bool doneWithA = false;

    // input/output files
    ifstream input( "testfile1.txt" );
    ofstream output( "output.txt" );

    // Get the coefficient and exponent from the input file
    input >> coef >> exponent;

    // Make a term in polynomail a
    a.setCoef( coef, exponent );


    while( inp开发者_运维知识库ut )
    {
        if( input >> coef >> exponent )
        {

            if( doneWithA )
            {
                // We passed "XXX" so start putting terms into polynomial B instead of A
                b.setCoef( exponent, coef );
            } else {
                // Put terms into polynomail A
                a.setCoef( exponent, coef );
            }
        }
        else
        {
            // Ran into "XXX"
            doneWithA = true;
        }
    }

The problem I'm having is that the values for polynomial A (what comes before XXX) are working, but not for B.

What I'm asking is: How do I make it so when I run into "XXX" i can set "doneWithA" to true, and continue reading the file AFTER "XXX"?


I would put them in seperate loops since you know there's two and only two:

coefType coef; // This will be an int
exponentType exponent; // This will be an int
Poly a,b;
ifstream input( "testfile1.txt" );

while( input >> coef >> exponent )
    a.setCoef( exponent, coef );
input.clear();
input.ignore(10, '\n');
while( input >> coef >> exponent )
    b.setCoef( exponent, coef );

//other stuff


I think the easiest way to do this is to always read the input as a string and then apply atoi(), http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ If this function fails then you have reached a string which is not a number i.e. "xxx".


    const string separator("XXX");
    while(input){
        string line;
        getline(input,line);
        if(line == separator)
            doneWithA = true;
        else {
            istringstream input(line);
            if(input >> coef >> exponent){
                if(doneWithA)
                    b.setCoef( coef, exponent );
                else
                    a.setCoef( coef, exponent );
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号