// I warn you, this file is just for information. it is not // meant to be complete programming in any sense of the word. // Use this information at your own risk. It is an attempt to // show you one possible way the poly problem could be attempted // The add module below does not add terms in sorted order as // would be required by your application. // it is a single link, which is appropriate for this appplication. // Moreover, your application should include a Header node, containing // the degree of the polynomial (and any other appropriate information // This application has no header. #ifndef _POLY_ #define _POLY_ template class polynomial { private: class polynode { public: CoefType coef; int expo; polynode *next; polynode(CoefType co = 0, int ex = 0, polynode *ptr = NULL) { coef = co; expo = ex; next = ptr; } }; public: // put function members here void display(); polynomial(); addterm(CoefType coef, int degree); private: polynode *head; int mydegree; // others possible }; template polynomial::addterm(CoefType coef, int degree) { int max; polynomial::polynode *temp; polynomial::polynode *tem; tem = new polynomial::polynode(); tem->coef = coef; tem->expo = degree; tem->next = NULL; mydegree = 0; if (head == NULL) head = tem; else { temp = head; while (temp->next != NULL) temp=temp->next; temp->next = tem; } max = 0; tem = head; for (tem = head; tem != NULL; tem = tem->next) { if (tem->expo > max) max = tem->expo; } head->expo = max; mydegree = max; } template polynomial::polynomial() { mydegree = 0; head = new polynomial::polynode(0,0,NULL); } template void polynomial::display() { polynomial::polynode *temp; temp = head->next; cout << mydegree << endl; while (temp != NULL) { cout << "+" << temp->coef << "x^" << temp->expo << " "; temp=temp->next; } cout << endl; } #endif