#includeusing namespace std; struct Term { float coef; int exp; Term* link; Term(float c, int e, Term* next = NULL) { coef = c, exp = e, link = next; } Term* InsertAfter(float c, int e); friend ostream& operator<<(ostream&, const Term&); }; class Polynomial { private: Term* first; friend ostream& operator<<(ostream &, const Polynomial&); friend istream& operator>>(istream&, const Polynomial&); friend Polynomial operator+(Polynomial&, Polynomial&); friend Polynomial operator*(Polynomial&, Polynomial&); public: Polynomial() { first = new Term(0, -1); } Polynomial(Polynomial& P); Term* getHead()const { return first; } int maxOrder(); }; Term* Term::InsertAfter(float c, int e) { link = new Term(c, e, link); return link; } Polynomial::Polynomial(Polynomial& P) { first = new Term(0, -1); Term* destptr = first, * scrptr = P.getHead()->link; while (scrptr != NULL) { destptr->InsertAfter(scrptr->coef, scrptr->exp); scrptr = scrptr->link; destptr = destptr->link; } } istream& operator>>(istream& in, Polynomial& P) { Term* rear = P.getHead(); float c; int e; while (1) { in >> c >> e; if (e < 0)break; rear=rear->InsertAfter(c, e); } return in; } ostream& operator<<(ostream&out , const Term& x) { if (x.coef == 0.0)return out; out << x.coef; switch(x.exp) { case 0:break; case 1:out << "X"; break; default:out << "X^" << x.exp; break; } return out; } ostream& operator<<(ostream& out, const Polynomial& P) { bool ishead = true; Term*currt=P.getHead()->link; while (currt != NULL) { if (ishead == false && currt->coef > 0.0)out << "+"; ishead = false; out << *currt; currt = currt->link; } return out; } int Polynomial::maxOrder() { Term* current = first; while (current->link != NULL) { current = current->link; } return current->exp; } Polynomial operator+(Polynomial& A,Polynomial& B){ Polynomial C; Term* Acurrt = A.getHead()->link, * Bcurrt =B.getHead()->link; Term* Ccurrt = C.getHead(); while (Acurrt != NULL && Bcurrt!= NULL ) { float tempcoef; int tempexp; if (Acurrt->exp == Bcurrt->exp) { tempcoef = Acurrt->coef + Bcurrt->coef; tempexp = Acurrt->exp; Acurrt = Acurrt->link; Bcurrt = Bcurrt->link; } else if (Acurrt->exp > Bcurrt->exp) { tempcoef = Bcurrt->coef; tempexp = Bcurrt->exp; Bcurrt = Bcurrt->link; } else { tempcoef = Bcurrt->coef; tempexp = Bcurrt->exp; Acurrt = Acurrt->link; } if (tempcoef != 0) Ccurrt->InsertAfter(tempcoef, tempexp); Ccurrt = Ccurrt->link; } while(Acurrt!=NULL){ Ccurrt->InsertAfter(Acurrt->coef,Acurrt->exp); Ccurrt = Ccurrt->link; Acurrt = Acurrt->link; } while (Bcurrt != NULL) { Ccurrt->InsertAfter(Bcurrt->coef, Bcurrt->exp); Ccurrt = Ccurrt->link; Bcurrt = Bcurrt->link; } return C; } Polynomial operator*(Polynomial& A, Polynomial& B) { Term* pa, * pb, * pc; int AL, BL, i, k, maxExp; Polynomial C; pc = C.getHead(); AL = A.maxOrder(); BL = B.maxOrder(); if (AL != -1 || BL != -1) { maxExp = AL + BL; float* result = new float[maxExp + 1]; for (i = 0; i <= maxExp; i++)result[i] = 0.0; pa = A.getHead()->link; while (pa != NULL) { pb = B.getHead()->link; while (pb != NULL) { k = pa->exp + pb->exp; result[k] = result[k] + pa->coef * pb->coef; pb = pb->link; } pa = pa->link; } for (int i = 0; i <= maxExp; i++) if (abs(result[i]) > 0.001) pc = pc->InsertAfter(result[i], i); delete[]result; } pc->link = NULL; return C; } //void test02() { // Polynomial P1,P2,P3,P4; // P1.getHead()->InsertAfter(6, 0)->InsertAfter(1,2)->InsertAfter(1, 8); // cout <<"P1="<< P1 << endl; // P2.getHead()->InsertAfter(2, 0)->InsertAfter(3, 2)->InsertAfter(1, 7); // cout << "P2="<< P2<



