【发布时间】:2011-10-04 19:57:50
【问题描述】:
我有一个如下所示的输入文件:
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”之后继续阅读文件?
【问题讨论】:
-
文件格式是强加给你的吗?它看起来很不雅。如果你能改变它,每行有一个多项式会让事情变得容易得多。