所实现的一元多项式的结构如下图所示:
若只对多项式进行“求值”等不改变多项式系数和指数的运算,采用类似顺序表的顺序存储结构即可,否则应采用链式存储结构,本文因为要进行一元多项式的加法,加法,乘法,故采用的是链式存储结构
Polynomail.h
#include<stdio.h> #include<assert.h> struct Node { int coef; //系数 int expn; //指数 }; //节点结构 typedef struct PolynNode { Node data; struct PolynNode *link; }PolynNode; typedef PolynNode* Polynmail; void InitPolyn (Polynmail &head); void CreatPolyn(Polynmail &head, int n); void push_back (Polynmail &head, PolynNode *s); void ShowPolyn (Polynmail head); void AddPolyn(Polynmail &head, Polynmail pa, Polynmail pb); void SubPolyn(Polynmail &head, Polynmail pa, Polynmail pb); void MulPolyn(Polynmail &head, Polynmail pa, Polynmail pb); int lenth(Polynmail head); void destory(Polynmail &head);
#include"Polynomail.h" void InitPolyn(Polynmail &head) { PolynNode *s = new PolynNode; assert(s != NULL); s->data.coef = 0; s->data.expn = -1; s->link = NULL; head = s; } void push_back(Polynmail &head, PolynNode *s) { PolynNode *p = head; while (p->link != NULL) { p = p->link; } p->link = s; } //创建一元多项式 void CreatPolyn(Polynmail &head, int n) { int c; int e; for (int i = 0; i < n; ++i) { printf("请输入第%d项的系数和指数 >", i + 1); scanf_s("%d%d", &c, &e); PolynNode *s = new PolynNode; s->data.coef = c; s->data.expn = e; s->link = NULL; push_back(head, s); } } //打印一元多项式 void ShowPolyn(Polynmail head) { PolynNode *s = head->link; while (s != NULL) { if (s->data.expn == 0)//5x^0 ---->5 { printf("%d", s->data.coef); } else if (s->data.expn == 1)//5x^1---->5x { printf("%dx", s->data.coef); } else//正常结果直接打印 { printf("%dx^%d", s->data.coef, s->data.expn); } //下一项存在且系数为正数,需打印正号(负数会自带符号) s = s->link; if (s != NULL && s->data.coef > 0) { printf("+"); } } printf("\n"); } //表达式pa + pb的结果保存在head中 void AddPolyn(Polynmail &head, Polynmail pa, Polynmail pb) { PolynNode *sa = pa->link; PolynNode *sb = pb->link; PolynNode *s; while (sa != NULL && sb != NULL) { s = new PolynNode; s->link = NULL; //sa节点的指数 < sb节点的指数 -----------> //1:将sa节点中的数据插入到结果中 2:指向下一个pa节点 if (sa->data.expn < sb->data.expn) { s->data.coef = sa->data.coef; s->data.expn = sa->data.expn; sa = sa->link; } //sa节点的指数 > sb节点的指数 -----------> //1:将sb节点中的数据插入到结果中 2:指向下一个pb节点 else if (sa->data.expn > sb->data.expn) { s->data.coef = sb->data.coef; s->data.expn = sb->data.expn; sb = sb->link; } //sa节点的指数 == sb节点的指数 //1:将二者系数的和即相同的指数插入到结果中 2:pa、pb都指向下一个节点 else if (sa->data.expn == sb->data.expn) { s->data.coef = sb->data.coef + sa->data.coef; s->data.expn = sb->data.expn; sb = sb->link; sa = sa->link; } //若所加结果的系数为0,不用插入结果中,反之需要插入到结果中 if (s->data.coef != 0) { push_back(head, s); } } //将pa 或者pb当中表达式较长的剩余节点全部插入到结果中 if (sa != NULL) { while (sa != NULL) { s = new PolynNode; s->link = NULL; s->data.coef = sa->data.coef; s->data.expn = sa->data.expn; push_back(head, s); sa = sa->link; } } if (sb != NULL) { while (sb != NULL) { s = new PolynNode; s->link = NULL; s->data.coef = sb->data.coef; s->data.expn = sb->data.expn; push_back(head, s); sb = sb->link; } } } //表达式pa - pb的结果保存在head中 void SubPolyn(Polynmail &head, Polynmail pa, Polynmail pb) { PolynNode *sa = pa->link; PolynNode *sb = pb->link; PolynNode *s; while (sa != NULL && sb != NULL) { s = new PolynNode; s->link = NULL; //sa - sb sa中存在而sb中不存在的节点,相减后为正 if (sa->data.expn < sb->data.expn) { s->data.coef = sa->data.coef; s->data.expn = sa->data.expn; sa = sa->link; } //sa - sb sb中存在而sa中不存在的节点,相减后为负 else if (sa->data.expn > sb->data.expn) { s->data.coef = -(sb->data.coef); s->data.expn = sb->data.expn; sb = sb->link; } //sa - sb 两者都存在的节点,结果 = 二者相减后的结果 else if (sa->data.expn == sb->data.expn) { s->data.coef = sb->data.coef - sa->data.coef; s->data.expn = sb->data.expn; sb = sb->link; sa = sa->link; } if (s->data.coef != 0) { push_back(head, s); } } if (sa != NULL) { while (sa != NULL) { s = new PolynNode; s->link = NULL; s->data.coef = sa->data.coef;//sa - sb sa中存在而sb中不存在的节点,相减后为正 s->data.expn = sa->data.expn; push_back(head, s); sa = sa->link; } } if (sb != NULL) { while (sb != NULL) { s = new PolynNode; s->link = NULL; s->data.coef = -(sb->data.coef);//sa - sb sb中存在而sa中不存在的节点,相减后为负 s->data.expn = sb->data.expn; push_back(head, s); sb = sb->link; } } } int lenth(Polynmail head) { int count = 0; PolynNode *s = head->link; while (s != NULL) { count++; s = s->link; } return count; } //表达式pa * pb的结果保存在head中 void MulPolyn(Polynmail &head, Polynmail pa, Polynmail pb) { PolynNode *sb = pb->link; while (sb != NULL)//sb中的每一个节点和sa中的所有节点相乘 { PolynNode *sa = pa->link;//因为sb的每个节点都要和sa中的全部节点相乘,所以sb的每个节点 while (sa != NULL) //相乘时,sa总要从头开始 { //创建相乘后的节点 PolynNode *p = new PolynNode; p->data.coef = (sa->data.coef)*(sb->data.coef);//系数相乘 p->data.expn = (sa->data.expn)+(sb->data.expn);//指数相加 p->link = NULL; //寻找已存在的节点中是否存在与刚创建的节点(指数)相同的节点 PolynNode *s = head->link; while (s != NULL && (s->data.expn) != (p->data.expn)) { s = s->link; } //不存在指数相同的节点,直接尾插即可 if (s == NULL) { push_back(head, p); } //存在指数相同的节点,修改系数即可 else { s->data.coef += p->data.coef; } //(sb的当前节点)为乘以sa中的下一个节点做准备 sa = sa->link; } //为sb中的下一个节点乘以sa的全部节点做准备 sb = sb->link; } } void destory(Polynmail &head) { PolynNode *s = head->link; while (s != NULL) { head->link = s->link; delete s; s = head->link; } delete head; head = NULL; }
#include"Polynomail.h" #include<cstdlib> int main() { Polynmail pa; Polynmail pb; Polynmail pc; InitPolyn(pa); InitPolyn(pb); InitPolyn(pc); int select = 1; int n; while (select) { printf("*****************************************\n"); printf("* [1]创建多项式pa [2]创建多项式pb *\n"); printf("* [3]打印多项式pa [4]打印多项式pb *\n"); printf("* [5]AddPolyn [6]SubPolyn *\n"); printf("* [7]MulPolyn [8]打印多项式pc *\n"); printf("* [0]退出系统 [0]退出系统 *\n"); printf("*****************************************\n"); scanf_s("%d", &select); switch (select) { case 1: printf("请输入多项式的项数:>"); scanf_s("%d", &n); CreatPolyn(pa, n); break; case 2: printf("请输入多项式的项数:>"); scanf_s("%d", &n); CreatPolyn(pb, n); break; case 3: printf("pa = "); ShowPolyn(pa); break; case 4: printf("pb = "); ShowPolyn(pb); break; case 5: AddPolyn(pc, pa, pb); break; case 6: SubPolyn(pc, pa, pb); break; case 7: MulPolyn(pc, pa, pb); break; case 8: printf("pc = "); ShowPolyn(pc); break; default: break; } } destory(pa); destory(pb); destory(pc); system("pause"); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
【c++版数据结构】之用带头节点的单链表实现一元多项式(C语言版)
原文地址:http://blog.csdn.net/zongyinhu/article/details/48009087