数据结构-多项式
来源:互联网 发布:淘宝客推广平台靠谱吗 编辑:程序博客网 时间:2024/05/17 09:31
实现了多项式输入,输出,加法,乘法等4个基本操作。
采用的算法:
加法:归并
乘法:1。两个多项式项两两相乘保存在数组中,2。对数组进行排序 3。顺序遍历数组,合并同类项计算结果
/* Author : Moyiii * Mail: lc09@vip.qq.com * 多项式的ADT实现,实现了加法和乘法 * 因为怕出现一些莫名错误,所以加法乘法都写成了静态成员函数 * 仅作学习之用,当然如果 * 你想拿去用,随你好啦。 * 如果发现BUG,或你有更好的想法,欢迎反馈给我*/#include <iostream>using namespace std;class PolyNode{public: PolyNode(int co = 0, int ex = -1):coef(co),exp(ex) { ; } int coef; int exp; PolyNode* next;};class Polynomal{public: Polynomal(); ~Polynomal(); void input(); void output(); void clear(); static void addPoly(Polynomal &polya,Polynomal &polyb, Polynomal &polyc); static void multiPoly(Polynomal &polya,Polynomal &polyb, Polynomal &polyc);private: PolyNode * head; PolyNode * tail; int length; void insNode(int coef, int exp);};Polynomal :: Polynomal(){ head = new PolyNode(); tail = head; head->next = NULL; length = 0;}//尾插法,跟链表的操作是一样的void Polynomal :: insNode(int coef, int exp){ if(coef != 0) { PolyNode *node = new PolyNode(coef, exp); node->next = tail->next; tail->next = node; tail = node; length++; } return;}//输入一个多项式void Polynomal :: input(){ cout << "请安升序顺序输入系数和指数,以-1 -1结束" << endl; int e,c; cin >> c >> e; while(e > 0) { insNode(c,e); cin >> c >> e; } cout << "您本次输入的多项式为:" << endl; output();}//输出一个多项式,做了一些格式处理void Polynomal :: output(){ PolyNode *p = head->next; bool first = false; while(p != NULL) { if(!first) { first = true; } else { if(p->coef > 0) { cout << "+"; } } if(p->coef == 1) { ; } else if(p->coef == -1) { cout << "-"; } else { cout << p->coef; } cout << "X"; if(p->exp != 1) { cout << "^" << p->exp; } p = p->next; } cout << endl; return;}Polynomal :: ~Polynomal(){ while(head != NULL) { PolyNode *p = head; head = head->next; delete p; }}//清除一个多项式void Polynomal :: clear(){ PolyNode *p = head->next; while(p != NULL) { head->next = p->next; delete p; p = head->next; } length = 0;}/*有些算法书采用计算两个多项式最大项相乘的结果max,然后建立一个0~max数组,将 相乘的结果分别映射在这max个数组中,这样的数组开销非常大。特别是碰到一些高次幂 因此本设计将直接计算两两项相乘结果,保存在数组中,然后对数组进行排序,最后 合并相同项建表。*/void swapNode(int &a, int &b){ int temp = a; a = b; b = temp; return;}void Polynomal :: multiPoly(Polynomal &polya,Polynomal &polyb, Polynomal &polyc){ int len = polya.length * polyb.length; int *coefs = new int[len + 1]; int *exps = new int[len + 1]; int index = 0; //获得所有项 for(PolyNode *ha = polya.head->next; ha != NULL; ha = ha->next) { for(PolyNode *hb = polyb.head->next; hb != NULL; hb = hb->next) { coefs[index] = ha->coef * hb->coef; exps[index] = ha->exp + hb->exp; index++; } } //后续处理方便 exps[index] = -1; //对项进行冒泡排序 for(int i = 1; i <= index - 1; ++i) { for(int j = 0; j < index - i; ++j) { if(exps[j] > exps[j+1]) { swapNode(exps[j],exps[j+1]); swapNode(coefs[j],coefs[j+1]); } } } //合并最终结果并建项 int cur = 0; int count = coefs[cur]; Polynomal temp; //在处理最后一个项时,会越界,此时前边增加的一个无用项就起到作用了 //因为系数是-1,所以不会与之前有任何冲突,而且不会被计算到结果里。 while(cur < index) { //相同指数,系数相加保存 if(exps[cur] == exps[cur+1]) { cur++; count = count + coefs[cur]; } else { //指数不同,从新计算系数并向项中加入一个节点 temp.insNode(count,exps[cur]); cur++; count = coefs[cur]; } } //同加法,把结果传递给PC polyc.clear(); PolyNode *p = temp.head; while(p != NULL) { polyc.insNode(p->coef, p->exp); p = p->next; } return;}//采用归并的算法思想,思路比较简单,看不懂的轻去看归并排序。void Polynomal :: addPoly(Polynomal &polya,Polynomal &polyb, Polynomal &polyc){ Polynomal temp; PolyNode *ha = polya.head->next; PolyNode *hb = polyb.head->next; while(ha != NULL && hb != NULL) { if(ha->exp < hb->exp) { temp.insNode(ha->coef, ha->exp); ha = ha->next; } else if(hb->exp < ha->exp) { temp.insNode(hb->coef, hb->exp); hb = hb->next; } else { temp.insNode(ha->coef + hb->coef, ha->exp); ha = ha->next; hb = hb->next; } } while(ha != NULL) { temp.insNode(ha->coef, ha->exp); ha = ha->next; } while(hb != NULL) { temp.insNode(hb->coef, hb->exp); hb = hb->next; } polyc.clear(); PolyNode *p = temp.head; while(p != NULL) { polyc.insNode(p->coef, p->exp); p = p->next; } return;}int main(){ Polynomal pa,pb,pc,pd; pa.input(); pb.input(); Polynomal :: addPoly(pa, pb, pc); pc.output(); Polynomal :: multiPoly(pa, pb, pd); pd.output(); return 0;}
0 0
- 数据结构-多项式
- 数据结构-多项式
- 数据结构 一元多项式相加
- 数据结构之一元多项式
- 数据结构实验,多项式类
- 数据结构 多项式的合并
- 数据结构_1_多项式运算
- 数据结构--一元多项式
- 【数据结构】链表:多项式加法
- 数据结构-一元多项式操作
- 数据结构-一元多项式加减程序
- 数据结构 -- 多项式相加和相乘
- 数据结构实现一元多项式相加
- 数据结构应用-------------多项式的相加
- 数据结构之多项式(C++)
- 多项式【数据结构课设一枚,671原创】
- 数据结构一元多项式运算分析
- [数据结构] 链表实现多项式
- 运维人员必须熟悉的运维工具汇总
- java 堆排序
- 学习记录,不断更新
- 107. 987654321 problem
- Mysql-SQL优化-统计某种类型的个数
- 数据结构-多项式
- JDK中的设计模式
- BadImageFormatException : Could not load file or assembly or one of its dependencies.
- OpenCV入门(二十一)-- 绘制彩色图像的直方图
- Windows7 64 VS2010/2012 去掉VMware菜单
- iOS开发之静态库的制作
- 博客搬家
- 【Leetcode】Unique Binary Search Tree II (DP)
- Android 生命周期-笔记