这是数据结构的实验~~ 由于某些实验要上交,所以一部分的程序的代码改为英语注释了 这是求解多项式相加和相乘的算法实现。 采用线性表就可以实现了。 代码可能有点繁杂,因为线性表的实现也包括在里面了。 Experiment1_1.h ,算法的实现的头文件。 [cpp] view plaincopy /* Experiment1_1.h */ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 实验题1.1 问题描述: // 有两个指数递减的一元多项式,写一程序先求这两个多项式的和,再求它们的积。 // // 提示 1 : // 用带表头结点的单链表作为多项式的存储表示; // 要建立两个单链表; // 多项式相加就是要把一个单链表中的结点插入到另一个单链表中去,要注意插入、删除操作中指针的正确修改。 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #include <iostream> using namespace std; // - - - - base constant value - - - - - const int OK = 1; const int FAIL = 0; const int EQUAL = 1; // equal to const int BIG = 2; // bigger than const int LESS = 3; // less than // - - - - data structure - - - - - // element of Polynomial typedef struct { float coef; int expn; }term, ElemType; // node of linklist typedef struct LinkList { term e; struct LinkList* next; }LinkList, LNode, Polynomial; // - - - - - - base functions - - - - - - int InitList (LinkList*& L); int SetCurElem (LNode*& h, const ElemType e); int MakeNode (LNode*& s, const ElemType e); int InsFirst (LNode*& q, LNode*& s); int compare (const term a, const term b); int LocateElem (LinkList* L, const ElemType e, LNode*& q); void CreatPolyn (Polynomial*& P, const int m); void DestroyPolyn (Polynomial*& P); void CopyPolyn (Polynomial*& P, const Polynomial* copy); int PrintPolyn (const Polynomial* P); void AddPolyn (Polynomial*& Pa, Polynomial*& Pb); void MutilplyPolyn(Polynomial*& Pa, Polynomial*& Pb); // add pa to pb and destroy pb void AddPolyn(Polynomial*& Pa, Polynomial*& Pb) { // aCur point to the current node of pa // aPrior point to the prior node of aCur // bCur point to the current node of pb // tmp pointer used temporary LNode* aCur; LNode* aPrior; LNode* bCur; LNode* tmp; // initialize the pointers aPrior = Pa; aCur = aPrior->next; bCur = Pb->next; // result save the result of the compare function // forDelete point to the node to be deleted int result; LNode* forDelete; // polynomial addition while (aCur && bCur) { result = compare(aCur->e, bCur->e); switch(result) { // add the b's coef to a case EQUAL: aCur->e.coef += bCur->e.coef; forDelete = bCur; bCur = bCur->next; delete forDelete; if (aCur->e.coef == 0.0) { forDelete = aCur; aCur = aCur->next; aPrior->next = aCur; delete forDelete; } break; // aCur point to the next node case BIG: aPrior = aPrior->next; aCur = aPrior->next; break; // insert bCur to aCur's prior node case LESS: tmp = bCur; bCur = bCur->next; aPrior->next = tmp; tmp->next = aCur; aPrior = aPrior->next; break; default: break; } } // if aCur=NULL then add b's rest nodes to the rear of a if (!aCur) { aPrior->next = bCur; } delete Pb; } // multiply b to a, and destroy b void MutilplyPolyn(Polynomial*& Pa, Polynomial*& Pb) { // Pa = A(x) = sum(ai * x ^ aei), i=1...m; // Pb = B(x) = sum(bi * x ^ bei), i=1...n; // Pa*Pb = sum(bi * A(x) * x^bei) = sum( sum((aj * bi) * x^(aej+bei)), j=1...m ), i=1...n; // backupPa save the copy of Pa // backupPa = A(x) = sum(ai * x ^ aei), i=1...m; // tmp memorize the intermediate result // tmp = sum((aj * bi) * x^(aej+bei)), j=1...m Polynomial* backupPa; Polynomial* tmp; // aCur point to backupPa LNode* aCur; LNode* bCur; LNode* tmpCur; // copy pa to backupPa and tmp CopyPolyn(backupPa, Pa); CopyPolyn(tmp, Pa); // preprocessing Pa:first destroy Pa then save the first result in Pa bCur = Pb->next; aCur = backupPa->next; tmpCur = tmp->next; while (aCur) { tmpCur->e.coef = bCur->e.coef * aCur->e.coef; tmpCur->e.expn = bCur->e.expn + aCur->e.expn; aCur = aCur->next; tmpCur = tmpCur->next; } bCur = bCur->next; DestroyPolyn(Pa); CopyPolyn(Pa, tmp); // multiply the two polynomial,add tmp to Pa when finish a intermediate result while (bCur) { CopyPolyn(tmp, Pa); aCur = backupPa->next; tmpCur = tmp->next; while (aCur) { tmpCur->e.coef = bCur->e.coef * aCur->e.coef; tmpCur->e.expn = bCur->e.expn + aCur->e.expn; aCur = aCur->next; tmpCur = tmpCur->next; } AddPolyn(Pa, tmp); bCur = bCur->next; } DestroyPolyn(Pb); } // initialize the linklist int InitList(LinkList*& L) { L = new LinkList; if (!L) { return FAIL; } L->next = NULL; return OK; } // set the node h to the value e int SetCurElem(LNode*& h, const ElemType e) { if (!h) { return FAIL; } else { h->e.coef = e.coef; h->e.expn = e.expn; return OK; } } // make a node using value e, use the pointer s to point to the node int MakeNode(LNode*& s, const ElemType e) { s = new LNode; if (!s) { return FAIL; } else { s->e.coef = e.coef; s->e.expn = e.expn; s->next = NULL; } return OK; } // to insert the node s after node q int InsFirst(LNode*& q, LNode*& s) { if (q == NULL || s == NULL) { return FAIL; } else { s->next = q->next; q->next = s; return OK; } } // compare two term int compare(const term a, const term b) { if (a.expn == b.expn) { return EQUAL; } else if (a.expn > b.expn) { return BIG; } else { return LESS; } } // locate the value e, if successed, return OK and q point to the position; // else return FAIL and q point to the position which node first less than e int LocateElem(LinkList* L, const ElemType e, LNode*& q) { LinkList* h; h = L->next; q = L; int result; while (h) { result = compare(h->e, e); if (result == EQUAL) { q = h; return OK; } else if (result == LESS) { return FAIL; } else { h = h->next; q = q->next; } } return FAIL; } // create the polunomial with the input value void CreatPolyn(Polynomial*& P, const int m) { InitList(P); term e; e.coef = 0.0; e.expn = -1; SetCurElem(P, e); int i; LNode* q; LNode* s; for (i=1; i<=m; i++) { cout<<"/ - "; cin>>e.coef>>e.expn; if (e.coef == 0.0) { continue; } if (LocateElem(P, e, q) == FAIL) { if (MakeNode(s, e)) { InsFirst(q, s); } } } } // destroy the polynomial void DestroyPolyn(Polynomial*& P) { if (P) { LNode* q; LNode* s; q = P; while (q->next) { s = q; q = q->next; delete s; } delete q; } } // print the polynomial int PrintPolyn(const Polynomial* P) { if (!P) { return FAIL; } LNode* s; s = P->next; if (s) { cout<<s->e.coef<<"*X^"<<s->e.expn; s = s->next; } while (s) { cout<<" + "<<s->e.coef<<"*X^"<<s->e.expn; s = s->next; } return OK; } // copy the polynomial 'copy' to the polynomial p void CopyPolyn(Polynomial*& P, const Polynomial* copy) { InitList(P); term e; e.coef = 0.0; e.expn = -1; SetCurElem(P, e); LNode* q; LNode* r; LNode* s; r = P; s = copy->next; while (s) { q = new LNode; q->e.coef = s->e.coef; q->e.expn = s->e.expn; q->next = NULL; r->next = q; r = r->next; s = s->next; } } test.cpp,应用算法完成多项式的加法和乘法 [cpp] view plaincopy /* test.cpp */ #include "Experiment1_1.h" void main() { Polynomial* P1; Polynomial* P2; Polynomial* backupP1; Polynomial* backupP2; int m1; int m2; cout<<"/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /"<<endl; cout<<"/ - 实验题1.1 求两多项式的和,积 - /"<<endl; cout<<"/ - - /"<<endl; cout<<"/ - 输入规则:系数 指数 - /"<<endl; cout<<"/ - 例:多项式:3 * x ^ 5,输入3 5 - /"<<endl; cout<<"/ - - /"<<endl; cout<<"/ - 注:各项的指数不能相同,否则会忽略相同项 - /"<<endl; cout<<"/ - 注:系数不能为0,否则会忽略该项 - /"<<endl; cout<<"/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /"<<endl; cout<<endl; cout<<"/ - 输入多项式P1的项数:"; cin>>m1; cout<<"/ - 输入多项式的数据:"<<endl; CreatPolyn(P1, m1); cout<<"/ - P1 = "; PrintPolyn(P1); cout<<endl; cout<<endl; cout<<"/ - 输入多项式P2的项数:"; cin>>m2; cout<<"/ - 输入多项式的数据:"<<endl; CreatPolyn(P2, m2); cout<<"/ - P2 = "; PrintPolyn(P2); cout<<endl; cout<<endl; CopyPolyn(backupP1, P1); CopyPolyn(backupP2, P2); cout<<"/ - P1 + P2 = "; AddPolyn(P1, P2); PrintPolyn(P1); cout<<endl; cout<<endl; DestroyPolyn(P1); cout<<"/ - P1 * P2 = "; MutilplyPolyn(backupP1, backupP2); PrintPolyn(backupP1); cout<<endl; cout<<endl; DestroyPolyn(backupP1);
-
数据结构 多项式相加
2012-11-04 21:29:31C语言版针对数据结构,关于链表的使用,编辑一个小程序进行多项式相加的工呢 -
数据结构多项式相加
2013-11-26 12:47:07完成多项式的相加 等功能,注释全面然后运行正确 -
数据结构多项式相加C++
2009-06-14 18:57:13数据结构作业 多项式相加 VC++ 数据结构作业 多项式相加 VC++ 数据结构作业 多项式相加 VC++ -
VC环境下数据结构多项式相加
2009-07-06 12:29:34VC环境下数据结构多项式相加(简单的2个多项式相加,输入输出可自己输入) -
数据结构多项式相加和相乘
2014-05-05 19:46:55这是数据结构的实验~~ 由于某些实验要上交,所以一部分的程序的代码改为英语注释了 这是求解多项式相加和相乘的算法实现。 采用线性表就可以实现了。 代码可能有点繁杂,因为线性表的实现也包括在里面了。 ...转载于:https://blog.51cto.com/okowo/1406820
-
数据结构之多项式相加
2014-03-24 21:12:33数据结构之多项式相加,用vs2013编译 -
数据结构实验 多项式相加
2011-12-05 23:14:31C语言数据结构书上的简单多项式相加实验,用C++写的. -
数据结构稀疏多项式相加
2019-10-10 16:09:52稀疏多项式相加 #include <stdio.h> #define MAXSIZE 100 #define OVERFLOW 0 #define OK 1 #define ERROR 0 int main(){ } //单链表的结构 typedef struct PNode{ float coef;//系数 int expn;//指数 ...稀疏多项式相加
#include <stdio.h>
#define MAXSIZE 100
#define OVERFLOW 0
#define OK 1
#define ERROR 0
int main(){} //单链表的结构 typedef struct PNode{ float coef;//系数 int expn;//指数 struct PNode *next; } PNode,*Polynomial; //多项式创建 void CreatePolyn(Polynomial &P,int n){ P=new PNode; P->next=NULL; for(int i=0;i<n;i++){ PNode *s=new PNode; scanf("%f%d",&s->coef,&s->expn); s->next=NULL; P->next=s; P=s; } } //多项式的相加 void AddPolyn(Polynomial &Pa,Polynomial &Pb){ PNode *p1=Pa->next; PNode *p2=Pb->next; PNode *p3=Pa; while(p1&&p2){ if(p1->expn==p2->expn){ float sum=p1->coef+p2->coef; if(sum==0){ p1=p1->next; p2=p2->next; }else { p1->coef=sum; p3->next=p1; p3=p1; p1=p1->next; p2=p2->next; } }else if(p1->expn<p2->expn){ p3->next=p1; p3=p1; p1=p1->next; }else{ p3->next=p2; p3=p2; p2=p2->next; } } p3->next=p1?p1:p2; } //欢迎广大网友提出意见,谢谢
-
数据结构链表多项式相加
2009-11-06 10:35:39利用链表来实现多项式相加LinkdeList -
数据结构一元多项式相加.rar
2010-08-30 23:01:45数据结构一元多项式相加 用C语言实现的,功能强大好理解。 -
数据结构 链表 多项式相加 C语言
2010-10-02 23:40:12数据结构 链表 多项式相加 C语言 struct node { int coef; int expo; struct node *next; }; void input(struct node **head) void display(struct node *head) void add(struct node **head1,struct node *head2... -
数据结构 实现多项式相加实验
2008-11-22 09:56:38实现一元多项式相加,有程序代码和实验结果,对于初学数据结构者很有帮助 -
数据结构试验2多项式相加
2012-10-23 21:36:18数据结构试验 多项式相加,是几个比较好的理解数据结构思想的例子。感兴趣的可以看看 -
数据结构 一元多项式相加
2018-03-22 23:21:30做一个豁达而努力的自己。相加的部分也就是用一个新的链表存储,,,和单链表尾插的方法差不多,,,也就是分了3种...//一元多项式的存储结构 typedef struct Node { int coe; //系数 int exp; //指数 Node *nex...做一个豁达而努力的自己。
相加的部分也就是用一个新的链表存储,,,和单链表尾插的方法差不多,,,也就是分了3种情况,,,
代码:
#include <iostream> using namespace std; //一元多项式的存储结构 typedef struct Node { int coe; //系数 int exp; //指数 Node *next; }*polynomial; //一元多项式的初始化 bool InitPoly(polynomial &L) { L = new Node; if(!L) return false; L->next = NULL; return true; } //一元多项式的创建 void CreatePoly(polynomial &L, int n) { polynomial s, r; r = L; while(n--) { int i = 1; s = new Node; cout << "输入第" << i << "项的系数和指数:"; i++; cin >> s->coe; cin >> s->exp; s->next = NULL; r->next = s; r = s; } } //一元多项式相加 polynomial AddPoly(polynomial pa, polynomial pb) { polynomial s; //创建和指针 polynomial r; //创建尾指针 s = new Node; //为和节点创建一个头结点 s->next = NULL; r = s; pa = pa->next; //指向首元结点 pb = pb->next; while(pa && pb) { if(pa->exp > pb->exp) { r->next = pb; r = pb; pb = pb->next; } else if(pa->exp < pb->exp) { r->next = pa; r = pa; pa = pa->next; } else { int sum = pa->coe + pb->coe; if(0 != sum) { pa->coe = pa->coe + pb->coe; r->next = pa; r = pa; } pa = pa->next; pb = pb->next; } } if(NULL != pa) r->next = pa; if(NULL != pb) r->next = pb; return s; } //一元多项式的输出 void PutPoly(polynomial L) { polynomial p; p = L->next; while(p) { cout << "(" << p->coe << "," << p->exp << ")" << '\t'; p = p->next; } cout << endl; } int main() { polynomial pa, pb; InitPoly(pa); InitPoly(pb); cout << "输入多项式a的项数:"; int n; cin >> n; CreatePoly(pa, n); cout << "输入多项式b的项数:"; cin >> n; CreatePoly(pb, n); cout << "a+b的多项式为:"; PutPoly(AddPoly(pa, pb)); return 0; }
-
数据结构——多项式相加、相减
2020-11-27 23:07:50多项式相加: #include<stdio.h> #include<stdlib.h> #define LEN sizeof(Poly) typedef struct term{ float xishu; int zhishu; struct term *next; }Poly,*Link; void CreatePolyn(Link *p,...多项式相加:
#include<stdio.h> #include<stdlib.h> #define LEN sizeof(Poly) typedef struct term{ float xishu; int zhishu; struct term *next; }Poly,*Link; void CreatePolyn(Link *p,int m); void PrintPolyn(Link p); int cmp(Link p1, Link p2); Link AddPolyn(Link pa, Link pb); int main() { Link P1,P2; int L1,L2; printf("第一个多项式的项数:"); scanf("%d",&L1); CreatePolyn(&P1,L1); printf("第一个多项式为:"); PrintPolyn(P1); printf("第二个多项式的项数:"); scanf("%d",&L2); CreatePolyn(&P2,L2); printf("第二个多项式为:"); PrintPolyn(P2); printf("\n"); printf("两个相加的结果为:"); PrintPolyn(AddPolyn(P1, P2)); return 0; } void CreatePolyn(Link *p,int m) { Link r,s; int i; *p=(Link)malloc(LEN); r=*p; for(i=0;i<m;i++) { s=(Link)malloc(LEN); printf("输入系数和指数(以空格隔开):"); scanf("%f %d", &s->xishu, &s->zhishu); r->next=s; r=s; } r->next=NULL; } void PrintPolyn(Link p) { Link s; s = p->next; while(s) { printf("%.2f X^%d", s->xishu, s->zhishu); s = s->next; if(s!=NULL) if(s->xishu>=0) printf("+"); } printf("\n"); } int cmp(Link a, Link b) { if (a->zhishu<b->zhishu) return -1; else if(a->zhishu == b->zhishu) return 0; else return 1; } Link AddPolyn(Link pa, Link pb) { Link newp, p, q, s, pc; float sum; p = pa->next; q = pb->next; newp=(Link)malloc(LEN); pc = newp; while(p&&q){ switch(cmp(p, q)) { case -1: s = (Link)malloc(LEN); s->xishu = p->xishu; s->zhishu = p->zhishu; pc->next = s; pc = s; p = p->next; break; case 0: sum = p->xishu+q->xishu; if(sum!=0.0) { s = (Link)malloc(LEN); s->xishu = sum; s->zhishu = p->zhishu; pc->next = s; pc = s; } p = p->next; q = q->next; break; case 1: s = (Link)malloc(LEN); s->xishu = q->xishu; s->zhishu = q->zhishu; pc->next = s; pc = s; q = q->next; break; } } while(p) { s = (Link)malloc(LEN); s->xishu = p->xishu; s->zhishu = p->zhishu; pc->next = s; pc = s; p = p->next; } while(q) { s = (Link)malloc(LEN); s->xishu = q->xishu; s->zhishu = q->zhishu; pc->next = s; pc = s; q = q->next; } pc->next = NULL; return newp; }
多项式相减:#include<stdio.h> #include<stdlib.h> #include<math.h> #define LEN sizeof(Poly) typedef struct term{ float coef; int expn; struct term *next; }Poly,*Link; int LocateElem(Link p, Link s, Link &q); void CreatePolyn(Link &p,int m); void PrintPolyn(Link p); int cmp(Link a, Link b); Link SubPolyn(Link pa, Link pb); int LocateElem(Link p, Link s, Link &q) { Link p1 = p->next; Link p2 = p; while(p1){ if(s->expn > p1->expn) { p1 = p1->next; p2 = p2->next; } else if(s->expn == p1->expn) { q = p1; return 1; }else { q = p2; return 0; } } if(!p1){ q = p2; return 0; } } void CreatePolyn(Link &p,int m) { Link s,q; int i; p=(Link)malloc(LEN); p->next=NULL; for(i=0;i<m;i++) { s=(Link)malloc(LEN); printf("输入系数和指数(以空格隔开):"); scanf("%f %d", &s->coef, &s->expn); if(!LocateElem(p, s, q)){ s->next = q->next; q->next = s; }else{ q->coef+=s->coef; } } } void PrintPolyn(Link p) { Link s; s = p->next; while(s) { printf(" %.2f X^%d", s->coef, s->expn); s = s->next; if(s!=NULL) if(s->coef>=0) printf(" +"); } printf("\n"); } int cmp(Link a, Link b) { if (a->expn<b->expn) return -1; else if(a->expn == b->expn) return 0; else return 1; } Link SubPolyn(Link pa, Link pb) { Link newp, p, q, s, pc; float sum; newp=(Link)malloc(LEN); pc = newp; p = pa->next; q = pb->next; while(q){ q->coef=0-q->coef; q=q->next; } q=pb->next; while(p&&q){ switch(cmp(p, q)) { case -1: s = (Link)malloc(LEN); s->coef = p->coef; s->expn = p->expn; pc->next = s; pc = s; p = p->next; break; case 0: sum = p->coef+q->coef; if(sum!=0.0) { s = (Link)malloc(LEN); s->coef = sum; s->expn = p->expn; pc->next = s; pc = s; } p = p->next; q = q->next; break; case 1: s = (Link)malloc(LEN); s->coef = q->coef; s->expn = q->expn; pc->next = s; pc = s; q = q->next; break; } } pc->next=p?p:q; return newp; } int main() { Link P1,P2,P3; int L1,L2; printf("请输入第一个多项式的项数:"); scanf("%d",&L1); CreatePolyn(P1,L1); printf("第一个多项式为:"); printf("P1(X)="); PrintPolyn(P1); printf("请输入第二个多项式的项数:"); scanf("%d",&L2); CreatePolyn(P2,L2); printf("第二个多项式为:"); printf("P2(X)="); PrintPolyn(P2); printf("\n"); printf("两个一元多项式相减: "); printf("P1(X)-P2(X)="); P3=SubPolyn(P1, P2); PrintPolyn(P3); return 0; }
-
数据结构——多项式相加
2016-09-22 03:46:15该代码实现多项式的相加 主要函数: 1. 按照升序将结点插入链表 2. 输出链表 3. 两链表相加的过程输入格式请看程序提示。#include #include #include #define FALSE 0 #define TRUE 1 typedef int DataType;... -
数据结构实验一一元多项式相加.docx
2020-11-06 07:30:13数据结构实验报告 实验一一元多项式相加 姓 名 周 成 学 号 13083511 专 业 软件工程 任 课 教 师 马慧珠 2013 年 12 月 01 日 精选 1.实验名称 一元多项式相加 2.实验目的 : 如何使用 C 语言实现链表的说明 创建... -
一元多项式相加数据结构实验一-1307班谭淇蔚_数据结构一元多项式的计算
2019-12-19 21:01:34中南大学 数据结构与算法课程实验 实验报告 题目 实验一线性表的操作 学生姓名 谭淇蔚 学生学号 3901130721 专业班级 软件工程 1307班 完成日期 2014 年 3 月 31 日星期一 实验一线性表的操作一元多项式相加 1.... -
c语言数据结构单链表多项式相加题
2019-03-26 20:35:11【问题描述】编写一个程序用单链表存储多项式,并实现两个一元多项式A与B相加的函数。A,B刚开始是无序的,A与B之和按降序排列。例如: 多项式A: 1.2X^0 2.5X^1 3.2X^3 -2.5X^5 多项式B: -1.2X^0 2.5X^1 3.2X^3 ... -
(数据结构)一元多项式相加
2020-05-08 15:21:36(数据结构)一元多项式相加: 题目说明: 编写一元多项式加法运算程序。要求用线性链表存储一元多项式(参照课本)。该程序有以下几个功能: 多项式求和 输入:输入三个多项式,建立三个多项式链表Pa、Pb、Pc ... -
数据结构学习:多项式相加
2020-09-27 15:05:45多项式相加 #include <stdio.h> #include <stdlib.h> typedef struct LNode{ float coef; //系数 int expo; //指数 struct LNode * next; }LNode; typedef struct LNode* LinkList; void CreateList_L... -
数据结构实验线性表多项式相加
2013-04-21 19:04:08利用线性表(分别用了数组和链表)实现了多项式的相加 -
数据结构的多项式相加的实现
2011-04-05 22:26:11关于还有多项式的加法规则,还可以实现链表的排序功能
收藏数
643
精华内容
257