题目描述:线性表是一种最简单、最基本,也是最常用的数据结构,其用途十分广泛,例如,用带表头结点的单链表求解一元整系数多项式加法和乘法运算。现给两个一元整系数多项式,请求解两者之和。
题目链接:点击打开链接
代码是借鉴别人的,链接:点击打开链接
<span style="font-size:18px;">#include <iostream> using namespace std; class nodeList; class Node //单链表节点 { private: int coef; int exp; Node * link; friend class nodeList; friend ostream & operator << (ostream &,const Node &); public: Node (int c, int e):coef(c),exp(e) { link = 0; } Node (int c,int e, Node * next):coef(c),exp(e) { link = next; } Node * Insert (int c, int e) { link = new Node (c,e,link); return link; } }; ostream & operator << (ostream & out, const Node & val) { if (val.coef == 1) { switch (val.exp) { case 0:out << 1;break; case 1:out << "X";break; default : out << "X^" << val.exp; break; } return out; } if (val.coef == -1) { switch (val.exp) { case 0:out << -1;break; case 1:out << "-X"; break; default : out << "-X^"<< val.exp;break; } return out; } else { out << val.coef; switch (val.exp) { case 0:break; case 1:out <<"X"; break; default : out<<"X^" << val.exp;break; } return out; } } class nodeList { friend ostream & operator << (ostream &, const nodeList & ); friend istream & operator >> (istream &, nodeList &); friend nodeList & operator + (nodeList &, nodeList &); private: Node * theList; public: nodeList(); ~nodeList(); void AddNode (istream & in); void Output (ostream & out) const; void Listadd (nodeList & r); }; nodeList :: nodeList() { theList = new Node (0,-1); theList ->link = theList; } nodeList::~nodeList() { Node * p = theList->link; while (p != theList) { theList->link = p->link; delete p; p = theList->link; } delete theList; } void nodeList:: AddNode(istream & in) { Node * q = theList; int count = 0; int c,e; for (;;) { cin >> c >>e; if (c==0 && e ==-1) { break; } else { q = q->Insert(c,e); } count ++; } if (count == 0) { q = q->Insert(0,0); cout << 0; } } void nodeList:: Output (ostream & out )const { int i=0,j,k=0; Node * m = theList->link; for (; m!=theList & m->coef == 0;m=m->link) { i++; } m = theList->link; for (;m!= theList; m=m->link) { k++; } m = theList->link; if (k==1&& m->coef == 0) { cout << 0 << endl; return; } if (k==i) { cout << 0<< endl; return ; } for (j =0; j<i;j++) { m=m->link; } out << *m; m=m->link; for (;m!=theList;m=m->link) { if (m->coef > 0) { cout << "+"; out << *m; } else if (m->coef < 0) { out << *m; } } cout << endl; } void nodeList::Listadd(nodeList & r) { Node * q,*q1 = theList,*p; p = r.theList->link; q = q1->link; while(p->exp >= 0) { while (p->exp < q->exp) { q1=q; q=q->link; } if (p->exp == q->exp) { q->coef = p->coef + q->coef; if (p->coef == 0) { q1->link = q->link; delete q; q = q1->link; } else { q1=q; q=q->link; } } else q1 = q1->Insert(p->coef,p->exp); p=p->link; } } ostream & operator <<(ostream & out, const nodeList & x) { x.Output(out); return out; } istream & operator >> (istream & in, nodeList & x) { x.AddNode (in); return in; } nodeList & operator + (nodeList & a, nodeList & b ) { a.Listadd (b); return a; } int main() { nodeList p,q,q1; cin>>p,cout<<p; cin>>q,cout<<q; q1=q+p; cout<<q1; }</span>
时间: 2024-10-11 05:09:58