当前位置:文档之家› 一元多项式的相加减复习过程

一元多项式的相加减复习过程

实验一一元多项式的表示和相减、相乘一、实验目的1.掌握链表的存储方式2.掌握一元多项式的存储及运算。

二、实验内容已知一元多项式P(x)和Q(x)已存在,求P(x)-Q(x)和P(x)* Q(x)并输出。

要求:1.通过键盘随机输入两多项式P(x)和Q(x)的内容。

2.输出结果要有P(x)和Q(x)的以及它们的差P(x)-Q(x)和乘积P(x)* Q(x)。

三、实验步骤:1.创建一元多项P(x)和Q(x)。

2.求P(x)-Q(x),P(x)* Q(x)。

3.输出P(x)、Q(x)、P(x)-Q(x),P(x)* Q(x)。

四、算法说明首先,定义一元多项式的存储方式,然后从键盘输入P(x)和Q(x)对应多项式的各对系数和指数,建立相应的一元多项式五、测试结果参考下图多项式相减多项式相乘六、源代码1.多项式的相减# include<stdio.h># include<malloc.h>typedef struct{float coef; //系数int expn; //指数}ElemType;typedef struct LNode{ //结点类型ElemType data;struct LNode *next;}*LinkList;void MakeNode(LinkList &s,ElemType e){ //生成结点s=(LinkList)malloc(sizeof(LNode));s->data=e;}void InsAfter(LinkList p,LinkList s){//插入结点s->next=p->next;p->next=s;}int compare(ElemType e1,ElemType e2){//比较if(e1.expn>e2.expn)return 1;else if(e1.expn<e2.expn)return -1;return 0;}void OrderInsert(LinkList &L,ElemType e,int(*compare)(ElemType,ElemType)){//有序插入LinkList p=L,q=p->next,s;while(q){int n=compare(e,q->data);if(n<0){MakeNode(s,e);InsAfter(p,s);break;}else if(n==0){q->data.coef=q->data.coef+e.coef;if(q->data.coef==0){p->next=q->next;free(q);}break;}p=p->next;q=p->next;}if(q==NULL){MakeNode(s,e);InsAfter(p,s); //最大,放在最后一个位置}}void InitList(LinkList &L){//初始化L=(LinkList)malloc(sizeof(LNode));L->next=NULL;}void SetCurElem(LinkList &p,ElemType e){ //设置结点p->data.coef=e.coef;p->data.expn=e.expn;}void CreatePolyn(LinkList &L,int m){InitList(L);ElemType e;e.coef=0.0;e.expn=-1;SetCurElem(L,e);//设置头结点的数据元素printf("请输入%d对多项式的值:\n",m);for(int i=1;i<=m;i++){scanf("%f%d",&e.coef,&e.expn); //输入值OrderInsert(L,e,compare);}}void show(LinkList L){//输出方法LinkList p=L->next;if(p){ //第一个输出printf("%.2fX^%d",p->data.coef,p->data.expn);p=p->next;}while(p){if(p->data.coef>0)printf("+");printf("%.2fX^%d",p->data.coef,p->data.expn);p=p->next;}printf("\n");}void ListDestroy(LinkList &L){//销毁LinkList p=L,q;while(p){q=p;p=p->next;free(q);}}void subtract(LinkList L1,LinkList L2,LinkList &L3){ //多项式相减ElemType e;InitList(L3);e.coef=0.0;e.expn=-1;SetCurElem(L3,e);//设置头结点的数据元素LinkList p1=L1->next,p2=L2->next,q;//r1始终指向新建链表的尾部,p1和p2表示当前结点while(p2){p2->data.coef=-p2->data.coef;p2=p2->next;}p2=L2->next;while(p1&&p2){int n=compare(p1->data,p2->data);switch(n){case -1:{OrderInsert(L3,p1->data,compare);p1=p1->next;break;} //L1中的值小,插入之case 1:{OrderInsert(L3,p2->data,compare);p2=p2->next;break;} //L2中的值小,插入之case 0:{ //相同e.coef=p1->data.coef+p2->data.coef;e.expn=p1->data.expn;if(e.coef!=0){OrderInsert(L3,e,compare);}p1=p1->next;p2=p2->next;break;}}}if(p1){OrderInsert(L3,p1->data,compare);p1=p1->next;} //添加L1 else if(p2){OrderInsert(L3,p2->data,compare);p2=p2->next;} //添加L2}LinkList FindThan( LinkList X, LinkList L ){LinkList Tmp;Tmp = L;while( Tmp->next != NULL && Tmp->next->data.expn >= X->data.expn ) { Tmp = Tmp->next;}return Tmp;}int main(){LinkList L1,L2,L3,L4;int m,n;printf("请输入多项式P(X)的项数:");scanf("%d",&m);CreatePolyn(L1,m);printf("多项式P(X)为:\n");show(L1);printf("请输入多项式Q(X)的项数:");scanf("%d",&n);CreatePolyn(L2,n);printf("多项式Q(X)为:\n");show(L2);subtract(L1,L2,L3);printf("多项式P(X)-Q(X)为:\n");show(L3);return 0;}2.多项式的相乘# include<stdio.h># include<malloc.h>typedef struct{float coef; //系数int expn; //指数}ElemType;typedef struct LNode{ //结点类型ElemType data;struct LNode *next;}*LinkList;void MakeNode(LinkList &s,ElemType e){ //生成结点s=(LinkList)malloc(sizeof(LNode));s->data=e;}void InsAfter(LinkList p,LinkList s){//插入结点s->next=p->next;p->next=s;}int compare(ElemType e1,ElemType e2){ //比较if(e1.expn>e2.expn)return 1;else if(e1.expn<e2.expn)return -1;return 0;void OrderInsert(LinkList &L,ElemType e,int(*compare)(ElemType,ElemType)){//有序插入LinkList p=L,q=p->next,s;while(q){int n=compare(e,q->data);if(n<0){MakeNode(s,e);InsAfter(p,s);break;}else if(n==0){q->data.coef=q->data.coef+e.coef;if(q->data.coef==0){p->next=q->next;free(q);}break;}p=p->next;q=p->next;}if(q==NULL){MakeNode(s,e);InsAfter(p,s); //最大,放在最后一个位置}}void InitList(LinkList &L){//初始化L=(LinkList)malloc(sizeof(LNode));L->next=NULL;void SetCurElem(LinkList &p,ElemType e){ //设置结点p->data.coef=e.coef;p->data.expn=e.expn;}void CreatePolyn(LinkList &L,int m){InitList(L);ElemType e;e.coef=0.0;e.expn=-1;SetCurElem(L,e);//设置头结点的数据元素printf("请输入%d对多项式的值:\n",m);for(int i=1;i<=m;i++){scanf("%f%d",&e.coef,&e.expn); //输入值OrderInsert(L,e,compare);}}void show(LinkList L){//输出方法LinkList p=L->next;if(p){ //第一个输出printf("%.2fX^%d",p->data.coef,p->data.expn);p=p->next;}while(p){if(p->data.coef>0)printf("+");printf("%.2fX^%d",p->data.coef,p->data.expn);p=p->next;}printf("\n");}void ListDestroy(LinkList &L){//销毁LinkList p=L,q;while(p){q=p;p=p->next;free(q);}}void Multiply(LinkList L1,LinkList L2,LinkList &L3){//多项式相乘ElemType e;InitList(L3);e.coef=0.0;e.expn=-1;SetCurElem(L3,e);//设置头结点的数据元素LinkList p1=L1->next,p2=L2->next,q;//r1始终指向新建链表的尾部,p1和p2表示当前结点while(p1){p2=L2->next;while(p2){e.coef=p1->data.coef*p2->data.coef;e.expn=p1->data.expn+p2->data.expn;OrderInsert(L3,e,compare);p2=p2->next;}p1=p1->next;} }int main(){LinkList L1,L2,L3;int m,n;printf("请输入多项式P(X)的项数:");scanf("%d",&m);CreatePolyn(L1,m);printf("多项式P(X)为:\n");show(L1);printf("请输入多项式Q(X)的项数:");scanf("%d",&n);CreatePolyn(L2,n);printf("多项式Q(X)为:\n");show(L2);Multiply(L1,L2,L3);printf("多项式P(X)*Q(X)为:\n");show(L3);return 0;}七、八、分析总结本程序从源代码开始经过多次调试,一开始创建多项式并没有遇到什么问题,但是减法开始遇到问题,好在经过调试和反复检验后问题都得以解决,在多项式相加的基础上修改,相减和相乘就容易得多。

相关主题