创建一个多项式对象,提供以下文件
出于某种原因,当我在文件中阅读时:
3
3 5
-2 3
1 0
3
-3 5
5 3
-7 1
-2
它不会正确写入对象,例如第一个数字是多少个项,正确读取下一个数字3是系数,下一个数字5是指数。登记/> 数字读入正确但不会写入coeff和expon对象。这里没有提供的代码是由老师编写的,无论如何都非常简单,所以我不会包含它。
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>
#include <string>
using namespace std;
class Term
{
private:
float coeff;
int expon;
public:
Term(); // --- Creates empty Term
Term(float c, int e); // --- Creates Term object with coeff. c
//and expon. e
float getCoeff(void); // --- returns coeff of term
int getExpon(void); // --- returns expon of term
void setCoeff(float c); // --- sets term's coeff. to c
void setExpon(int c); // --- sets term's expon to c
};
class Poly
{
public:
//Constructors
Poly(); // creates empty polynomial
Poly(const Poly &poly); // copy constructor
~Poly(); // destructor free dynamically allocate ptrs.
//Member Functions
void insertTerm(Term t); // inserts Term t into polynomial
//Accessors
int getNumTerms(void); // returns number of terms in array
Term getTerm(int index); // returns term at index
//Mutators
void setNumTerms(int numberOfTerms);
//Overloaded Operators
Poly& operator = (const Poly& rightSide);//overloaded assignment operator
float operator () (float x);
private:
int numTerms; // number of terms in polynomial
int maxItems; // array capacity
Term *terms; // dynamically allocated array of terms
};
//Overloaded Operators for Poly Class
ostream& operator << (ostream& outFile, Poly& op1);
istream& operator >> (istream& inFile, Poly& op1);
Poly operator + (Poly& op1, Poly& op2);
//Functions
void error(string msg = "ERROR\n", int errorCode = 1, bool doExit = true);
int main(int argc, char* argv[])
{
//open file
ifstream inFile;
inFile.open("test1.txt");
if (inFile.fail())
{
cout << "open file error " << argv[argc - 1] << endl;
exit(1);
}
Poly f;
Poly g;
Poly p;
int value (0); // value of x for evaluation
inFile >> f;
inFile >> g;
cout << "F(x) = " << f << endl;
cout << "G(x) = " << g << endl;
// make copies of the polynomials
Poly temp1(f);
Poly temp2(g);
// add polynomials f and g
p = f + g;
cout << "\nP(x) = (F + G)(x) = " << p << endl;
// read in a value for X
inFile >> value;
// evaluate the polynomial
cout << "P(" << value << ") = " << p(value) << endl << endl;
// swap temp1 and temp2 - shouldn't change f & g
Poly temp3(temp2);
temp2 = temp1;
temp1 = temp3;
// Insert a new term 8x^6
temp1.insertTerm(Term(8,6));
cout << "temp1(x) is same as 8x^6 + G(x) or " << temp1 << endl;
cout << "temp2(x) is same as F(x) or " << temp2 << endl;
cout << "temp3(x) is same as G(x) or " << temp3 << endl << endl;
cout << "Original functions were:" << endl;
cout << "F(x) = " << f << endl;
cout << "G(x) = " << g << endl;
inFile.close();
return 0;
}
/***********************************************************************
* overloaded >> operator that reads an entire polynomial into an array
* of Terms in the proper order.
***********************************************************************/
istream& operator >> (istream& inFile, Poly& op1)
{
int numTerms;
int expon;
float coeff;
int i = 0;
//get number of terms
inFile >> numTerms;
if (numTerms < 1)
error("numTerms < 1\n", 2);
else
op1.setNumTerms(numTerms);
//read in terms
while (i < op1.getNumTerms())
{
//get coefficient check for valid term
inFile >> coeff;
if (inFile.fail())
{
error("inFile.fail() -- coeff\n", 3);
exit(1);
}
cerr << op1.getTerm(i).getCoeff() << endl;
op1.getTerm(i).setCoeff(coeff);
cerr << op1.getTerm(i).getCoeff() << endl;
//get exponent and check for valid term
inFile >> expon;
if (inFile.fail())
{
error("inFile.fail() -- expon\n", 4);
exit(2);
}
op1.getTerm(i).setExpon(expon);
i++;
}
i = op1.getNumTerms();
op1.getTerm(i).setCoeff(0);
return inFile;
}
/*****************************************************************************
* error function produces error code
****************************************************************************/
void error(string msg, int errorCode, bool doExit)
{
cout << "*** ERROR ***\n"
<< msg << endl
<< "*** END ERROR ***\n";
if (doExit == true)
exit(errorCode);
}