从文件中读取整数,中间有一个字符串
我有一个如下所示的输入文件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会把它们放在单独的循环中,因为你知道有两个且只有两个:
I would put them in seperate loops since you know there's two and only two:
我认为最简单的方法是始终将输入读取为字符串,然后应用 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".