数据结构之一元多项式运算操作5-(加,减,乘,化简)

来源:互联网 发布:越南废除汉字 知乎 编辑:程序博客网 时间:2024/06/10 14:40

此操作实现一元多项式的基本操作


代码如下
#include <iostream>#include <cstdlib>#include <cmath>using namespace std;struct PolyNode{float coef;//存放系数int exp;//存放指数PolyNode *next;};class Poly{private:PolyNode *head = NULL;public:Poly();//建立空多项式~Poly();//释放多项式Poly(const Poly&);//拷贝构造函数void Create();//通过输入系数和指数建立多项式void Disp();//显示void Simplify();//多项式的化简void Add(Poly &py);//多项式相加void Substract(Poly &py);//多项式相减Poly Multiply(Poly &py);//多项式相乘};//建立空多项式(带头结点)Poly::Poly(){//cout << "1 \n";head = new PolyNode;if (!head)throw "内存分配失败 \n";head->next = NULL;}//通过输入系数和指数建立多项式void Poly::Create(){bool flag;float f;int i;PolyNode *p = head, *s = NULL;//作为临时变量while (cin >> f >> i){flag = true;if (fabs(f) < 1e-6 && i == 0)break;//结束输入else if (fabs(f) < 1e-6)//系数等于0continue;//结束本次循环else if (i == 0)//指数等于0flag = false;//输入每一项的系数和指数s = new PolyNode;s->coef = f;if (!flag)s->exp = 0;elses->exp = i;s->next = p->next;p->next = s;p = s;}}//拷贝构造函数(防止对象在浅复制时,多次被释放产生未定义行为)Poly::Poly(const Poly &py){//cout << "2 \n";head = new PolyNode;if (!head)throw "内存分配失败 \n";head->next = NULL;PolyNode *s = NULL, *p = head;PolyNode *first = py.head->next;while (first){s = new PolyNode;s->coef = first->coef;s->exp = first->exp;s->next = p->next;p->next = s;p = s;first = first->next;}}//多项式的化简(同指数的进行合并)void Poly::Simplify(){PolyNode *s = NULL, *t = NULL;t = new PolyNode;if (!t)throw "分配内存失败 \n";t->next = head->next;head->next = t;//作为首结点(存放指数为0的数)t->coef = 0;t->exp = 0;PolyNode *p = t;PolyNode *p2 = p->next;//p2作为p的next//合并多项式中的指数为0的项while (p2){if (p2->exp == 0)//当前结点的指数为0{t->coef += p2->coef;//加上当前结点的系数s = p2;//保存当前结点p->next = p2->next;p2 = s->next;delete s;}else{p = p->next;p2 = p2->next;}}if (fabs(t->coef) < 1e-6)//当系数和为0,销毁{head->next = t->next;delete t;t = NULL;}//合并指数相同的项,按照指数升序排(插排思想)PolyNode *first = NULL;//待插的第一个元素if (head->next->exp != 0)t = head;first = t->next->next;//连上第一个指数不为0的项t->next->next = NULL;while (first){for (s = first, p = t, p2 = t->next; p2 && p2->exp < s->exp; p = p2, p2 = p2->next);//找到第一个待插或者合并的项if (p2 == NULL){first = first->next;s->next = p->next;p->next = s;continue;}if (p2->exp == s->exp){p2->coef += s->coef;first = first->next;delete s;//释放结点}else{first = first->next;s->next = p2;p->next = s;}}}//多项式相加void Poly::Add(Poly &py){PolyNode *first = py.head, *s = NULL;PolyNode *p = NULL, *p2 = NULL;bool flag;while (first->next){flag = true;for (p = head, p2 = head->next, s = first->next; p2; p = p2, p2 = p2->next){if (p2->exp == s->exp)//指数相同{p2->coef += s->coef;first->next = s->next;//first = first->next;delete s;flag = false;break;}}//没找到指数相等的情况if (flag)first = first->next;}//处理项数中系数为0的情况first = head;while (first->next){if (first->next->coef == 0){s = first->next;first->next = s->next;delete s;continue;}first = first->next;}first = head;while (first->next)first = first->next;first->next = py.head->next;delete py.head;}//多项式相减void Poly::Substract(Poly &py){PolyNode *first = py.head, *s = NULL;PolyNode *p = NULL, *p2 = NULL;bool flag;while (first->next){flag = true;for (p = head, p2 = head->next, s = first->next; p2; p = p2, p2 = p2->next){if (p2->exp == s->exp)//指数相同{p2->coef -= s->coef;first->next = s->next;//first = first->next;delete s;flag = false;break;}}//没找到指数相等的情况if (flag)first = first->next;}//处理项数中系数为0的情况first = head;while (first->next){if (first->next->coef == 0){s = first->next;first->next = s->next;delete s;continue;}first = first->next;}first = py.head->next;while (first){first->coef *= -1;//系数变相反数first = first->next;}first = head;while (first->next)first = first->next;first->next = py.head->next;delete py.head;}//多项式相乘Poly Poly::Multiply(Poly &py){PolyNode *first = py.head, *s = NULL;Poly p;PolyNode *p2 = NULL, *p1 = p.head, *p3 = NULL;//p1指向p当前的最后结点//每一项都与其相乘while (first->next){for (p2 = head->next, s = first->next; p2; p2 = p2->next){p3 = new PolyNode;p3->coef = p2->coef * s->coef;p3->exp = p2->exp + s->exp;//cout << s->coef << " " << s->exp << " " << endl;p3->next = p1->next;//将相乘后的项放入t中p1->next = p3;p1 = p3;}first = first->next;}//p.Disp();return p;//返回结果}//显示多项式void Poly::Disp(){PolyNode *p = head->next;while (p){if (p == head->next)//第一项原样输出if (p->exp == 0)cout << p->coef;elsecout << p->coef << "x^" << p->exp;else{if (p->coef > 0)cout << "+";if (p->exp == 0)cout << p->coef;elsecout << p->coef << "x^" << p->exp;}p = p->next;}cout << endl;}Poly::~Poly(){PolyNode *p = head->next, *s = NULL;while (p){s = p;p = p->next;delete s;}}int main(){Poly p1;float f = 0;int i = 0;cout << "现在开始构建一元多项式 \n";cout << "每一行输入一项的系数和指数(空格分开,连续输入两个0表示结束输入) \n";p1.Create();cout << "第一个多项式为:";p1.Disp();cout << "现在开始构建第二个一元多项式 \n";cout << "每一行输入一项的系数和指数 \n";Poly p2;p2.Create();cout << "第二个多项式为:";p2.Disp();cout << "\n开始多项式操作 \n";Poly p3 = p1, p4 = p2;//拷贝Poly p5 = p1, p6 = p2;//拷贝p1.Add(p2);cout << "p1+p2=";p1.Disp();cout << "p1-p2=";p3.Substract(p4);p3.Disp();cout << "p1*p2=";Poly p7 = p5.Multiply(p6);p7.Disp();cout << "化简后的表达式:p1*p2=";p7.Simplify();p7.Disp();system("pause");return 0;}

运行如下

0 0
原创粉丝点击