資料結構 單鏈表的應用:多項式及其運算
阿新 • • 發佈:2018-12-31
基本上照著書打就可以了.......
#include <iostream> using namespace std; struct Term { float coef; int exp; Term *link; Term (float c, int e, Term *next = 0) { coef = c; exp = e; link = next; } Term* InsertAfter (float c, int e); friend ostream& operator<< (ostream&, const Term&); }; class Polynomal { public: Polynomal () {first = new Term(0, -1);} Polynomal (Polynomal &R); int maxOrder(); Term* getHead() const {return first;} private: Term *first; friend ostream& operator<< (ostream&, const Polynomal&); friend istream& operator>> (istream&, Polynomal&); friend Polynomal operator+ (Polynomal&, Polynomal&); friend Polynomal operator* (Polynomal&, Polynomal&); }; Term* Term::InsertAfter(float c, int e) { link = new Term(c, e);//此處和書上不同 return link; } ostream& operator<<(ostream& out, const Term& x) { if (x.coef != 1 || x.exp ==0)//此處和書上不同,如果係數為±1不顯示1. { if (x.coef == -1 && x.exp !=0) out << '-'; else out << x.coef; } switch (x.exp) { case 0: break; case 1: out << "X"; break; default: out << "X^" << x.exp; break; } return out; } Polynomal::Polynomal(Polynomal &R) { first = new Term(0, -1); Term *destptr = first, *srcptr = R.getHead()->link; while(srcptr) { destptr->InsertAfter(srcptr->coef, srcptr->exp); srcptr = srcptr->link; destptr = destptr->link; } } int Polynomal::maxOrder() { Term *current = first; while (current->link) current = current->link; return current->exp; } istream& operator>> (istream& in, Polynomal &x) { Term *rear = x.getHead(); int c, e;//c不應該是float麼? while (1) { cout << "Input a term(coef,exp):" << endl; in >> c >> e; if (e < 0) break; rear = rear->InsertAfter(c,e); } return in; } ostream& operator<< (ostream& out, Polynomal &x) { Term *current = x.getHead()->link; cout << "The polynomal is : " << endl; bool h = true; while (current) { if (h == false && current->coef>0.0) out << "+"; h = false; out << *current; current = current->link; } out << endl; return out; } Polynomal operator+ (Polynomal &A, Polynomal &B) { Term *pa, *pb, *pc, *p; float temp; Polynomal C; pc = C.first; pa = A.getHead()->link; pb = B.getHead()->link; while (pa && pb) { if (pa->exp == pb->exp) { temp = pa->coef + pb->coef; if (temp > 0.001 || temp < -0.001)//fabs還要包含標頭檔案,麻煩.. pc = pc->InsertAfter(temp,pa->exp); pa = pa->link; pb = pb->link; } else if (pa->exp < pb->exp) { pc = pc->InsertAfter(pa->coef, pa->exp); pa = pa->link; } else { pc = pc->InsertAfter(pb->coef, pb->exp); pb = pb->link; } } if (pa) p = pa; else p = pb; while (p) { pc = pc->InsertAfter(p->coef, p->exp); p = p->link; } return C; } Polynomal operator* (Polynomal &A, Polynomal &B) { Term *pa, *pb, *pc; int AL, BL, i, k, maxExp; Polynomal 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) { pb = B.getHead()->link; while (pb) { k = pa->exp + pb->exp; result[k] = result[k] + pa->coef*pb->coef; pb = pb->link; } pa = pa->link; } for (i = 0; i <= maxExp; ++i) if (result[i] > 0.001 || result[i] < -0.001)//同上 pc = pc->InsertAfter(result[i],i); delete [] result; } pc->link = 0; return C; } int main() { Polynomal A; cout << "Input A:\n"; cin >> A; cout << A; Polynomal B; cout << "Input B:\n"; cin >> B; Polynomal C; C = A+B; Polynomal D; D = A*B; cout << "A: " << A << "B: " << B << "A+B: " << C << "A*B: " << D; }