一元多项式相加C语言代码
一元多项式的运算C程序设计
69 printf("\n"); 70 } 71 void PolynAdd(pPOLYN pa, pPOLYN pb) //多项式相加 pa=pa+pb 72 { 73 pPOLYN pa1, pb1, pc1, p; 74 pa1=pa‐>next; //指向被加链表的第1个有效项 75 pb1=pa‐>next; //指向加链表的第1个有效项 76 pc1=pa; //指向被加链表 77 pc1‐>next=NULL; 78 pc1‐>expn=0; // 清空多项式项目数量 79 while(pa1 && pb1) //两个多项式都未结束 80 { 81 if(pa1‐>expn > pb1‐>expn) // pa1指数大于pb1指数 82 { 83 pc1‐>next=pa1; // 将pa1加入结果链表中 84 pc1=pa1; 85 pa1=pa1‐>next; //处理pa1中的下一项 86 pc1‐>next=NULL; 87 } 88 else 89 { //pa1指数等于pb1指数,进行系数项加 90 pa1‐>coef+=pb1‐>coef; //累加素数 91 if(pa1‐>coef!=0) //若系数不为0 92 { 93 pc1‐>next=pa1; //将相加结果添加到结果链表中 94 pc1=pa1; 95 pa1=pa1‐>next; //处理pa1中的下一项 96 pc1‐>next=NULL; 97 p=pb1; 98 pb1=pb1‐>next; //处理pb1中的下一项 99 free(p); 100 } 101 else // 系数为0, 则不记录该项 102 { 103 p=pa1; // 用临时指针指向pa1中的该项 104 pa1=pa1‐>next; //从链表中删除该项 105 free(p); //释放该项占用内存 106 p=pb1; //用临时指针指向pb1中的该项 107 pb1=pb1‐>next; //从链表中删除该项 108 free(p); // 释放该项占用内存
数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算
#include<stdio.h>#include<stdlib.h>#include<malloc.h>typedef int ElemType;/*单项链表的声明*/typedef struct PolynNode{int coef; // 系数int expn; // 指数struct PolynNode *next;}PolynNode,*PolynList;/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/ /*指数系数一对一对输入*/void CreatePolyn(PolynList &L,int n){int i;PolynList p,q;L=(PolynList)malloc(sizeof(PolynNode)); // 生成头结点L->next=NULL;q=L;printf("成对输入%d个数据\n",n);for(i=1;i<=n;i++){p=(PolynList)malloc(sizeof(PolynNode));scanf("%d%d",&p->coef,&p->expn); //指数和系数成对输入q->next=p;q=q->next;}p->next=NULL;}// 初始条件:单链表L已存在// 操作结果: 依次对L的每个数据元素调用函数vi()。
一旦vi()失败,则操作失败void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType)){PolynList p=L->next;while(p){vi(p->coef, p->expn);if(p->next){printf(" + "); //“+”号的输出,最后一项后面没有“+” }p=p->next;}printf("\n");}/*ListTraverse()调用的函数(类型要一致)*/void visit(ElemType c, ElemType e){if(c != 0){printf("%dX^%d",c,e); //格式化输出多项式每一项}}/* 多项式相加,原理:归并 *//* 参数:两个已经存在的多项式 *//* 返回值:归并后新的多项式的头结点 */PolynList MergeList(PolynList La, PolynList Lb){PolynList pa, pb, pc, Lc;pa = La->next;pb = Lb->next;Lc = pc = La; // 用La的头结点作为Lc的头结点while(pa&&pb){if(pa->expn < pb->expn){pc->next = pa; //如果指数不相等,pc指针连上指数小的结点,pc = pa;pa = pa->next; //指向该结点的指针后移}else if(pa ->expn > pb->expn ){pc->next = pb; //pc指针连上指数小的结点,pc = pb;pb = pb->next; //指向该结点的指针后移}else//(pa ->expn = pb->expn ){pa->coef = pa->coef + pb->coef; //指数相等时,系数相加 pc->next = pa;pc = pa;pa = pa->next; //两指针都往后移pb = pb->next;}}pc->next = pa ? pa:pb; // 插入剩余段return Lc;}void main(){PolynList ha,hb,hc;printf("非递减输入多项式ha, ");CreatePolyn(ha,5); // 正位序输入n个元素的值printf("非递减输入多项式hb, ");CreatePolyn(hb,5); // 正位序输入n个元素的值printf("多项式ha :");PolynTraverse(ha, visit); printf("\n");printf("多项式hb :"); PolynTraverse(hb, visit); printf("\n");hc = MergeList(ha,hb); PolynTraverse(hc, visit); }。
一元多项式相加算法
}
else
{
temp=p->next;free(p);p=temp;
temp=q->next;free(q);q=temp;
}
}
else
{
place->next=q;
pa=InitList(ppa);
printf("你输入的多项式为:\n");
print(pa);
printf("\n");
printf("请分别有序地输入第二个一元多项式的系数和指数(!!已输入系数为0为结束!!):\n");
pb=InitList(ppb);
printf("你输入的多项式为:\n");
place=place->next;
q=q->next;
}
}
if(p!=NULL)
place->next=p;
else
place->next=q;
}
void print(Link p)
{
while(p->next!=NULL)
{
p=p->next;
}* Link;
typedef struct{
Link head,tail;
int len;
}LinkList;
Link InitList(LinkList &L){
int c,e;
Link r;
L.head=(LNode *)malloc(LEN);
一元多项式的各种运算实现(c++,c)
float a=1.0;
for(int i=1;i<=k;i++)
a=a*1e-1;
return a;
}
//赋值,参数是指向头结点的指针
void fuzhi(node *&toup)
{
float fumi(int k);
void init(node *&toup);
if(!toup) init(toup);
if(toup) destory(toup);//假如有头结点或元素节点,销毁结点
toup=(node*)malloc(sizeof(node));
if(!toup){cout<<"溢出错误"<<endl;exit(0ext=NULL;
}
//销毁全部,包括头结点
cout<<"*******************************************************************************"<<endl;
cout<<"举例:3 x3 9.3x-3 -x....."<<endl;
}
//求未知数指数是-k时的值
float fumi(int k)
while(p)
{
if(p->data ==0)
{
if(!pr)
{
tem=p;
toup->next =p->next ;
p=p->next ;
toup->length--;
free(tem);
tem=NULL;
一元多项式加法、减法、乘法实现源代码
⼀元多项式加法、减法、乘法实现源代码////链接程序:#include#include#include//using namespace std;#define N 1000//#define INF 65535void link();void shunxu();void Menu();typedef struct{int a[N];//记录多项式int len;//记录多项式的长度}Ploy;typedef struct //项的表⽰{ float coef; //系数int expn; //指数}term;typedef struct LNode{ term data; //term多项式值struct LNode *next;}LNode,*LinkList; //两个类型名typedef LinkList polynomail; //⽤带头结点的有序链表表⽰多项式/*⽐较指数*/int cmp(term a,term b){ if(a.expn>b.expn) return 1;if(a.expn==b.expn) return 0;if(a.expnelse exit(-2);}/*⼜⼩到⼤排列*/void arrange1(polynomail pa){ polynomail h=pa,p,q,r;if(pa==NULL) exit(-2);for(p=pa;p->next!=NULL;p=p->next); r=p;for(h=pa;h->next!=r;)//⼤的沉底{ for(p=h;p->next!=r&&p!=r;p=p->next)if(cmp(p->next->data,p->next->next->data)==1){ q=p->next->next;p->next->next=q->next;q->next=p->next;p->next=q;}r=p;//r指向参与⽐较的最后⼀个,不断向前移动} }/*由⼤到⼩排序*/void arrange2(polynomail pa){ polynomail h=pa,p,q,r;if(pa==NULL) exit(-2);for(p=pa;p->next!=NULL;p=p->next); r=p;for(h=pa;h->next!=r;)//⼩的沉底{ for(p=h;p->next!=r&&p!=r;p=p->next)if(cmp(p->next->next->data,p->next->data)==1){ q=p->next->next;p->next->next=q->next;q->next=p->next;p->next=q;}r=p;//r指向参与⽐较的最后⼀个,不断向前移动} }/*打印多项式,求项数*/int printpolyn(polynomail P){ int i;polynomail q;if(P==NULL) printf("⽆项!\n");else if(P->next==NULL) printf("Y=0\n");else{ printf("该多项式为Y=");q=P->next;i=1;if(q->data.coef!=0&&q->data.expn!=0){ printf("%.2fX^%d",q->data.coef,q->data.expn); i++; }if(q->data.expn==0&&q->data.coef!=0)printf("%.2f",q->data.coef);//打印第⼀项q=q->next;if(q==NULL){printf("\n");return 1;}while(1)//while中,打印剩下项中系数⾮零的项,{ if(q->data.coef!=0&&q->data.expn!=0){ if(q->data.coef>0) printf("+");printf("%.2fX^%d",q->data.coef,q->data.expn); i++;}if(q->data.expn==0&&q->data.coef!=0){ if(q->data.coef>0) printf("+");printf("%f",q->data.coef);}q=q->next;if(q==NULL){ printf("\n"); break; }}}return 1;}/*1、创建并初始化多项式链表*/polynomail creatpolyn(polynomail P,int m){//输⼊m项的系数和指数,建⽴表⽰⼀元多项式的有序链表P polynomail r,q,p,s,Q;int i;P=(LNode*)malloc(sizeof(LNode));r=P;for(i=0;i{s=(LNode*)malloc(sizeof(LNode));printf("请输⼊第%d项的系数和指数:",i+1);scanf("%f%d",&s->data.coef,&s->data.expn);r->next=s;r=s;}r->next=NULL;if(P->next->next!=NULL){for(q=P->next;q!=NULL/*&&q->next!=NULL*/;q=q->next)//合并同类项for(p=q->next,r=q;p!=NULL;)if(q->data.expn==p->data.expn){q->data.coef=q->data.coef+p->data.coef;r->next=p->next;Q=p;p=p->next;free(Q);}else{r=r->next;p=p->next;}}return P;}/*2、两多项式相加*/polynomail addpolyn(polynomail pa,polynomail pb) {//完成多项式相加运算,即:Pa=Pa+Pb polynomail s,newp,q,p,r;int j;p=pa->next;q=pb->next;newp=(LNode*)malloc(sizeof(LNode));r=newp;while(p&&q)//p&&q都不为空{s=(LNode*)malloc(sizeof(LNode));switch(cmp(p->data,q->data)){case -1: s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s;r=s;p=p->next;break;case 0: s->data.coef=p->data.coef+q->data.coef;if(s->data.coef!=0.0){s->data.expn=p->data.expn;r->next=s;r=s;}p=p->next;q=q->next;break;case 1: s->data.coef=q->data.coef;s->data.expn=q->data.expn;r->next=s;r=s;q=q->next;break;}//switch}//while p||q有⼀个跑完就跳出该循环while(p)//p没跑完{s=(LNode*)malloc(sizeof(LNode));s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s;r=s;p=p->next;}//p跑完跳出循环while(q)//q没跑完{s=(LNode*)malloc(sizeof(LNode));s->data.coef=q->data.coef;s->data.expn=q->data.expn;r=s;q=q->next;}//q跑完跳出循环//p&&q都跑完r->next=NULL;for(q=newp->next;q->next!=NULL;q=q->next)//合并同类项{for(p=q;p!=NULL&&p->next!=NULL;p=p->next)if(q->data.expn==p->next->data.expn){q->data.coef=q->data.coef+p->next->data.coef;r=p->next;p->next=p->next->next;free(r);}}printf("升序 1 , 降序 2\n");printf("选择排序⽅式:");scanf("%d",&j);if(j==1)arrange1(newp);elsearrange2(newp);return newp;}/*3、两多项式相减*/polynomail subpolyn(polynomail pa,polynomail pb){//完成多项式相减运算,即:Pa=Pa-Pbpolynomail s,newp,q,p,r,Q; int j;p=pa->next;q=pb->next;newp=(LNode*)malloc(sizeof(LNode));r=newp;while(p&&q){s=(LNode*)malloc(sizeof(LNode));switch(cmp(p->data,q->data)){case -1: s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s;r=s;p=p->next;break;case 0: s->data.coef=p->data.coef-q->data.coef;if(s->data.coef!=0.0){s->data.expn=p->data.expn;r->next=s;r=s;}p=p->next;q=q->next;break;case 1: s->data.coef=-q->data.coef;s->data.expn=q->data.expn;r->next=s;r=s;}//switch}//whilewhile(p){s=(LNode*)malloc(sizeof(LNode));s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s;r=s;p=p->next;}while(q){s=(LNode*)malloc(sizeof(LNode));s->data.coef=-q->data.coef;s->data.expn=q->data.expn;r->next=s;r=s;q=q->next;}r->next=NULL;if(newp->next!=NULL&&newp->next->next!=NULL)//合并同类项{for(q=newp->next;q!=NULL;q=q->next)for(p=q->next,r=q;p!=NULL;)if(q->data.expn==p->data.expn){q->data.coef=q->data.coef+p->data.coef;r->next=p->next;Q=p;p=p->next;free(Q);}else{r=r->next;p=p->next;}}printf("升序 1 , 降序 2\n");printf("选择:");scanf("%d",&j);if(j==1)arrange1(newp);elsearrange2(newp);return newp;}/*4两多项式相乘*/polynomail mulpolyn(polynomail pa,polynomail pb){//完成多项式相乘运算,即:Pa=Pa*Pbpolynomail s,newp,q,p,r;int i=20,j;newp=(LNode*)malloc(sizeof(LNode));r=newp;for(p=pa->next;p!=NULL;p=p->next)for(q=pb->next;q!=NULL;q=q->next){s=(LNode*)malloc(sizeof(LNode));s->data.coef=p->data.coef*q->data.coef;s->data.expn=p->data.expn+q->data.expn;r->next=s;r=s;}r->next=NULL;printf("升序 1 , 降序 2\n");printf("选择:");scanf("%d",&j);if(j==1)arrange1(newp);elsearrange2(newp);for(;i!=0;i--){for(q=newp->next;q->next!=NULL;q=q->next)//合并同类项for(p=q;p!=NULL&&p->next!=NULL;p=p->next)if(q->data.expn==p->next->data.expn){q->data.coef=q->data.coef+p->next->data.coef;r=p->next;p->next=p->next->next;free(r);}}return newp;}/*5、销毁已建⽴的两个多项式*/void delpolyn(polynomail pa,polynomail pb){polynomail p,q;p=pa;while(p!=NULL){q=p;p=p->next;free(q);}p=pb;while(p!=NULL){q=p;p=p->next;free(q);}printf("两个多项式已经销毁\n");}void Menulink(){printf("\n");printf(" ********⼀元多项式链式存储的基本运算********\n"); printf(" 1、创建两个⼀元多项式请按1\n");printf(" 2、两多项式相加得⼀新多项式请按2\n");printf(" 3、两多项式相减得⼀新多项式请按3\n");printf(" 4、两多项式相乘得⼀新多项式请按4\n");printf(" 5、销毁已建⽴的两个多项式请按5\n");printf(" 6、退出该⼦系统返回主菜单请按6\n");printf(" 7、退出该系统请按7\n");printf(" ********************************************\n");printf("\n");}void link() //⼀元多项式链式存储的实现{polynomail pa=NULL,pb=NULL;polynomail p,q;polynomail addp=NULL,subp=NULL,mulp=NULL; int n,m;printf("已进⼊链式存储⼀元多项式运算的⼦系统\n"); Menulink();while(1){printf("请选择你想进⾏的链式存储运算操作:\n"); scanf("%d",&n);switch(n){case 1:if(pa!=NULL){printf("已建⽴两个⼀元多项式,请选择其他操作!"); break;}printf("请输⼊第⼀个多项式:\n");printf("要输⼊⼏项:");scanf("%d",&m);while(m==0){printf("m不能为0,请重新输⼊m:");scanf("%d",&m);}pa=creatpolyn(pa,m);printpolyn(pa);printf("请输⼊第⼆个多项式:\n");printf("要输⼊⼏项:");scanf("%d",&m);while(m==0){printf("m不能为0,请重新输⼊m:");scanf("%d",&m);}pb=creatpolyn(pb,m);printpolyn(pb);break;case 2:if(pa==NULL){printf("请先创建两个⼀元多项式!\n");break;}addp=addpolyn(pa,pb);printpolyn(addp);break;case 3:if(pa==NULL){printf("请先创建两个⼀元多项式!\n");break;}subp=subpolyn(pa,pb);printpolyn(subp);break;case 4:if(pa==NULL){printf("请先创建两个⼀元多项式!\n"); break;}mulp=mulpolyn(pa,pb);printpolyn(mulp);break;case 5:if(pa==NULL){printf("请先创建两个⼀元多项式!\n"); break;}delpolyn(pa,pb);pa=pb=NULL;printf("两个⼀元多项式的销毁成功!\n"); break;case 6:if(addp!=NULL){p=addp;while(p!=NULL){q=p;p=p->next;free(q);}}if(subp!=NULL){p=subp;while(p!=NULL){q=p;p=p->next;free(q);}}printf("返回主菜单\n");Menu();break;case 7:if(addp!=NULL){p=addp;while(p!=NULL){q=p;p=p->next;free(q);}}if(subp!=NULL){p=subp;while(p!=NULL){q=p;p=p->next;free(q);}}printf("已成功退出该系统,谢谢你的使⽤!\n");exit(-2);break;}//switch}//while}//2、顺序程序:void ADD(Ploy A,Ploy B,Ploy *M)/*多项式A与多项式B相加,得到多项式M*/{int la=A.len,lb=B.len,i;M->len=la>lb?la:lb;for(i=0;i<=la&&i<=lb;i++){M->a[i]=A.a[i]+B.a[i];}while(i<=la){M->a[i]=A.a[i];i++;}while(i<=lb){M->a[i]=B.a[i];i++;}return;}void SUB(Ploy A,Ploy B,Ploy *M)/*多项式A与多项式B相减(A-B),得到多项式M*/{int la=A.len,lb=B.len,i;M->len=la>lb?la:lb;for(i=0;i<=la&&i<=lb;i++){M->a[i]=A.a[i]-B.a[i];}while(i<=la) {M->a[i]=A.a[i];i++;}while(i<=lb) {M->a[i]=0-B.a[i];i++;}return ;}void MUL(Ploy A,Ploy B,Ploy *M)/*多项式A与多项式B相乘,得到多项式M*/{int i,j;for(i=0;i<=A.len+B.len+1;i++) M->a[i]=0;for(i=0;i<=A.len;i++)for(j=0;j<=B.len;j++){M->a[i+j]+=A.a[i]*B.a[j];}M->len=A.len+B.len;return ;}void GetPloy(Ploy *A){int i,coef,ex,maxe=0;//ex指指数,maxe指最⼤指数char ch;printf("请输⼊每个项的系数及对应的指数,指数为负数时标志输⼊结束!\n");for(i=0;iA->a[i]=0;scanf("%d%d",&coef,&ex);while(ex>=0){if(ex>maxe)maxe=ex;if(A->a[ex]!=0){printf("你输⼊的项已经存在,是否更新原数据?(Y/N)"); cin>>ch;if(ch=='Y'||ch=='y'){A->a[ex]=coef;printf("更新成功,请继续输⼊!\n");}elseprintf("请继续输⼊!\n");;}elseA->a[ex]=coef;scanf("%d%d",&coef,&ex);}A->len=maxe;return ;}void PrintPloy1(Ploy A)//降序输出顺序⼀元多项式{int i;printf(" %dx^%d ",A.a[A.len],A.len);for(i=A.len-1;i>=1;i--){if(A.a[i]==0) ;else if(A.a[i]==1) printf(" + x^%d ",i);else if(A.a[i]==-1) printf(" - x^%d ",i);else{if(A.a[i]>0)printf("+ %dx^%d ",A.a[i],i);elseprintf("- %dx^%d ",-A.a[i],i);}}if(A.a[0]==0) ;else if(A.a[0]>0)printf(" + %d",A.a[0]);//打印x的0次项elseprintf(" - %d",-A.a[0]);printf("\n");return ;}void PrintPloy2(Ploy A)//升序输出顺序⼀元多项式{int i=0;while(A.a[i]==0)++i;if(i==0)printf("%d",A.a[i]);else{if(A.a[i]==1)printf("x^%d",i);else if(A.a[i]==-1)printf("-x^%d",i);elseprintf("%dx^%d",A.a[i],i);}for(++i;i<=A.len;i++){if(A.a[i]==0) ;else if(A.a[i]==1)printf(" + x^%d",i);else if(A.a[i]==-1)printf(" - x^%d",i);else if(A.a[i]>1)printf(" + %dx^%d",A.a[i],i);else if(A.a[i]<-1)printf(" - %dx^%d",-A.a[i],i);}}void Menushunxu(){printf("\n");printf(" ********⼀元多项式顺序存储的基本运算********\n");printf(" 1、更新两个多项式⼀元多项式请按1\n");printf(" 2、两多项式相加得⼀新多项式请按2\n");printf("3、两多项式相减得⼀新多项式请按3\n");printf(" 4、两多项式相乘得⼀新多项式请按4\n");printf(" 5、退出该⼦系统,返回主菜单请按5\n");printf(" 6、退出该系统请按6\n");printf(" ********************************************\n");printf("\n");return ;}void shunxu() //⼀元多项式顺序存储的实现{Ploy A,B,M;int n,m;printf("进⼊顺序存储⼀元多项式运算⼦系统\n");printf("请输⼊多项式A:\n");GetPloy(&A);printf("请输⼊多项式B:\n");GetPloy(&B);printf("输出两个⼀元多项式A、B,降幂输出请按1,升幂输出请按2!\n"); cin>>m;while(m<1&&m>m){printf("你输⼊的输出新创⼀元多项式的操作号不合法,请重新输⼊\n"); cin>>m;}switch(m){case 1:if(m==1){printf("A降=");PrintPloy1(A);printf("\n");printf("B降=");PrintPloy1(B);break;case 2:if(m==2){printf("A升=");PrintPloy1(A);printf("\n");printf("B升=");PrintPloy1(B);}break;}Menushunxu();while(1){printf("请选择你想进⾏的顺序存储运算操作:\n");cin>>n;while(n<1&&n>6){printf("输⼊的顺序操作号不对,请重新输⼊\n");cin>>n;}switch(n){case 1:if(n==1)printf("更新两个多项式:\n");printf("请输⼊多项式A:\n");GetPloy(&A);printf("请输⼊多项式B:\n");GetPloy(&B);printf("输出两个更新的⼀元多项式A、B,降幂输出请按1,升幂输出请按2!\n"); cin>>m;while(m<1&&m>2){printf("你输⼊的输出排序操作号不合法,请重新输⼊\n");cin>>m;}switch(m){case 1:if(m==1){printf("A降=");PrintPloy1(A);printf("\n");printf("B降=");PrintPloy1(B);}break;case 2:if(m==2){printf("A升=");PrintPloy1(A);printf("\n");printf("B升=");PrintPloy1(B);}break;break;case 2:if(n==2)ADD(A,B,&M);printf("降幂输出请按1,升幂输出请按2!\n");cin>>m;while(m<1&&m>2){printf("你输⼊的输出排序操作号不合法,请重新输⼊\n"); cin>>m;}switch(m){case 1:if(m==1){printf("ADD降=");PrintPloy1(M);printf("\n");}break;case 2:if(m==2){printf("ADD升=");PrintPloy2(M);printf("\n");}break;}break;case 3:if(n==3)SUB(A,B,&M);printf("降幂输出请按1,升幂输出请2!\n");cin>>m;while(m<1&&m>2){printf("你输⼊的输出排序操作号不合法,请重新输⼊\n"); cin>>m;}switch(m){case 1:if(m==1){printf("SUB降=");PrintPloy1(M);printf("\n");}break;case 2:if(m==2){printf("SUB升=");PrintPloy2(M);printf("\n");}break;}break;case 4:if(n==4)MUL(A,B,&M);printf("降幂输出请按1,升幂输出请2!\n");cin>>m;while(m<1&&m>3){printf("你输⼊输出排序操作号不合法,请重新输⼊\n"); cin>>m;}switch(m){case 1:if(m==1){printf("MUL降=");PrintPloy1(M);printf("\n");}break;case 2:if(m==2){printf("MUL升=");PrintPloy2(M);printf("\n");}break;}break;case 5:if(n==5)printf("返回主菜单\n");Menu();break;case 6:if(n==6)printf("已成功退出该系统,谢谢你的使⽤!\n");exit(-2);break;}}}void Menu(){printf("\n");printf(" ************⼀元多项式的基本运算系统************\n"); printf(" 1、⼀元多项式顺序存储的⼦系统请按1\n");printf(" 2、⼀元多项式链式存储的基本运算请按2\n"); printf(" 3、退出系统请按3\n");printf(" ************************************************\n"); printf("\n");printf("请输⼊你想进⾏的操作号:\n");int n;scanf("%d",&n);while(n!=1 && n!=2 && n!=3){printf("对不起,你的输⼊不正确,请重新输⼊!\n"); scanf("%d",&n);}switch(n){case 1:if(n==1)shunxu();break;case 2:if(n==2)link();break;case 3:if(n==3)printf("已成功退出该系统,谢谢你的使⽤!\n"); exit(-2);}}void main(){Menu();}。
一元多项式加减乘除运算
#include <iostream>#include <stdio.h>using namespace std;class Node{public:int Coe;int Exp;Node *next;};class List{public://初始化构造函数,别忘了初始化Node *InsertNode(Node *Head,Node *Ipt); //声明一个输入节点函数Node *InsertTerm(); //声明一个输入项函数void ShowPloy(Node *Head); //显示多项式的函数Node *AddPloy(Node *Head1,Node *Head2); //声明一个加法函数Node *SubPloy(Node *Head1,Node *Head2); //声明一个减法函数Node *MulPloy(Node *Head1,Node *Head2); //声明一个乘法函数void MainShow(); //程序界面设计};Node *List::InsertNode(Node *Head,Node *Ipt) //插入节点函数,返回值为空{int Flag=0; //初始化一个信号,便于我后面的判断Node *pre,*now; //定义两个指针,分别代表我们之前的位置以及现在的位置pre=Head; //将头指针赋给pre,即我们之前的位置在头结点if (pre->next==NULL){pre->next=Ipt; //此时的Ipt就是我们传入的ptr指针,他是我们要插入的那个指针,这行语句是在我们已经判断了头指针后面为空的情况下,我们将Ipt赋给头指针的后一项}else{now=pre->next; //如果头指针后面的值不为空,那么代表着这个链表已经开始存储数据,所以我们需要遍历链表,同时我们将现在的位置定在头指针的后面,然后开始依次比较,看插入的节点的幂数与现在指针的幂数哪个大,若比现在大,那么插到现在位置的前面,若小,那么继续比较,若相同,那么合并这两项///接下来会是一系列if的判断语句,OK,如果你现在有点头晕那么先休息片刻,再来看这段程序,当然这还不算什么while(Flag==0){if ((Ipt->Exp)<(now->Exp)) //判断结果为插入指针的幂数小于现在指针的幂数,执行花括号里面的语句{if (now->next==NULL) //如果现在指针后面的指针值为空,执行花括号里面的语句{now->next=Ipt;Flag=1; //标记Flag变为1,跳出while 循环}else //看清楚,这个else是表示如果现在指针后面指针的值不为空的情况,那么执行花括号的语句{pre=now; //如果接下来还有值得话,那么我就将现在的指针定义为我之前的指针now=pre->next; //而我的位置将向后移动一位,我现在的位置变成了now之后的那个指针,然后我继续做判断}}else if(Ipt->Exp>now->Exp) //这个else表示的是插入节点的幂数大于现在节点的幂数时,执行花括号内的语句{Ipt->next=now; //当插入的幂数大于现在的幂数时,我们就只需将插入的节点放到现在节点的前面即可,那么我们就将插入节点的后面的指针定义为现在的指针pre->next=Ipt; //之前的指针指向我们现在的插入指针Flag=1; //标记Flag变为1,,跳出while循环}else //这个else表示我们的插入节点幂数既不大于也不小于现在的节点幂数,那么很显然,这个表示的是幂数相同的情况。
一元稀疏多项式运算(数据结构c语言)
{ CreateItem(p);
p->coef=coef;
p->expn=expn;
insert(pp,p);
}
else if(Select("has the same expn,Replace older value?"))
/************************************************************///要添加
Polyn SubtractPolyn(Polyn h1,Polyn h2)
{
//int flag;
Item *head,*last,*pa=h1->next,*pb=h2->next,*s,*s0;
}
else if(q->expn==expn)
{ *p=q;
return(0);
}
else
{ *p=pre;
return(-1);
}
}
/************************************************************/
/************************************************************/
int ItemComp(Item x,Item y)
{ if(x.expn<y.expn)
return(-1);
else if(x.expn==y.expn)
h->next=NULL;
一元多项式的计算代码
一元多项式的计算代码:#include<iostream.h>#include <stdlib.h>#include <math.h>typedef struct Polynomial{float coe; //系数float exp;//指数struct Polynomial *next;}*Polyn,Polynomial;Polyn ma,mb;void Insert(Polyn p,Polyn h){if(p->coe==0) delete p;else{Polyn q1,q2;q1=h;q2=h->next;while(q2&&p->exp<q2->exp){q1=q2;q2=q2->next;}if(q2&&p->exp==q2->exp){q2->coe+=p->coe;delete p;if(!q2->coe){q1->next=q2->next;delete q2;}}else{p->next=q2;q1->next=p;}}}Polyn CreatePolyn(Polyn head,int m) {int i;Polyn p;p=head=new Polynomial;head->next=NULL;for(i=0;i<m;i++){p=new Polynomial;;cout<<"请输入第"<<i+1<<"项的系数:";cin>>p->coe;cout<<" 指数:";cin>>p->exp;Insert(p,head);}return head;}void DestroyPolyn(Polyn p){Polyn t;while(p!=NULL){t=p;p=p->next;delete t;}}void PrintPolyn(Polyn Pm){Polyn qa=Pm->next;int flag=1;if(!qa){cout<<"0";cout<<endl;return;}while (qa){if(qa->coe>0&&flag!=1) cout<<"+";if(qa->coe!=1&&qa->coe!=-1){cout<<qa->coe;if(qa->exp==1) cout<<"X";else if(qa->exp) cout<<"X^"<<qa->exp;}else{if(qa->coe==1){if(!qa->exp) cout<<"1";else if(qa->exp==1) cout<<"X";else cout<<"X^"<<qa->exp;}if(qa->coe==-1){if(!qa->exp) cout<<"-1";else if(qa->exp==1) cout<<"-X";else cout<<"-X^"<<qa->exp;}}qa=qa->next;flag++;}cout<<endl;}int compare(Polyn a,Polyn b){if(a&&b){if(!b||a->exp>b->exp) return 1;else if(!a||a->exp<b->exp) return -1;else return 0;}else if(!a&&b) return -1;else return 1;}Polyn AddPolyn(Polyn pa,Polyn pb){Polyn qa=pa->next;Polyn qb=pb->next;Polyn headc,hc,qc;hc=new Polynomial;hc->next=NULL;headc=hc;while(qa||qb){qc=new Polynomial;switch(compare(qa,qb)){case 1:{qc->coe=qa->coe;qc->exp=qa->exp;qa=qa->next;break;}case 0:{qc->coe=qa->coe+qb->coe;qc->exp=qa->exp;qa=qa->next;qb=qb->next;break;}case -1:{qc->coe=qb->coe;qc->exp=qb->exp;qb=qb->next;break;}}if(qc->coe!=0){qc->next=hc->next;hc->next=qc;hc=qc;}else delete qc;}return headc;}Polyn SubtractPolyn(Polyn pa,Polyn pb){Polyn h=pb;Polyn p=pb->next;Polyn pd;while(p){p->coe*=-1;p=p->next;pd=AddPolyn(pa,h);for(p=h->next;p;p=p->next)p->coe*=-1;return pd;}Polyn MultiplyPolyn(Polyn pa,Polyn pb){Polyn hf,pf;//Polyn qa=pa->next; //新建一个结点作为pa的后继结点Polyn qb=pb->next; //新建一个结点作为pb的后继结点hf=new Polynomial;hf->next=NULL;while(qa)//使用while循环,使得多项式的每项得以运算{qb=pb->next;while(qb){pf=new Polynomial;pf->coe=qa->coe*qb->coe;pf->exp=qa->exp+qb->exp;Insert(pf,hf);//调用插入函数,将新的结点插入到新建链表中,并合并同类项qb=qb->next;}qa=qa->next;}return hf;//返回所得链表的头指针}void DevicePolyn(Polyn pa,Polyn pb){Polyn quotient,remainder,temp1,temp2;Polyn qa=pa->next;Polyn qb=pb->next;quotient=new Polynomial; //建立头结点,存储商quotient->next=NULL;remainder=new Polynomial; //建立头结点,存储余数remainder->next=NULL;temp1=new Polynomial;temp1->next=NULL;temp2=new Polynomial;temp2->next=NULL;temp1=AddPolyn(temp1,pa);while(qa!=NULL&&qa->exp>=qb->exp)temp2->next=new Polynomial;temp2->next->coe=(qa->coe)/(qb->coe);temp2->next->exp=(qa->exp)-(qb->exp);Insert(temp2->next,quotient);pa=SubtractPolyn(pa,MultiplyPolyn(pb,temp2));qa=pa->next;temp2->next=NULL;}remainder=SubtractPolyn(temp1,MultiplyPolyn(quotient,pb));pb=temp1;cout<<endl<<"shang"<<endl;//printf("\t商:");PrintPolyn(quotient);cout<<"yushu"<<endl;//printf("\t余数:");PrintPolyn(remainder);}float ValuePolyn(Polyn head,float x){Polyn p;p=head->next;float result=0;while(p!=NULL){result+=(p->coe)*(float)pow(x,p->exp);p=p->next;}return result;}void desktop(){system("cls");cout<<endl<<endl<<endl<<" 一元多项式的计算"<<endl;cout<<"**********************************************"<<endl;cout<<" ** 1.输出多项式a和 b **"<<endl;cout<<" ** 2.建立多项式a+b **"<<endl;cout<<" ** 3.建立多项式a-b **"<<endl;cout<<" ** 4.建立多项式a*b **"<<endl;cout<<" ** 5.建立多项式a/b**"<<endl;cout<<" ** 6.计算多项式a的值**"<<endl;cout<<" ** 7.退出**"<<endl;cout<<"**********************************************"<<endl<<endl;cout<<" 执行操作:";}void input(){int m,n;//Polyn pa,pb;cout<<"请输入多项式a的项数:";cin>>m;ma=CreatePolyn(ma,m);cout<<endl;cout<<"请输入多项式b的项数:";cin>>n;mb=CreatePolyn(mb,n);}void main(){//int m,n;float x,result;char key;//Polyn pa,pb;cout<<endl<<endl<<endl<<endl<<" 欢迎您的使用!"<<endl;cout<<" 系统正在初始化数据,请稍后..."<<endl;_sleep(3*1000);system("cls");while(key){desktop();cin>>key;switch (key){case'1':input();cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);break;case'2':input();//pc=AddPolyn(pa,pb);cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);cout<<"多项式a+b:";PrintPolyn(AddPolyn(ma,mb));//DestroyPolyn(pc);break;case'3':input();//pd=SubtractPolyn(pa,pb);cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);cout<<"多项式a-b:";PrintPolyn(SubtractPolyn(ma,mb));//DestroyPolyn(pd);break;case'4':input();//pd=SubtractPolyn(pa,pb);cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);cout<<"多项式a*b:";PrintPolyn(MultiplyPolyn(ma,mb));//DestroyPolyn(pd);break;case'5':input();//pd=SubtractPolyn(pa,pb);cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);cout<<"多项式a/b:";DevicePolyn(ma,mb);//DestroyPolyn(pd);break;case'6':input();cout<<"多项式a:";PrintPolyn(ma);cout<<"输入x的值:x=";cin>>x;result=ValuePolyn(ma,x);cout<<"多项式a的值:"<<result<<endl;break;case'7':DestroyPolyn(ma);DestroyPolyn(mb);exit(0);break;default:cout<<"Error!!!"<<endl;}cout<<endl<<endl;system("pause");}}。
C语言实现一元多项式相加(源代码)
C语言实现一元多项式相加(源代码)#include<stdio.h>#include<malloc.h>//动态申请空间的函数的头文件typedef struct node //定义节点类型{float coef; //多项式的系数int expn; //多项式的指数struct node * next; //结点指针域}PLOYList;void insert(PLOYList *head,PLOYList *input) //查找位置插入新链节的函数,且让输入的多项式呈降序排列{PLOYList *pre,*now;int signal=0;pre=head;if(pre->next==NULL) {pre->next=input;} //如果只有一个头结点,则把新结点直接连在后面else{now=pre->next;//如果不是只有一个头结点,则设置now指针while(signal==0){if(input->expn < now->expn){if(now->next==NULL){now->next=input;signal=1;}else{pre=now;now=pre->next;//始终让新输入的数的指数与最后一个结点中的数的指数比较,小于则插在其后面}}else if( input->expn > now->expn ){input->next=now;pre->next=input;signal=1;}//若新结点中指数比最后一个结点即now中的指数大,则插入now之前else//若指数相等则需合并为一个结点,若相加后指数为0则释放该结点{now->coef=now->coef+input->coef;signal=1;free(input);if(now->coef==0){pre->next=now->next;free(now);}}//else} //while}//else}//voidPLOYList *creat(char ch) //输入多项式{PLOYList *head,*input;float x;int y;head=(PLOYList *)malloc(sizeof(PLOYList)); //创建链表头head->next=NULL;scanf("%f %d",&x,&y);//实现用户输入的第一个项,包括其指数和系数while(x!=0)//当用户没有输入结束标志0时可一直输入多项式的项,且输入一个创建一个结点{input=(PLOYList *)malloc(sizeof(PLOYList)); //创建新链节input->coef=x;input->expn=y;input->next=NULL;insert(head,input); //每输入一项就将其排序,是的链表中多项式呈降序排列scanf("%f %d",&x,&y);}return head;}PLOYList *add(PLOYList *head,PLOYList *pre) //多项式相加,head为第一个多项式建立的链表表头,pre 为第二个多项式建立的链表表头{PLOYList *input;int flag=0;while(flag==0){if(pre->next==NULL)flag=1; //若该链表为空,则无需进行加法运算,跳出循环else{pre=pre->next;input=(PLOYList *)malloc(sizeof(PLOYList));//申请空间input->coef=pre->coef;input->expn=pre->expn;input->next=NULL;insert(head,input); // 把g(x)插入到f(x)中,相当于两者相加,结果保存于f(x) }}return head;}void print(PLOYList *fun) //输出多项式,fun指要输出的多项式链表的表头{PLOYList *printing;int flag=0;printing=fun->next;if(fun->next==NULL)//若为空表,则无需输出{printf("0\n");return;}while(flag==0){if(printing->coef>0&&fun->next!=printing)printf("+");if(printing->coef==1);else if(printing->coef==-1)printf("-");elseprintf("%f",printing->coef);if(printing->expn!=0) printf("x^%d",printing->expn);else if((printing->coef==1)||(printing->coef==-1))printf("1");if(printing->next==NULL)flag=1;elseprinting=printing->next;}printf("\n");}void start() //用户选择界面{printf(" * 1.两个一元多项式相加*\n");printf(" * 0.退出系统*\n");printf(" \n");printf(" 注:输入多项式格式为:系数1 指数1 系数2 指数2 ……,并以0 0 结束:\n");printf(" \n");printf(" 请选择操作: ");}void main(){PLOYList *f,*g,*pf,*hf,*p;int sign=-1;start();while(sign!=0){scanf("%d",&sign);switch(sign){case 0:break;case 1://多项式相加{printf(" 你选择的操作是多项式相加:\n");printf(" 请输入第一个多项式f(x):");f=creat('f');printf(" 第一个多项式为:f(x)=");print(f);printf(" 请输入第二个多项式g(x):");g=creat('g');printf(" 第二个多项式为:g(x)=");print(g);printf(" 结果为:F(x)=f(x)+g(x)=");f=add(f,g);print(f);printf("\n\n");printf(" 继续请选择相应操作,退出请按0. ");break;}}}}。
数据结构课程设计—一元多项式加法、减法、乘法运算的实现
1。
一元多项式加法、减法、乘法运算的实现1。
1设计内容及要求1)设计内容(1)使用顺序存储结构实现多项式加、减、乘运算。
例如:10321058)(2456+-+-+=x x x x x x f ,x x x x x x g +--+=23451020107)(求和结果:102220128)()(2356++-+=+x x x x x g x f(2)使用链式存储结构实现多项式加、减、乘运算,10305100)(1050100+-+=x x x x f ,x x x x x x g 320405150)(10205090+++-=求和结果:1031040150100)()(102090100++-++=+x x x x x x g x f2)设计要求(1)用C 语言编程实现上述实验内容中的结构定义和算法。
(2)要有main()函数,并且在main()函数中使用检测数据调用上述算法.(3)用switch 语句设计如下选择式菜单.***************数据结构综合性实验***********************一、多项式的加法、减法、乘法运算***************** 1。
多项式创建 ***************** 2。
多项式相加 ***************** 3.多项式相减 ***************** 4。
多项式相乘 ***************** 5。
清空多项式 ***************** 0.退出系统 ***************** 请选择(0—5) ************************************************************请选择(0—5):1.2数据结构设计根据下面给出的存储结构定义:#define MAXSIZE 20 //定义线性表最大容量//定义多项式项数据类型typedef struct{float coef; //系数int expn; //指数}term,elemType;typedef struct{term terms[MAXSIZE]; //线性表中数组元素int last; //指向线性表中最后一个元素位置}SeqList;typedef SeqList polynomial;1。
一元多项式求解(cc++)源程序
break;
}
}while(tag==1);//考虑多项式名重复情况
q=(name *)malloc(sizeof(name));
strcpy(q->s,s2);
q->next=pname->next;
pname->next=q;//头插法
PrintPoly(pname);//输出所有的多项式
}
void Plus(name *pname){//多项式相加
char s[2][N],s2[N];
name *p[2],*q;
node *pp,*qq,*rr,*ss;
int tag,i;
PrintPoly(pname);//输出所有多项式
if(strcmp(q->s,s)==0){
printf("多项式名有冲突,请重新输入:");
scanf("%s",s);
tag=1;
break;
}
}while(tag==1);//考虑多项式名重复情况
strcpy(p->s,s);
printf("请输入多项式的项数(输入数字):");
scanf("%s",s2);
tag=1;
break;
}
}while(tag==1);//考虑多项式名重复情况
q=(name *)malloc(sizeof(name));
strcpy(q->s,s2);
q->next=pname->next;
pname->next=q;//头插法
一元符号多项式c语言版
数据结构实验室开放项目实验报告题目:一元符号多项式的表示及运算姓名:班级:学号:一、内容总结1.算法概要设计:①一元多项式的创建Status CreatPolyn(polynomail P)②一元多项式的加法void AddPolyn(polynomail Pa,polynomail Pb)③一元多项式的减法void SubPolyn(polynomail Pa,polynomail Pb)④一元多项式的乘法⑤void MultPolyn(polynomail P a,polynomail Pb)2.发现的问题:根据上面的设计纲要展开程序的设计,在程序编写和调试过程中主要遇到以下几点问题:①函数形参与实参的传递情况,经常忘记传值不传址的概念,导致程序运行错误。
在初始化和建立多项式等一些以改变原来多项式的一定要用指针传递。
②程序运行控制条件考虑不足,例如在乘法函数的循环控制中,条件选取错误,使得结果不正确。
③链表的链接混乱,链表链接有疏漏,指针链接情况考虑不周全,例如在加法case1中,没有移动ta,导致结果错误。
④程序编写技巧运用,减法就是加法,编写取反函数,将被减数各项取反,减法可用加法实现。
在乘法中,也是利用加法实现。
3.解决方法:通常采用的方法为自我思考摸索,主要手段是查资料和上网搜索,然后与同学间相互讨论,请教老师。
当自我期望与现实差距过大时,自行规避一段时间,自行调节情绪,有意识的讲问题随身携带,随时随地的思考。
二、实验展示1.输入要求按照多项式指数递增的顺序,依次输入多项式的参数对,第一个参数为系数,后一个参数为指数,如输入1 2,表示x^2项。
以输入系数为零的项为结束。
例:1 2 3 4 5 6 8 9 0 3然后回车,就会建立一元多项式:x^2+3x^4+5x^6+8x^9。
2.输出要求输出内容为建立和运算之后的结果。
多项式有序的按照指数递增的顺序,输出参数对,以小括号为一个单位,第一个为系数,第二个为指数。
一元多项式相加C语言代码
数据结构作业一元多项式相加C语言代码#include<stdio.h>#include<malloc.h>typedef struct node{int exp,coef;struct node *link;}PolyNode,*Polylinklist;Polylinklist Creat(int n){Polylinklist p,r=NULL,list=NULL;int coef,exp,i;for(i=1;i<=n;i++){scanf("%d %d",&coef,&exp);p=(Polylinklist)malloc(sizeof(PolyNode));p->coef=coef;p->exp=exp;p->link=NULL;if(list==NULL)list=p;elser->link=p;r=p;}return(list);}Polylinklist ATTACH(int coef,int exp,Polylinklist r){Polylinklist w;w=(Polylinklist)malloc(sizeof(PolyNode));w->exp=exp;w->coef=coef;r->link=w;return w;}Polylinklist PADD(Polylinklist a,Polylinklist b){Polylinklist c;Polylinklist r,p=a,q=b;int x;c=(Polylinklist)malloc(sizeof(PolyNode));r=c;while(p!=NULL&&q!=NULL)if(p->exp==q->exp){x=p->coef+q->coef;if(x!=0)r=ATTACH(x,q->exp,r);p=p->link;q=q->link;}else if(p->exp<q->exp){r=ATTACH(q->coef,q->exp,r);q=q->link;}else {r=ATTACH(p->coef,p->exp,r);p=p->link;}while(p!=NULL){r=ATTACH(p->coef,p->exp,r);p=p->link;}while(q!=NULL){r=ATTACH(q->coef,q->exp,r);q=q->link;}r->link=NULL;p=c;c=c->link;free(p);return c;}void Result(Polylinklist w){Polylinklist m;m=w;while(w==NULL){printf("0");break;}while(w!=NULL){if(w->exp==0)printf("%d",w->coef);elseprintf("%d*x^%d",w->coef,w->exp);w=w->link;while(w!=NULL){if(w->coef>0){if(w->exp==0)printf("+%d",w->coef);elseprintf("+%d*x^%d",w->coef,w->exp);}else{if(w->exp!=0)printf("%d*x^%d",w->coef,w->exp);elseprintf("%d",w->coef);}w=w->link;}}}void main(){Polylinklist c=NULL;PolyNode *Lengtha;PolyNode *Lengthb;int a1,b1;printf("Please input a's Length:");scanf("%d",&a1);Lengtha=Creat(a1);printf(" a=");Result(Lengtha);printf("\n");printf("Please input b's Length:");scanf("%d",&b1);Lengthb=Creat(b1);printf(" b=");Result(Lengthb);printf("\n");c=PADD(Lengtha,Lengthb);printf("\n");printf(" c=");Result(c);printf("\n");}。
一元多项式相加(C语言实现)
//一元多项式相加//#define ERROR 0#include<stdio.h>#include<malloc.h>//#include<stdlib.h>typedef struct poly{ float coef;int expn;struct poly *next;}poly,*Linkpoly;void main(){Linkpoly creatpoly();Linkpoly addpoly(Linkpoly La,Linkpoly Lb);void printpoly(Linkpoly L);Linkpoly La,Lb;La=creatpoly();Lb=creatpoly();printf("多项式1:\n");printpoly(La);printf("\n");printf("多项式2:\n");printpoly(Lb);printf("\n");addpoly(La,Lb);printf("相加后的多项式:\n");printpoly(La);}Linkpoly creatpoly()//创建链表,输入多项式{int i,n;Linkpoly p,L;printf("请输入多项式项数:\n");scanf("%d",&n);printf("逆序输入多项式: <系数,指数>\n");L=(Linkpoly)malloc(sizeof(poly));//头结点L->next=NULL;for(i=n;i>0;--i){p=(Linkpoly)malloc(sizeof(poly));scanf("%f,%d",&p->coef,&p->expn);p->next=L->next; L->next=p;}return L;}Linkpoly addpoly(Linkpoly La,Linkpoly Lb)//多项式相加{Linkpoly p,r,q,s,s1,s2;r=La;//r指向p的前驱p=La->next;q=Lb->next;while(p&&q){if(p->expn<q->expn) { r=p;p=p->next;continue;}if(p->expn>q->expn) {s=q->next;q->next=r->next;r->next=q;q=s;r=r->next;continue;}if(p->expn==q->expn&&(p->coef+q->coef)!=0) { p->coef=p->coef+q->coef;r=p;p=p->next;q=q->next;continue;} if(p->expn==q->expn&&(p->coef+q->coef)==0) { s1=p;s2=q;p=r;p->next=p->next->next;p=p->next;q=q->next;continue;free(s1);free(s2);}}if(q){p=r;p->next=q;p=p->next;// free(p);}// return La;}void printpoly(Linkpoly L){Linkpoly p;p=L->next;while(p){printf("<%0.2f,%d> ",p->coef,p->expn);p=p->next;}}。
一元多项式计算器 c语言
typedef struct Linklistomial{
float coef;
int expn;
struct Linklistomial *next;
}Linklistomial,*Linklist;
void Insert(Linklist p,Linklist h){
Linklist qa=pa->next;
Linklist qb=pb->next;
Linklist headc,hc,qc;
hc=(Linklist)malloc(sizeof(struct Linklistomial));//建立头结点
hc->next=NULL;
scanf("%f %d",&p->coef,&p->expn);
Insert(p,head); //调用Insert函数插入结点
}
return head;
}
void DestroyLinklist(Linklist p){ //销毁多项式p
qc->expn=qa->expn;
qa=qa->next;
qb=qb->next;
break;
}
case -1: {
qc->expn=qa->expn;
qa=qa->next;
break; }
case 0: {
qc->coef=qa->coef-qb->coef;
qc->coef=-qb->coef;
qc->expn=qb->expn;
一元多项式(C语言版)
//指数相同
sum=p1->coef+p2->coef;
//sum 保存两项的系数和
if(sum){
//系数和不为 0
p1->coef=sum;
//修改 pa 当前结点 p1 的系数值
为两项系数的和
p3->next=p1;p3=p1;
//将修改后的 pa 当前结点 p1 链在
p3 之后,p3 指向 p1
pre 之间
}
printf("--创建成功--\n");
return p;
}
//一元多项式项数
int pnodelength(polynomial p)
{
polynomial q;
//q
int count=0;
//总共有多少项
q=p->next;
//第一项(首结点)
while(q)
//while(q)
pre=p;
//pre 用于保存 q 的前驱,初值为
头结点
q=p->next;
//q 初值为首结点
while(q&&q->expn<=s->expn){
//通过比较找到第一个大于输入
项指数的项 q
pre=q;
//
q=q->next;
//
}
//
s->next=q;pre->next=s;
//将输入项 s 插入到 q 和其前驱结点
q=q->next;
//指向下一个项
}
if(i!=0) putchar('\n');
//多项式不空,换行
}
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Polylinklist m; m=w;
while(w==NULL)
{ printf("0"); break;
} while(w!=NULL) {
if(w->exp==0) printf("%d",w->coef);
else printf("%d*x^%d",w->coef,w->exp);
数据结构作业
一元多项式相加 C 语言代码
#include<stdio.h> #include<malloc.h> typedef struct node {
int exp,coef; struct node *link; } PolyNode,*Polylinklist; Polylinklist Creat(int n) { Polylinklist p,r=NULL,list=NULL; int coef,exp,i; for(i=1;i<=n;i++) {
Polylinklist PADD(Polylinklist a,Polylinklist b) { Polylinklist c;
Polylinklist r,p=a,q=b; int x; c=(Polylinklist)malloc(sizeof(PolyNode)); r=c; while(p!=NULL&&q!=NULL) if(p->exp==q->exp) {
p=p->link; } while(p!=NULL) { r=ATTACH(p->coef,p->exp,r); p=p->link; } while(q!=NULL) { r=ATTACH(q->coef,q->exp,r); q=q->link; } r->link=NULL; p=c; c=c->link; free(p); return c; }
Result(Lengtha); printf("\n");
printf("Please input b's Length:"); scanf("%d",&b1); Lengthb=Creat(b1); printf(" b="); Result(Lengthb); printf("\n"); c=PADD(Lengtha,Lengthb);
printf("\n"); printf(" c="); Result(c); printf("\n"); }
scanf("%d %d",&coef,&exp); p=(Polylinklist)malloc(sizeof(PolyNode)); p->coef=coef; p->exp=exp; p->link=NULL; if(list==NULL)
list=p; else
r->link=p; r=p; } return(list); } Polylinklist ATTACH(int coef,int exp,Polylinklist r) { Polylinklist w; w=(Polylinklist)malloc(sizeof(PolyNode)); w->exp=exp; w->coef=coef; r->link=w; return w; }
x=p->coef+q->coef; if(x!=0) r=ATTACH(x,q->exp,r); p=p->link; q=q->link; } else if(p->exp<q->exp) {
r=ATTACH(q->coef,q->exp,r); q=q->link; } else {r=ATTACH(p->coef,p->exp,r);
w=w->link; }
} } void main()
{ Polylinklist c=NULL; PolyNode *Lengtha; PolyNode *Lengthb; int a1,b1; printf("Please input a's Length:"); scanf("%d",&a1); Lengtha=Creat(a1); printf(" a=");
w=w->link;
while(w!=NULL) {
if(w->coef>0) {
if(w->exp==0) printf("+%d",w->coef); else printf("+%d*x^%d",w->coef,w->exp); } else { if(w->exp!=0) printf("%d*x^%d",w->coef,w->exp); else printf("%d",w->coef); }