English 中文(简体)
阅读档案中的分类账,在档案中加插
原标题:Reading integers from file, with a string in between

我有一份参考文件:

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( input )
    {
        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"?

问题回答

我会把他们 put住,因为你们知道有两点,只有两点:

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,  
 );
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 );
            }
        }
    }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签