从文件中读取整数,中间有一个字符串

发布于 2024-12-08 07:48:17 字数 1452 浏览 2 评论 0原文

我有一个如下所示的输入文件:

3 2
5 1
3 0
XXX
2 1
3 0

我需要单独读取每个整数,将其放入多项式中。 “XXX”表示第二个多项式的起始位置。根据上面的示例,第一个多项式将为 3x^2 + 5x^1 + 3x^0,第二个多项式将为 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;
        }
    }

我遇到的问题是多项式 A 的值(XXX 之前的值)有效,但 B 无效。

我要问的是:如何做到这一点,以便当我遇到“XXX”时我可以设置“doneWithA”为true,并在“XXX”之后继续读取文件?

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( 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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫思念不要吵 2024-12-15 07:48:17

我会把它们放在单独的循环中,因为你知道有两个且只有两个:

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 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
眼泪淡了忧伤 2024-12-15 07:48:17

我认为最简单的方法是始终将输入读取为字符串,然后应用 atoi(), http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
如果此函数失败,则您到达的字符串不是数字,即“xxx”。

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".

网名女生简单气质 2024-12-15 07:48:17
    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 );
            }
        }
    }
    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 );
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文