南邮OJ 1005 多项式加法(二)
来源:互联网 发布:金山软件股票多少钱 编辑:程序博客网 时间:2024/05/18 02:08
一、首先,这个多项式是一个链表,多项式的每一项是链表一个节点,那么可以想到两种情况:
1)多项式只有一项或者是多项式的最后一项,那么这个节点就只需要有系数和指数两个元素,且不需要指向下 一个节点。
2)多项式的其中一项,那么这个节点就需要有系数、指数以及指向下一个节点的指针。
<span style="font-size:18px;"><span style="font-size:18px;">class Node{private: int coef; int exp; Node * link;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; }}</span></span></span>另外节点要有插入的功能,所以节点类中要有公有成员函数Insert()函数:
<span style="font-size:18px;"><span style="font-size:18px;">Node * Insert(int c, int e){ link = new Node (c,e,link); return link;}</span></span>二、链表是由节点构成的,且链表要有添加节点、输出链表以及链表相加的功能。
<span style="font-size:18px;"></pre><pre name="code" class="cpp"><span style="font-size:18px;">class nodeList{private: Node * theList;public: nodeList(); ~nodeList(); void AddNode(istream & in); void Output(ostream & out); void ListAdd(nodeList & r);}</span></span>
三、由于对象是类,对于输出操作符不能直接输出节点类,所以我们要做一个运算符重载。
ostream & operator << (ostream & out, const Node & val)
整个思路即为上面的三大块,现在一块一块的分开分析。
第一块,链表元素节点:
<span style="font-size:18px;">class Node {private: int coef; //系数 int exp; //指数 Node * link; //指向下一个节点的指针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; } friend class nodeList; //链接要操作节点 friend ostream & operator << (ostream &, const Node &); //节点的输出用到节点的私有成员}</span>
第二块:链表
<span style="font-size:18px;">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;} }elseq1 = 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;}</span>
第三块:运算符重载,输出对象是节点类
<span style="font-size:18px;">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;}}</span>
0 0
- 南邮OJ 1005 多项式加法(二)
- 南邮 OJ 1005 多项式加法
- 南邮OJ 1005 多项式加法
- noj 1005 多项式加法
- noj 1005 多项式加法
- 数据结构学习笔记(二)多项式加法与乘法
- 1005——多项式加法
- 多项式加法(10分)
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- C语言 整数的整数次方(不考虑大数问题)
- 1012
- 存储班长信息的学生类,将Stu类的数据成员的访问权限改为private,你的程序是否能完成要求的功能?如果不行,请修改程序。请不要修改给出的代码,只能修改自己写的代码。
- 红黑树
- 图像特效---(Sketch Filter)素描滤镜
- 南邮OJ 1005 多项式加法(二)
- HDU 1060 Leftmost Digit(大数问题)
- 一个强大的下拉刷新框架
- 放盘子
- HDU 1102 Constructing Roads (最小生成树)
- 第七周第一项目——成员函数
- 图像特效---Glow Filter发光滤镜
- Nova reboot 和 lock 操作 - 每天5分钟玩转 OpenStack(32)
- redis常用命令