多项式相加C语言程序

合集下载

C语言实现多项式的相加

C语言实现多项式的相加

C语⾔实现多项式的相加本⽂实例为⼤家分享了C语⾔多项式相加的具体代码,供⼤家参考,具体内容如下包含带头节点的链表的初始化,输出:#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>typedef struct Pol{int coe; // 系数int index; // 指数struct Pol *next;}Pol;int main(int argc, char *argv[]){Pol *head1 = NULL; // 第⼀个多项式Pol *head2 = NULL; // 第⼆个多项式Pol *Initiate(Pol *head1); // 声明初始化函数void Output(Pol *head); // 声明输出函数void PolAdd(Pol *head1, Pol *head2); // 声明相加函数int coe, index;char sign;Pol *p;int n = 0;// 初始化第⼀个多项式head1 = Initiate(head1);p = head1;while (1){scanf("%dx%d%c", &coe, &index, &sign);p->next = (Pol *)malloc(sizeof(Pol));p = p->next;p->coe = coe;p->index = index;p->next = NULL;if(sign == '\n')break;}printf("第⼀多项式输⼊完毕。

\n");// 初始化第⼆个多项式head2 = Initiate(head2);p = head2;while (1){scanf("%dx%d%c", &coe, &index, &sign);p->next = (Pol *)malloc(sizeof(Pol));p = p->next;p->coe = coe;p->index = index;p->next = NULL;if (sign == '\n')break;}printf("第⼆多项式输⼊完毕。

多项式加法(C语言实现)

多项式加法(C语言实现)

多项式加法#include <stdio.h>#include <stdlib.h>#define Max_Size 100typedef struct node{float coef;int expn;struct node *next;}PolyNode;int CreCoeStr(float C[]){char flag;int i=0;do{scanf("%f",&C[i++]);scanf("%c",&flag);} while (flag!='#');return(i);}void CreExpStr(int E[]){int i=0;char flag;do{scanf("%d",&E[i++]);scanf("%c",&flag);} while (flag!='#');}void InitPolyList(PolyNode **sq){if((*sq=(PolyNode *)malloc(sizeof(PolyNode)))==NULL) exit(1);(*sq)->next=NULL;}void CreatPolyList(PolyNode **sq,float C[],int E[],int num){int i;PolyNode *s,*r=*sq;for(i=0;i<num;i++){if((s=(PolyNode *)malloc(sizeof(PolyNode)))==NULL) exit(1);s->coef=C[i];s->expn=E[i];r->next=s;r=s;}r->next=NULL;}void InsertSortPoly(PolyNode **sq){PolyNode *p,*q,*r,*u;p=(*sq)->next;(*sq)->next=NULL;while (p){r=*sq;q=(*sq)->next;while (q&&q->expn<=p->expn){r=q;q=q->next;}u=p->next;p->next=r->next;r->next=p;p=u;}}void DispList(PolyNode *sq){PolyNode *p=sq->next;while(p){printf("(%7.2f,%d)",p->coef,p->expn);p=p->next;}printf("\n");}PolyNode *AddPoly(PolyNode *sq_a,PolyNode *sq_b){PolyNode *p_1=sq_a->next,*p_2=sq_b->next,*tc,*p,*s,*pc;InitPolyList(&pc);tc=pc;while (p_1&&p_2){if(p_1->expn<p_2->expn){if((s=(PolyNode *)malloc(sizeof(PolyNode)))==NULL) exit(1);s->coef=p_1->coef;s->expn=p_1->expn;s->next=NULL;tc->next=s;tc=s;p_1=p_1->next;}else if(p_1->expn>p_2->expn){if((s=(PolyNode *)malloc(sizeof(PolyNode)))==NULL) exit(1);s->coef=p_2->coef;s->expn=p_2->expn;s->next=NULL;tc->next=s;tc=s;p_2=p_2->next;}else{if(p_1->coef+p_2->coef){if((s=(PolyNode *)malloc(sizeof(PolyNode)))==NULL) exit(1);s->coef=p_1->coef+p_2->coef;s->expn=p_1->expn;s->next=NULL;tc->next=s;tc=s;}p_1=p_1->next;p_2=p_2->next;}}if(p_1) p=p_1;else p=p_2;while (p){if((s=(PolyNode *)malloc(sizeof(PolyNode)))==NULL) exit(1);s->coef=p->coef;s->expn=p->expn;tc->next=s;tc=s;p=p->next;}tc->next=NULL;return pc;}void main(){PolyNode *sq_1,*sq_2,*sq_3;float C_1[Max_Size],C_2[Max_Size];int E_1[Max_Size],E_2[Max_Size],num_1,num_2;printf("\n\t\t两个多项式相加运算\n");printf("\n");printf("\n");printf("请输入多项式A 的各项系数(以‘#’结束): ");num_1=CreCoeStr(C_1);printf("\n请输入多项式A 的各项幂数(以‘#’结束): ");CreExpStr(E_1);printf("\n请输入多项式B 的各项系数(以‘#’结束): ");num_2=CreCoeStr(C_2);printf("\n请输入多项式B 的各项幂数(以‘#’结束): ");CreExpStr(E_2);InitPolyList(&sq_1);InitPolyList(&sq_2);CreatPolyList(&sq_1,C_1,E_1,num_1);CreatPolyList(&sq_2,C_2,E_2,num_2);printf("\n原多项式A :\n");DispList(sq_1);printf("\n原多项式B :\n");DispList(sq_2);InsertSortPoly(&sq_1);InsertSortPoly(&sq_2);printf("\n排列后的多项式A : \n");DispList(sq_1);printf("\n排列后的多项式B : \n");DispList(sq_2);sq_3=AddPoly(sq_1,sq_2);printf("\n多项式相加结果: \n");DispList(sq_3);printf("\n");}。

多项式相加c语言

多项式相加c语言
Print(&sum);
reset(&p);
reset(&q);
reset(&sum);
return 0;
}
```
在上述代码中,首先定义了一个`Node`结构体,包含多项式的系数和指数,并通过链表的方式存储多项式的每一项。然后,实现了一系列函数,包括多项式的增加、输出、相加和消除。在`main`函数中,调用这些函数来完成多项式的输入、相加和输出。
请注意,这只是一个简单的示例,实际应用中可能需要考虑更多的边界情况和错误处理。
以下是一个简单的 C 语言示例,展示了如何实现多项式相加:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义结构的数据部分
struct poly {it zhishu;float xishu;
};
// 定义一个多项式结构
typedef struct node {
s->date.xishu = s->date.xishu + s->next->date.xishu;
s->next = s->next->next;
} else {
s = s->next;
}
}
d = d->next;
}
}
// 链表的消除函数
void reset(Node *pList) {
Node *p = pList;
while (p->next) {
if (flog == 1)
printf(" + ");
else {

多项式加减法C++源码

多项式加减法C++源码

《程序设计实践》课程实验报告学号:126040594姓名:王乾帅程序名称:多项式加法一:程序源码:#include<iostream>#include<string>using namespace std;int NUM=0;//全局变量,控制结构体下标,大小代表多项式的项数;int mt=0;struct Data //结构体,用来存放单个项的系数和幂,分别为x,y;{int x;int y;};Data Da[1000];bool cmp(Data a,Data b) //为了使用sort对结构体进行排序,自定义cmp函数;{return a.y>b.y;}void run(string &a); //自定义函数,用来对字符串进行处理,提取出字符串中的单个项;int main(){int i,stop,sig,sig2=0;string a,b;cout<<"多项式加减法运算器\n"<<"请输入需要选择的运算(1加法.2减法.)\n";cin>>sig;cout<<"请输入第一个多项式:";//多项式输入cin>>a;cout<<"请输入第二个多项式:";cin>>b;run(a);//处理多项式1,将数据存入结构体中;if(sig==2) mt=1;run(b);//处理多项式2;sort(Da,Da+NUM,cmp);//对结构体按照y(即单个项的幂)的大小排序for(i=0;i<NUM;)//以下为输出部分{stop=i+1;//搜索幂相同的结构体,将x相加,在进行各种判断,进而输出while(Da[i].y==Da[stop].y&&stop<NUM+1){stop++;}if(stop-i-1==0) //只搜索到一项的输出{if(Da[i].y==0){//判断输出是否为第一项,判断输出为正数还是负数,分别进行输出;if(Da[i].x>0&&i!=0&&sig2==1) {cout<<"+"<<Da[i].x;sig2=1;}if(Da[i].x>0&&i!=0&&sig2!=1) {cout<<Da[i].x;sig2=1;}if(Da[i].x>0&&i==0) {cout<<Da[i].x;sig2=1;}if(Da[i].x<0) {cout<<Da[i].x;sig2=1;}}else if(Da[i].y==1){if(Da[i].x>1&&i!=0&&sig2==1) {cout<<"+"<<Da[i].x;sig2=1;cout<<"x";}if(Da[i].x>1&&i!=0&&sig2!=1) {cout<<Da[i].x;sig2=1;cout<<"x";}if(Da[i].x>1&&i==0) {cout<<Da[i].x;sig2=1;cout<<"x";}if(Da[i].x==1&&i!=0&&sig2==1) {cout<<"+";sig2=1;cout<<"x";}if(Da[i].x==1&&i!=0&&sig2!=1) {sig2=1;cout<<"x";}if(Da[i].x==1&&i==0) {sig2=1;cout<<"x";}if(Da[i].x<-1) {cout<<Da[i].x;sig2=1;cout<<"x";}if(Da[i].x==-1) {cout<<"-";sig2=1;cout<<"x";}}else //搜索到多个项的输出{if(Da[i].x>1&&i!=0&&sig2==1) {cout<<"+"<<Da[i].x<<"x^"<<Da[i].y;sig2=1;}if(Da[i].x>1&&i!=0&&sig2!=1) {cout<<Da[i].x<<"x^"<<Da[i].y;sig2=1;}if(Da[i].x>1&&i==0) {cout<<Da[i].x<<"x^"<<Da[i].y;sig2=1;}if(Da[i].x==1&&i!=0&&sig2==1) {cout<<"+"<<"x^"<<Da[i].y;sig2=1;}if(Da[i].x==1&&i!=0&&sig2!=1) {cout<<"x^"<<Da[i].y;sig2=1;}if(Da[i].x==1&&i==0) {cout<<"x^"<<Da[i].y;sig2=1;}if(Da[i].x<-1) {cout<<Da[i].x<<"x^"<<Da[i].y;sig2=1;}if(Da[i].x==-1) {cout<<"-"<<"x^"<<Da[i].y;sig2=1;}}}else{int sum=0,j;for(j=i;j<stop;j++){sum+=Da[j].x;}if(Da[i].y==0){//判断输出是否为第一项,判断输出为正数还是负数,分别进行输出;if(sum>0&&i!=0&&sig2==1) {cout<<"+"<<sum;sig2=1;}if(sum>0&&i!=0&&sig2!=1) {cout<<sum;sig2=1;}if(sum>0&&i==0) {cout<<sum;sig2=1;}if(sum<0) {cout<<sum;sig2=1;}}else if(Da[i].y==1){if(sum>1&&i!=0&&sig2==1) {cout<<"+"<<sum<<"x";sig2=1;}if(sum>1&&i!=0&&sig2!=1) {cout<<sum<<"x";sig2=1;}if(sum>1&&i==0) {cout<<sum<<"x";sig2=1;}if(sum==1&&i!=0&&sig2==1) {cout<<"+"<<"x";sig2=1;}if(sum==1&&i!=0&&sig2!=1) {cout<<"x";sig2=1;}if(sum==1&&i==0) {cout<<"x";sig2=1;}if(sum<-1) {cout<<sum<<"x";sig2=1;}if(sum==-1) {cout<<"-"<<"x";sig2=1;}}else{if(sum>1&&i!=0&&sig2==1) {cout<<"+"<<sum<<"x^"<<Da[i].y;sig2=1;}if(sum>1&&i!=0&&sig2!=1) {cout<<sum<<"x^"<<Da[i].y;sig2=1;}if(sum>1&&i==0) {cout<<sum<<"x^"<<Da[i].y;sig2=1;}if(sum==1&&i!=0&&sig2==1) {cout<<"+"<<"x^"<<Da[i].y;sig2=1;}if(sum==1&&i!=0&&sig2!=1) {cout<<"x^"<<Da[i].y;sig2=1;}if(sum==1&&i==0) {cout<<"x^"<<Da[i].y;sig2=1;}if(sum<-1) {cout<<sum<<"x^"<<Da[i].y;sig2=1;}if(sum==-1) {cout<<"-"<<"x^"<<Da[i].y;sig2=1;}}}i=stop;}if(sig2==0) cout<<"0";cout<<endl;return 0;}void run(string &a) //自定义函数的函数体{int b[100],i,j;int sum1,sum2;for(i=0;i<a.length();i++){if(a[i]=='x') //以X为关键字进行搜索带有X的项{sum1=1;int k=i;int w=0;if(a[i-1]=='-') sum1=-1;if(a[k-1]>='0'&&a[k-1]<='9'){sum1=0;while(a[k-1]>='0'&&a[k-1]<='9')//将字符串转化为数字{w++;b[w]=a[k-1]-'0';k--;}for(j=w;j>=1;j--){sum1=sum1*10+b[j];}if(a[k-1]=='-') sum1=0-sum1;}sum2=1;if(a[i+1]=='^'){sum2=0;k=i;w=0;while(a[k+2]>='0'&&a[k+2]<='9'){w++;b[w]=a[k+2]-'0';k++;}for(j=1;j<=w;j++){sum2=sum2*10+b[j];}}if(mt==1){Da[NUM].x=-sum1;Da[NUM].y=sum2;}else{Da[NUM].x=sum1;Da[NUM].y=sum2;}NUM++;}}for(i=0;i<a.length();) //搜索不含X的单个数字项{int sum3=0,stop,k;if(a[i]>='0'&&a[i]<='9'){k=i;while(a[k]>='0'&&a[k]<='9'&&k<=a.length()){k++;}if(k==a.length()&&i==0) //输入仅有一个数字{stop=k;for(j=i;j<stop;j++){sum3=sum3*10+(a[j]-'0');}i=stop+1;}if((a[k]=='+'||a[k]=='-')&&i==0) //输入有一个数字并且在在字符串开头{stop=k;for(j=i;j<stop;j++){sum3=sum3*10+(a[j]-'0');}i=stop+1;}if((a[i-1]=='+'||a[i-1]=='-')&&k==a.length()) //输入有一个数字并且在在字符串结尾{stop=k;for(j=i;j<stop;j++){sum3=sum3*10+(a[j]-'0');}if(a[i-1]=='-') sum3=-sum3;i=stop+1;}if((a[k]=='+'||a[k]=='-')&&(a[i-1]=='+'||a[i-1]=='-')) //输入有一个数字并且在在字符串中央{stop=k;for(j=i;j<stop;j++){sum3=sum3*10+(a[j]-'0');}if(a[i-1]=='-') sum3=-sum3;i=stop+1;}else i++;}else i++;if(sum3!=0){if(mt==1){Da[NUM].x=-sum3;Da[NUM].y=0;}else{Da[NUM].x=sum3;Da[NUM].y=0;}NUM++;}}}二:程序测试1.2.3. 4. 5. 6. 7.8.三:编程过程中注意事项:输入:1.注意紧含数字时的处理(数字在开头,中间,结尾)输出:1.注意系数为1,-1的情况2.注意次数为1,为0的情况3.注意输出开头不能有+4.注意若为0,则不输出,除非仅仅输出出一个0.。

多项式运算Cpp程序

多项式运算Cpp程序

//===用带表头结点的单链表实现多项式的加-减-乘-除运算的C++程序====//======================一元多项式的运算:OOP======================//1.降幂建立一个一元多项式并输出----------------------------------//2.求两个多项式的和并输出(原多项式不变)--------------------------//3.利用求和函数,求两个多项式的差并输出(原多项式不变)------------//4.利用求和与求差函数,求两个多项式的积并输出(原多项式不变)------//5.利用求和、差、积函数,求两多项式的商并输出(原多项式不变)------#include <conio.h> //getch#include <cstdlib> //exit#include <cmath> //fabs#include<iostream> //cout,cinusing namespace std;typedef struct{ //元素类型定义float coef; //系数(Coefficient)int expn; //指数(exponential )}EType; //元素类型名(ElementType)typedef struct LNode{ //链表结点类型定义EType d; //结点数据域struct LNode *next; //结点指针域}LNode,*LinkNP; //结点及结点指针类型名typedef struct{ //表头结点类型LinkNP head,tail; //链表头尾指针int len; //链表长度}LHNode; //链表头结点类型名typedef LHNode* polyn; //多项式表头结点指针类型名//================多项式类的定义================//class polynomail{private:polyn p; //数据成员public:polynomail() //==构造函数:初始化表头结点及其指针=={p=new LHNode;if(!p)errExit("New LHNode error!"); //检测指针是否为空p->head=NULL,p->tail=NULL,p->len=0;}void errExit(char *str) //遇错自动退出程序{cout<<str; exit(0);}int CmpElemExpn(EType a,EType b) //==比较两项的幂次=={if(a.expn>b.expn)return 1;else if(a.expn<b.expn)return -1;else return 0;}int MakeNode(LinkNP &L,EType e) //==生成值为e的结点,指针存入L=={L=new LNode; //向内存申请空间if(!L)errExit("new LinkNP error !"); //检测指针是否为空L->d=e; //给结点赋值L->next=NULL;return 1;}void AppNode(LinkNP L) //==结点链入尾部=={if(p->head==NULL) //若表为空p->head=p->tail=L;else{p->tail->next=L; //L连到尾部p->tail=L; //修改头结点的尾指针}p->len+=1; //表长增1}void InsFirst(LinkNP s) //==s插入链首结点前=={if(!p->head)AppNode(s);else s->next=p->head,p->head=s,p->len+=1;}void InsSortNode(LinkNP s) //==s所指插入有序链p中=={LinkNP h=p->head,t=p->tail;if(!h||s->d.expn<t->d.expn) //若表空或s比尾大...AppNode(s); //...则直接连入尾部else if(s->d.expn>h->d.expn) //s比首元素指数大InsFirst(s); //作为首元素插入else{while(1){ //查找插入位置if(s->d.expn>h->next->d.expn)break; //h停在待插点前h=h->next;}s->next=h->next; //插入h之后h->next=s;p->len+=1;}}int LocateElem(EType e,LinkNP& q) //==p中找e且指针入q== {LinkNP h=p->head;while(h&&CmpElemExpn(h->d,e)) //查找是否有eh=h->next;q=h; //指针存入qif(q)return 1; //若有else return 0; //若无}void CreatPoly(char *str) //==逐个降幂有序创建== {int i=1;LinkNP s,q;EType e;printf("\nCreat polyn %s:\n",str);while(1){printf("\tcoef%d,expn%d=",i,i);scanf("%f,%d",&e.coef,&e.expn); //输入系数及幂次if(e.coef==0&&e.expn==0)break; //若全0则结束if(fabs(e.coef)<1.0e-30||(e.expn<0))//若系数或指数非法{cout<<"Error!\n";continue;} //重新输入if(!LocateElem(e,q)){ //若该项不存在...if(MakeNode(s,e)) //若结点创建成功InsSortNode(s); //将s所指有序插入}else{cout<<"Error!\n";continue;} //...否则重新输入i++; //记录项数}}void ItemPrint(float a,int n) //输出某项ax^n{if(n==0)cout<<a; //指数为0else if(n==1){ //指数为1if(a==1)cout<<"x";else if(a==-1)cout<<"-x";else cout<<a<<"x";}else{ //指数大于1if(a==1)cout<<"x^"<<n;else if(a==-1)cout<<"-x^"<<n;else cout<<a<<"x^"<<n;}}void PrintPoly(char *str) //==输出多项式=={LinkNP L;L=p->head;if(L){ //若非空则输出首元结点printf("\n多项式%s中有%d项: ",str,p->len);ItemPrint(L->d.coef,L->d.expn);L=L->next;}else printf("\n多项式%s为空!",str);while(L){ //输出之后所有结点if(L->d.coef>0)cout<<"+";ItemPrint(L->d.coef,L->d.expn);L=L->next;}}//==============两多项式类相加==============//void AddPoly(polynomail pa,polynomail pb) //求两个多项式的和{LinkNP ha,hb,ht;EType ea,eb,ec;ha=pa.p->head,hb=pb.p->head;while(ha&&hb){ //a,b都非空时循环ea=ha->d,eb=hb->d;switch(CmpElemExpn(ea,eb)){ //比较大小case 1: //若ea大则将ea加入ecif(MakeNode(ht,ea))AppNode(ht);ha=ha->next; //a表指针后移break;case 0: //若ea与ec相等则将和加入ec ec.coef=ea.coef+eb.coef;ec.expn=ea.expn;if(ec.coef) //若系数不为0if(MakeNode(ht,ec)) //若开辟结点成功AppNode(ht); //将结点指针L链入cha=ha->next,hb=hb->next; //a,b两表指针同时后移break;case -1: //若eb大则将eb加入ec if(MakeNode(ht,eb))AppNode(ht);hb=hb->next; //b表指针后移break;}}while(ha){ //若ea有剩余,则将剩余加入ea=ha->d;if(MakeNode(ht,ea))AppNode(ht);ha=ha->next;}while(hb){ //若eb有剩余,则将剩余加入eb=hb->d;if(MakeNode(ht,eb))AppNode(ht);hb=hb->next;}}void CopyPoly(polynomail s) //拷贝多项式{LinkNP np=s.p->head,q;while(np){if(MakeNode(q,np->d))AppNode(q);np=np->next;}}void RevSignPoly(polynomail &pn) //系数反号{LinkNP p=pn.p->head;while(p){p->d.coef=-p->d.coef; //反号p=p->next;}}//==============两多项式类相减==============//void SubPoly(polynomail a,polynomail b) //将b反号与a相加{polynomail pn;pn.CopyPoly(b);RevSignPoly(pn);AddPoly(a,pn);}//==============两多项式类相乘==============//void MulPoly(polynomail a,polynomail b,polynomail &c){ //a与b相乘存入c LinkNP ha=a.p->head,hb=b.p->head;polynomail pa,pb,pc,pt;if(!ha||!hb){c=pt;return;}; //空多项式即0while(ha){ //取多项式的某项pb.CopyPoly(b);hb=pb.p->head;while(hb){ //与多项式b相乘hb->d.coef*=ha->d.coef;hb->d.expn+=ha->d.expn;hb=hb->next;}pt.CopyPoly(c); //c复制到ptc.Destroy(); //c清零c.AddPoly(pt,pb); //pt与pb之和存入cpt.Destroy(); //pt清零pb.Destroy();ha=ha->next;}}//==============两多项式类相除==============//void DivPoly(polynomail a,polynomail b,polynomail &q,polynomail &r) { //a与b相除商存入q余存入r polynomail pa,pb,pt;LinkNP ep,h,ha=a.p->head,hb=b.p->head;if(!ha){q=r=pt;return;}if(!hb)errExit("\n多项式b不能为零"); //除式不能为0pa.CopyPoly(a);while(ha&&hb&&ha->d.expn>=hb->d.expn){ //a比b首项指数大ep=new LNode;ep->next=NULL;ep->d.coef=ha->d.coef/hb->d.coef;ep->d.expn=ha->d.expn-hb->d.expn;q.AppNode(ep); //商式因子连入qpb.CopyPoly(b);h=pb.p->head;while(h){ //商式与除式相乘h->d.coef*=ep->d.coef;h->d.expn+=ep->d.expn;h=h->next;}pt.SubPoly(pa,pb); //再与被除式相减pa.Destroy();pb.Destroy();pa.CopyPoly(pt);pt.Destroy();ha=pa.p->head;}r.CopyPoly(pa);}void Destroy() //释放内存空间== {LinkNP px=p->head,pt;while(px){pt=px;px=px->next;delete pt; //释放内存空间}p->head=NULL;p->tail=NULL;p->len=0;}};//================多项式类的测试================//void main(){polynomail a,b,c,r; //创建对象a,b,c空多项式a.CreatPoly("a"); //有序创建多项式a并输出a.PrintPoly("a");b.CreatPoly("b"); //有序创建多项式b并输出b.PrintPoly("b");c.AddPoly(a,b); //a与b相加其和存入c并输出c.PrintPoly("和式");c.Destroy(); //清除cc.SubPoly(a,b); //a与b相减其差存入c并输出c.PrintPoly("差式");c.Destroy();c.MulPoly(a,b,c); //a与b相乘其积存入c并输出c.PrintPoly("积式");c.Destroy();c.DivPoly(a,b,c,r); //a与b相除其商存入c余存入rc.PrintPoly("商式");r.PrintPoly("余式");a.Destroy(); //销毁对象所有对象b.Destroy();c.Destroy();r.Destroy();getch(); //按任意键结束运行}。

多项式相加c语言程序

多项式相加c语言程序

#include "stdafx.h"//备注:我用的编译平台所提供的头文件不是“stdio.h"#include"malloc.h"#include<cstdlib> //此头文件用来解决执行可以执行文件exe后直接退出的问题,希望可以帮到大家了解一个新函数和头文件typedef struct List{ //定义一个动态链表float coef;int expn;struct List *next;}*list;list initlist()//初始化,生成新结点{list l;l=(list)malloc(sizeof(List));if (!l) printf("error");l->next=NULL;return l;}void insertlist (list l,float *coe,int *exp)//每次scanf后插入结点,从链尾插入,main函数中定义一个链表尾指针{list s;s=(list)malloc(sizeof(List));if(!s)printf("error");s->coef=*coe;s->expn=*exp;l->next =s;s->next =NULL;}void putout(list l)//输出链表{list p;p=l->next;while(p->next){ if(p->expn==0)printf("1+");else if(p->coef==1&&p->expn==1){printf("x+");}elseprintf("%.2fx^%d+",p->coef,p->expn);p=p->next ;} if(p->expn==0)printf("1\n");else if(p->coef==1&&p->expn==1){printf("x\n");}elseprintf("%.2fx^%d\n",p->coef,p->expn);}int cmp(int f,int g) //比较函数,用来判定当前两多项式指数问题{if (f<g)return (-1);else if(f==g)return 0;elsereturn (1);}void addlist(list n,list m){//多项式相加list hn,hm,pn,pm;float sum;pn=n->next ;pm=m->next ;hn=n;hm=m;//定义一个当前节点的指针和一个头指针while(hn->next&&hm->next){switch (cmp(pn->expn,pm->expn))//比较两指数{case -1:hn=pn;pn=pn->next;break;case 0:sum=pn->coef +pm->coef ;if(sum!=0){pn->coef =sum;pm=pm->next ;free(hm->next);hm->next =pm;hn=hn->next ;pn=pn->next ;}//ifelse {hn->next =pn->next ;free(pn);pn=hn->next ;hm->next =pm->next ;free(pm);pm=hm->next ;}break;//elsecase 1:hm->next =pm->next ;hn->next=pm;pm->next =pn;hn=pm;pn=pn->next ;pm=hm->next ;break;}//switch}//whileif(hm->next ) //参考书本43页算法的思想,将剩余结点插入当前链表中{hn->next=pm;free(hm);}}//addlistvoid chongpaixu(list l)//将输入的多项式按升序排列,并将指数相同的合并(还不能执行){ list s;list q;list k;list w;float sum;k=initlist();q=l->next ;s=l;while(q->next){for(w=l ;q->next !=NULL;q=q->next ){for (s=s->next ;s->next!=NULL;s=s->next ){switch(cmp(s->expn,q->expn)){case -1:w=w->next ;break;case 1:k->coef=q->coef;q->coef=s->coef;s->coef=k->coef;k->expn=q->expn ;q->expn =s->expn ;s->expn =k->expn ;free(k);w=w->next ;break;case 0:sum=s->coef+q->coef;if(sum){s->coef=sum;s->next=q;free(q);q=s->next ;}//ifelse {w->next=q->next;free(s);free(q);s=w->next ;q=s->next ;}//elsebreak;}//switch}}}//while}void putmessage(void)//用来表明备注{printf("备注:该算法经过上课时老师给我们提出的问题进行了修改,不过关于排序的算法还不能完善\n");printf("因此,请输入时请将多项式按照升序输入,将相同指数合并!\n");printf("\n");printf("\n");printf("\n");printf("\n");printf("\n");printf("\n");}void main(){list l,s,hl,hs; int i,d,exp;float *q;int *w;float coe;putmessage();q=&coe;w=&exp;l=initlist();s=initlist();hl=l;hs=s;printf("请输入l的项数\n");scanf("%d",&d);if(d>0){for (i=0;i<d;i++){printf("请输入第%d项的系数\n",i+1);scanf("%f",&coe);printf("请输入第%d项的指数\n",i+1);scanf("%d",&exp);if(coe){insertlist(hl,q,w);hl=hl->next ;}}/*chongpaixu(l);*/if(l->next ){printf("你输入的多项式是f(x)=");putout(l);}else printf("你输入的多项式是f(x)=0\n"); }else printf("你输入的多项式l不存在\n");printf("请输入s的项数\n");scanf("%d",&d);if(d>0){for (i=0;i<d;i++){printf("请输入第%d项的系数\n",i+1);scanf("%f",&coe);printf("请输入第%d项的r指数\n",i+1);scanf("%d",&exp);if(coe){insertlist(hs,q,w);hs=hs->next ;}}if(s->next){printf("你输入的多项式是f(x)=");putout(s);}else printf("你输入的多项式是f(x)=0\n");}else printf("你输入的多项式不存在\n");hl=l;hs=s;if(hl->next &&hs->next ){addlist(l,s);printf("合并后的多项式f(x)=");if(l->next){putout(l);}else printf("0\n");}else printf("no add\n");system ( "pause" );//不加此语句,exe文件在执行完毕直接跳出}。

两多项式求和(C语言版)

两多项式求和(C语言版)

两多项式求和(C语言版)#include "stdlib.h"#include "stdio.h"# define OVERFLOW -2typedef struct term{ float coef; //多项式系数int expn; //多项式指数struct term *next;} node;node *Create(int n)//创建一个n个结点的链表,并给每//个节点数据域赋值{ node *head, *p, *q;//int i;head=(node *)malloc(sizeof(node));if(!head) { printf("分配内存失败!");exit(OVERFLOW);}for(i=0;i<n;i++)< p="">{ p=(node *)malloc(sizeof(node));if(!p){ printf("分配内存失败!");exit(OVERFLOW);}printf("请输入第%d项系数:\n",i+1);//从键盘读取数据scanf("%f",&p->coef);printf("请输入第%d项指数:\n",i+1);scanf("%d",&p->expn);//从键盘读取数据if(i==0)//如果是第一个节点,则指针q、head同时指向该第一个实节点head->next=q=p;else //否则一个节点一个节点往后接{ q->next=p;p->next=NULL;q=p;}}return(head);//返回所建链表头指针}int cmp (node *m,node *n )//比较两个指数大小,并返回-1或0或1 { if(m->expnexpn)return -1;else if(m->expn==n->expn)return 0;elsereturn 1;}float Sum(node *m,node *n)//求多项式两个系数之和{ return m->coef+n->coef;}node *Add (node *a,node *b )//两个多项式相加{ node *ha,*hb,*pa,*pb,*tmpb;int t;ha=a;hb=b;while(ha->next!=NULL) {pa=ha->next;pb=hb->next;tmpb=hb;while(tmpb->next!=NULL){t=cmp(pa,pb);if(t==0){ (pa->coef)+=(pb->coef);pb=pb->next;tmpb->next=pb;}if(t!=0){tmpb=pb;pb=pb->next;}}ha=ha->next;}if(hb->next!=NULL)//如果多项式b还有某些项未执行相加操作,//则将其接到结果多项式a后面ha->next=hb->next;return a;//返回结果多项式}int main(){ node *x,*y,*p,*hp;int m=0,n=0;char c1,c2;printf("请输入第一个多项式的项数!\n");scanf("%d",&m);printf("请输入第一个多项式\n\n");x=Create(m);printf("请输入第二个多项式的项数!\n");scanf("%d",&n);printf("请输入第二个多项式\n\n");y=Create(n);p=Add(x,y );printf("两个多项式相加成功,其结果如下:\n"); while(p->next!=NULL)//输出相加所得的多项式{ printf("%5.1f",p->next->coef);if(p->next->expn!=0)printf(" * X(%d)",p->next->expn);p=p->next;if(p->next!=NULL)printf(" + ");}c1=getchar();c2=getchar();if(c2=='\n')printf("\n");return 0;}</n;i++)<>。

C语言实现多项式相加算法

C语言实现多项式相加算法
if(q->n==0&&q->xi<0)
printf("%0.2f",q->xi);
if(q->n==1&&q->xi==1)
printf("x");
if(q->n==1&&q->xi!=1&&q->xi>0)
printf("%0.2fx",q->xi);
if(p->n!=0&&p->n!=1&&p->xi==1)
printf("+x^%d",p->n); p=p->nexFra bibliotek; }
printf("\n");
}
main()
{
int i,j;
term *head1,*head2,*head3;
printf("请输入第一个多项式的项数:\n");
}
}
while(p!=NULL)
{
s=(term *)malloc(sizeof(term));
s->n=p->n;
s->xi=p->xi;
r->next=s;
r=s;
p=p->next;
}
while(q!=NULL)
q=head->next;
if(q->xi==0)
printf(" ");
if(q->n==0&&q->xi>0&&q->xi!=1)

多项式求和,源代码

多项式求和,源代码
//多项式的表示和相加
#include<stdio.h>
#include<malloc.h>
# define NULL 0
struct polyn
{
float xishu;
int zhishu;
struct polyn *next;
};
typedef struct polyn polyn;
/*函数功能:建立一个表示一元多项式的有序链表P
else
printf("%.2fX^%d ",p->xishu,p->zhishu);
p=p->next;
while (p!=NULL)
{
if (p->zhishu==0)
printf("+ %.2f ",p->xishu);
else if(p->zhishu==1&&p->xishu!=1)
printf("+ %.2fX ",p->xishu);
{
pb->next=p->next;
p->next=pb;
return pa;
}
else
p=p->next;
}
return NULL;
}
/*函数功能:将多项式p,q相加
函数参数:p,q的头结点指针head1,head2(保证head1的最高指数>head2的最高指数)
函数返回值:相加所得的多项式链表头结点指针
print(head1);
printf("\n请输入多项式q(x)的项数:\n");

编写程序实现多项式求和

编写程序实现多项式求和
}
int sort(Poly node[],int n)//排序函数,合并同指数项去除0系数项
{
int i,j,k,t;
Poly nodet[1];
for(i=1;i<=n-1;i++)//排序
for(j=i+1;j<=n;j++)
{
if((node[i].exp)>(node[j].exp))
{
nodet[0]=node[i];
qb->next=qa;
pa->next=qb;
pa=qb;
qb=pb->next;
}
else
{
sum=qa->coef+qb->coef;
if(sum==0)
{
pa->next=qa->next;
delete qa;
qa=pa->next;
pb->next=qb->next;
delete qb;
qb=pb->next;
cout<<"请输入第一个多项式的系数:"<<endl;
for(i=0;i<=n; i++)
cin>>a[i];
cout<<"请输入第二个多项式的最大指数:";
cin>>n;
cout<<"请输入第二个多项式的系数:"<<endl;
for(i=0;i<=n; i++)
cin>>b[i];
cout<<"多项式之和为:";

多项式的加法和乘法计算c语言代码

多项式的加法和乘法计算c语言代码

多项式的加法和乘法计算C语言代码多项式是代数学中常见的一种数学表达式形式,通常表示为:P(x) = a0 + a1*x + a2*x^2 + a3*x^3 + ... + an*x^n其中a0, a1, a2,...,an是多项式P(x)的系数,x是变量,n是多项式的次数。

在实际的编程开发中,我们经常需要实现多项式的加法和乘法计算。

下面我们将介绍如何使用C语言来实现多项式的加法和乘法计算。

一、多项式的表示与存储在C语言中,可以使用结构体来表示多项式,并使用数组来存储多项式的系数。

假设我们要表示一个最高次数为n的多项式,可以定义结构体如下:```ctypedef struct {int coeff[MAX_SIZE]; // 存储多项式系数的数组int degree; // 多项式的最高次数} Polynomial;其中MAX_SIZE是一个常数,用来表示多项式的最大阶数。

在实际使用中,可以根据需要进行调整。

二、多项式的加法多项式的加法实质上是将两个多项式的对应系数相加,并将结果存储在一个新的多项式中。

下面是多项式加法的C语言代码实现:```cPolynomial addPolynomial(Polynomial poly1, Polynomial poly2) {Polynomial result;int i;// 确定结果多项式的最高次数result.degree = (poly1.degree > poly2.degree) ?poly1.degree : poly2.degree;// 将对应系数相加for (i = 0; i <= result.degree; i++) {result.coeff[i] = poly1.coeff[i] + poly2.coeff[i];return result;}```以上代码通过遍历多项式的系数数组,将对应位置的系数相加,并将结果存储在result中。

数据结构多项式运算的程序(加减法和乘法)C语言版

数据结构多项式运算的程序(加减法和乘法)C语言版

数据结构多项式运算的程序(加减法和乘法)C语言版#include#includetypedef struct node{//定义节点类型float coef;int expn;struct node * next;}PLOY;void start()//用户选择界面{printf("************************************\n");printf(" 两个一元多项式的相加/相减,相乘:\n");printf("************************************\n");printf("请选择操作:\n");printf("0.退出\n");printf("1.两个一元多项式相加\n");printf("2.两个一元多项式相乘\n");printf("3.两个一元多项式相减\n");}void insert(PLOY *head,PLOY *inpt)//查找位置插入新链节程序{PLOY *pre,*now;int signal=0;pre=head;//pre定义为现在的前一个链节if(pre->next==NULL) {pre->next=inpt;}else {now=pre->next;while(signal==0){if(inpt->expnexpn)//当新链节小于现在的连接时向后移一个链节{if(now->next==NULL){now->next=inpt;signal=1;}else{pre=now;now=pre->next;}}elseif(inpt->expn>now->expn)//如果发现比现在的链节大了就插入到这个连接的前面{inpt->next=now;pre->next=inpt;signal=1;}else{now->coef=now->coef+inpt->coef;signal=1;free(inpt);//与当前链节相等指数if(now->coef==0){pre->next=now->next;free(now);}}}}}PLOY *creat(char ch)//输入多项式{PLOY *head,*inpt;float x;int y;head=(PLOY *)malloc(sizeof(PLOY));//创建链表头head->next=NULL;printf("请输入一元多项式%c:(格式是:系数指数;以0 0 结束!)\n",ch);scanf("%f %d",&x,&y);while(x!=0){inpt=(PLOY *)malloc(sizeof(PLOY));//创建新链节inpt->coef=x;inpt->expn=y;inpt->next=NULL;insert(head,inpt);//不然就查找位置并且插入新链节printf("请输入一元多项式%c的下一项:(以0 0 结束!)\n",ch);scanf("%f %d",&x,&y);}return head;}PLOY *addPLOY(PLOY *head,PLOY *pre)//多项式相加{PLOY *inpt;int flag=0;while(flag==0){if(pre->next==NULL)flag=1;//当现在指向空时跳出循环else{pre=pre->next;inpt=(PLOY *)malloc(sizeof(PLOY));//创建新链节inpt->coef=pre->coef;inpt->expn=pre->expn;inpt->next=NULL;insert(head,inpt);}//否则把当前"g(x)"的链节插入到"y(x)"中}return head;}PLOY *minusPLOY(PLOY *head,PLOY *pre)//多项式相加{PLOY *inpt;int flag=0;while(flag==0){if(pre->next==NULL)flag=1;//当现在指向空时跳出循环else{pre=pre->next;inpt=(PLOY *)malloc(sizeof(PLOY));//创建新链节inpt->coef=0-pre->coef;inpt->expn=pre->expn;inpt->next=NULL;insert(head,inpt);}//否则把当前"g(x)"的链节插入到"y(x)"中}return head;}PLOY *byPLOY(PLOY *head1,PLOY *head2)//多项式相乘{PLOY *inpt,*res,*pre;int flag=0;res=(PLOY *)malloc(sizeof(PLOY));//创建链表头res->next=NULL;head1=head1->next;pre=head2;while(flag==0){if(pre->next==NULL){pre=head2;//当现在指向空时跳出循环head1=head1->next;continue;}if(head1==NULL){flag=1;//当现在指向空时跳出循环continue;}pre=pre->next;inpt=(PLOY *)malloc(sizeof(PLOY));//创建新链节inpt->coef=pre->coef*head1->coef;inpt->expn=pre->expn+head1->expn;inpt->next=NULL;insert(res,inpt);//把当前"g(x)"的链节插入到"y(x)"中}return res;}void print(PLOY *fun)//输出多项式{PLOY *printing;int flag=0;printing=fun->next;//正在被打印的链节if(fun->next==NULL)//如果函数为空打印0{printf("0\n");return;}while(flag==0){if(printing->coef>0&&fun->next!=printing) printf("+");//为正数时打印"+"号if(printing->coef==1);//如果为"1"就不用打印系数了else if(printing->coef==-1)printf("-");//如果为"-1"就打印"-"号就行了elseprintf("%f",printing->coef);//其余情况都得打印if(printing->expn!=0) printf("x^%d",printing->expn);//如果指数为"0"不打印指数项else if((printing->coef==1)||(printing->coef==-1))printf("1");if(printing->next==NULL)flag=1;//如果现在的链节没有下一个就结束elseprinting=printing->next;}printf("\n");}void main(){PLOY *f,*g;int sign=-1;//设置标志start();while(sign!=0){scanf("%d",&sign);switch(sign){case 0:break;//退出case 1:printf("你选择的操作是多项式相加:\n"); f=creat('f');//输入多项式f(x)printf("f(x)=");print(f);g=creat('g');//输入多项式g(x)printf("g(x)=");print(g);printf("F(x)=f(x)+g(x)=");f=addPLOY(f,g);//两个多项式相加print(f);sign=-1;//复位标志start();//回复用户选择界面break;}case 2:{printf("你选择的操作是多项式相乘:\n"); f=creat('f');//输入多项式f(x)printf("f(x)=");print(f);g=creat('g');//输入多项式g(x)printf("g(x)=");print(g);printf("F(x)=f(x)*g(x)=");f=byPLOY(f,g);//两个多项式相加print(f);sign=-1;//复位标志start();//回复用户选择界面break;case 3:{printf("你选择的操作是多项式相减:\n");f=creat('f');//输入多项式f(x)printf("f(x)=");print(f);g=creat('g');//输入多项式g(x)printf("g(x)=");print(g);printf("F(x)=f(x)-g(x)=");f=byPLOY(f,g);//两个多项式相加print(f);sign=-1;//复位标志start();//回复用户选择界面break;}default:{printf("输入有误!请重新选择操作!\n");//选择错误,返回选择界面start();break;}}}}。

一元多项式相加C语言代码

一元多项式相加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语言多项式求和

c语言多项式求和

c语言多项式求和本篇文章将介绍C语言中如何实现多项式求和。

1、定义结构体在C语言中,我们通常用结构体来表示多项式。

定义一条多项式时,我们需要定义它的次数,系数和指数。

下面是一个多项式结构体的定义:```#define MAX_N 100struct polynomial {int degree; // 多项式次数double coef[MAX_N]; // 多项式系数,最高项为系数数组的第0项int expon[MAX_N]; // 多项式指数,最高项为指数数组的第0项};```2、输入多项式我们可以定义一个函数来读取多项式的系数和指数,然后将它们保存到结构体中。

```void read_poly(struct polynomial *poly) {printf("Enter the degree of the polynomial: ");scanf("%d", &poly->degree);printf("Enter the coefficients:\n");for (int i = 0; i <= poly->degree; ++i) {scanf("%lf", &poly->coef[i]);}printf("Enter the exponents:\n");for (int i = 0; i <= poly->degree; ++i) {scanf("%d", &poly->expon[i]);}}```这个函数首先要求用户输入多项式的次数,然后依次读取多项式的系数和指数。

我们也可以定义一个函数来输出多项式。

这个函数将多项式的系数和指数格式化输出到标准输出流中。

这个函数首先以标准的方式输出第1项,然后对于每一项,它都以类似“+ax^b”的方式输出。

多项式相加(含运行结果截图)

多项式相加(含运行结果截图)
while (r){
printf("指数:%d系数:%f\n",r->expn,r->coef);
r=r->next;
}
}
main()
{
void creatpolyn(struct linklist *p,int n); //输入n项的系数,建立一个链表
struct linklist * addpolyn(struct linklist*p,struct linklist*q); //两个多项式相加,返回结果指针
la->coef=s; //此项不为0需要修改la中当前节点的系数值
la=la->next;
p=p->next;
lb=lb->next;
q=q->next;
}
else {
p->next=la->next;
r=la;
la=la->next;
free(r);
lb=lb->next;
q=q->next;
}
scanf("%d",&n);
creatpolyn(p,n);
printf("请输入第二个多项式的项数:\n");
scanf("%d",&n);
creatpolyn(q,n);
r=addpolyn(p,q);
print(r);
}
四.算法分析
多项式由系数和指数组成,建立两个链表分别存储系数和指数。为了方便实验过程,实验在输入的时候按照指数的由低到高输入,避免了排序。在相加过程中,将两个链表从头到尾进行比较,直到比到一个链表没有元素为止,将其中指数相同的系数进行加减运算。最后打印出多项式。

多项式相加的函数(数据结构_C语言版)

多项式相加的函数(数据结构_C语言版)

多项式相加的函数(数据结构_C语言版)一、编写一个程序用单链表存储多项式,并实现两个多项式相加的函数。

【源程序】#include#include#define MAX 20typedef struct{ float coef;int exp;}PolyArray[MAX];typedef struct pnode{ float coef;//系数int exp;//指数struct pnode *next;}PolyNode;void DispPoly(PolyNode *L)//输出多项式{ PolyNode *p=L->next;while(p!=NULL){ printf("%gx^%d",p->coef,p->exp);p=p->next;}printf("\n");}void CreateListR(PolyNode *&L,PolyArray a,int n)//尾插法建立单链表{ PolyNode *s,*r;int i;L=(PolyNode *)malloc(sizeof(PolyNode)); //创建头结点L->next=NULL;r=L; //r始终指向终端结点,开始时指向头结点for (i=0;i<n;i++)< p="">{s=(PolyNode *)malloc(sizeof(PolyNode));//创建新结点s->coef=a[i].coef;s->exp=a[i].exp;r->next=s; //将*s插入*r之后r=s;}r->next=NULL;}void Sort(PolyNode *&head)//按exp域递减排序{ PolyNode *p=head->next,*q,*r;if(p!=NULL){ r=p->next;p->next=NULL;p=r;while(p!=NULL){ r=p->next;q=head;while(q->next!=NULL&&q->next->exp>p->exp)q=q->next;p->next=q->next;q->next=p;p=r;}}}void Add(PolyNode *ha,PolyNode *hb,PolyNode *&hc)//求两个有序集合的并{PolyNode *pa=ha->next,*pb=hb->next,*s,*tc;float c;hc=(PolyNode *)malloc(sizeof(PolyNode));tc=hc;while(pa!=NULL&&pb!=NULL){ if(pa->exp>pb->exp){ s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点s->exp=pa->exp;s->coef=pa->coef;tc->next=s;tc=s;pa=pa->next;}else if(pa->expexp){ s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点s->exp=pb->exp;s->coef=pb->coef;tc->next=s;tc=s;pb=pb->next;}else{ c=pa->coef+pb->coef;if(c!=0)//系数之和不为0事创建新结点{ s=(PolyNode *)malloc(sizeof(PolyNode));s->exp=pa->exp;s->coef=c;tc->next=s;tc=s;}pa=pa->next;pb=pb->next;}}if(pb!=NULL)pa=pb;//复制余下的结点while(pa!=NULL){ s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点s->exp=pa->exp;s->coef=pa->coef;tc->next=s;tc=s;pa=pa->next;}tc->next=NULL;}void main(){PolyNode *ha,*hb,*hc;PolyArray a={{1.5,0},{2.5,1},{3.3,3},{-2.5,5}};PolyArray b={{-1.5,0},{2.5,1},{3.7,3},{-2.5,5},{5.6,7}};CreateListR(ha,a,4);CreateListR(hb,b,5);printf("原多项式A为:");DispPoly(ha);printf("原多项式B为:");DispPoly(hb);Sort(ha);Sort(hb);printf("有序多项式A:");DispPoly(ha);printf("有序多项式B:");DispPoly(hb);Add(ha,ha,hc);printf("多项式相加结果:");DispPoly(hc); printf("\n");}</n;i++)<>。

C语言 多项式的加减

C语言 多项式的加减

多项式的加减详细设计#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct term{ //多项式结点的定义float coef; //系数int exp; //指数struct term *link; //链接指针}*Poly;void Input(Poly &pl) //输入多项式{term *nterm,*p,*pre;float c;int e;pl=new term; //pl为头指针pl->link=NULL;printf("***当输入指数<0时结束,且不进入运算***\n");while(1){printf("请输入系数和指数\n");scanf("%f%d",&c,&e);if(e<0)break; //循环结束条件nterm=new term; //开辟新结点if(!nterm) //验证是否开辟到{printf("error");exit(1);}nterm->coef=c;nterm->exp=e;nterm->link=NULL;int g=0;pre=pl;if(pl->link==NULL) //加入第一个结点pl->link=nterm;else //加入第二及以后的结点{p=pre->link;while(g==0){if( p->exp<nterm->exp){if(p->link==NULL){p->link=nterm;g=1;}else{pre=p;p=pre->link;}//向后推进}elseif( p->exp>nterm->exp){nterm->link=p;pre->link=nterm;g=1;}else //指数相等的项的系数相加{ p->coef=p->coef+nterm->coef;g=1;delete nterm;if(p->coef==0)//节点的系数为0时,删除{pre->link=p->link;delete p;}}}}}}void Output(Poly &pl) //输出多项式{term *p=pl->link;printf("输出多项式为:");int h=0;while(!h){if(pl->link!=p && p->coef>0)//最初不输出+号printf("+");printf("%0.1f",p->coef);switch(p->exp)//输出指数格式{case 0:break;case 1:printf("X");break;default :printf("X^");printf("%d",p->exp);}if(p->link==NULL)//循环结束条件h=1;elsep=p->link;}printf("\n");}void Add(Poly &A, Poly &B, Poly &C ,int Q) //多项式加减//两个一元多项式链表的头结点分别是A,B,相加减结果为C {term *pa,*pb,*pc,*p,*s;float temp;C=new term;C->link=NULL;pc=C;pa=A->link;pb=B->link;while(pa!=NULL && pb!=NULL)//pa,pb都存在时{s=new term;if(!s){printf("error");exit(1);}if(pa->exp==pb->exp) //系数相等{if(Q==1) //多项式相加temp=pa->coef+pb->coef;else //多项式相减if(pa->coef>pb->coef)temp=pa->coef-pb->coef;elsetemp=pb->coef-pa->coef;if(temp>0){pc->link=s;pc=pc->link;pc->coef=temp;pc->exp=pa->exp;}else//加减后的系数为0,删除delete s;pa=pa->link;pb=pb->link;}else //系数不相等if(pa->exp<pb->exp){pc->link=s;pc=pc->link;pc->coef=pa->coef;pc->exp=pa->exp;pa=pa->link;}else{pc->link=s;pc=pc->link;pc->coef=pb->coef;pc->exp=pb->exp;pb=pb->link;}}p=(pa?pa:pb);//P指向剩余链表的地址while(p!=NULL)//剩余结点原样输入{pc->link=new term;if(!pc->link){printf("error");exit(1);}pc=pc->link;pc->coef=p->coef;pc->exp=p->exp;p=p->link;}pc->link=NULL;}void main(){Poly pa,pb,pc;printf("\n");printf("//**此程序可以实现两个多项式的加减**//\n");printf("*****输入第一个多项式*****:\n");Input(pa);Output(pa);printf("\n");printf("\n");printf("*****输入第二个多项式*****:\n");Input(pb);Output(pb);int Q;printf("/////////请选择操作/////////\n");printf("********1:两多项式相加*******\n");printf("********2:两多项式相减*******\n");scanf("%d",&Q);Add(pa,pb,pc,Q);printf("你所需要的结果是:\n");Output(pc);}测试结果。

多项式求和程序

多项式求和程序

多项式求和:例如:x^2+3x^4+5x^6+7x^8 则输入1 2 3 4 5 6 7 8 0 0 其中输入0 0用于结束!#include <stdio.h>#include <stdlib.h>#define LEN sizeof(struct entry)struct entry{int coef;int expn;struct entry *next;};void init(struct entry *strPer){struct entry *p,*p1,*p2;int m;int n;int k;k=0;p=strPer;(*p).coef=0;(*p).expn=-1;p1=p2=(struct entry *) malloc (LEN);printf("请输入多项式:\n");scanf("%d%d",&p1->coef,&p1->expn);p->next=NULL;while (p1->coef!=0){k=k+1;if (k==1) p->next=p1;else p2->next=p1;p2=p1;p1=(struct entry *) malloc (LEN);scanf("%d%d",&p1->coef,&p1->expn);}p2->next=NULL;printf("您输入的多项式是:\n");p=strPer->next;do {if(p->coef==1){printf("x^%d",p->expn);}else if (p->coef==-1){printf("-x^%d",p->expn);}else{if(p->coef>0) printf("+%dx^%d",p->coef,p->expn);else printf("%dx^%d",p->coef,p->expn);}p=p->next;}while(p!=NULL);printf(" \n");}int cmp(int a,int b){if (a<b) return -1;if (a=b) return 0;if (a>b) return 1;}struct entry * delFirst(struct entry *h,struct entry *q){q=h->next;h->next=(h->next)->next;return (q);}void insFirst(struct entry *h,struct entry *q){q->next=h->next;h->next=q;}void append(struct entry *h,struct entry *q){while (h->next!=NULL){h=h->next;}h->next=q->next;}void iput(struct entry *strPer){struct entry *p;p=strPer->next;printf("相加得到的多项式是:\n");do{if(p->coef==1){printf("x^%d",p->expn);}else if (p->coef==-1){printf("-x^%d",p->expn);}else{if(p==strPer+1){if(p->coef>0) printf("%dx^%d",p->coef,p->expn);else printf("%dx^%d",p->coef,p->expn);}else{if(p->coef>0) printf("+%dx^%d",p->coef,p->expn);else printf("%dx^%d",p->coef,p->expn);}}p=p->next;}while(p!=NULL);}void main(){struct entry *strPerA;struct entry *strPerB;struct entry *ha;struct entry *hb;struct entry *qa;struct entry *qb;int a;int b;int sum;int k;int h;h=0;strPerA=(struct entry *) malloc (LEN);strPerB=(struct entry *) malloc (LEN);if (strPerA!=NULL && strPerB!=NULL){init(strPerA);init(strPerB);}ha=strPerA;hb=strPerB;qa=ha->next;qb=hb->next;while(qa && qb){a=qa->expn;b=qb->expn;k=cmp(a,b);h=h+1;switch (k){case -1:ha=qa;qa=qa->next;break;case 0 :sum=qa->coef+qb->coef;if (sum!=0){(*qa).coef=sum;ha=qa;}else {delFirst(ha,qa);}qb=delFirst(hb,qb);//free(qb);qb=qb->next;qa=ha->next;break;case 1:qb=delFirst(hb,qb);insFirst(ha,qb);qb=hb->next;ha=ha->next;break;}}if (strPerB->next!=NULL) append(strPerA,strPerB); iput(strPerA);free(strPerB);}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

printf("%.2fx^%d\n",p->coef,p->expn); } int cmp(int f,int g) //比较函数,用来判定当前两多项式指数问题 {if (f<g) return (-1); else if(f==g) return 0; else return (1); } v多项式相加 list hn,hm,pn,pm;float sum; pn=n->next ;pm=m->next ;hn=n;hm=m;//定义一个当前节点的指针和一个头指针 while(hn->next&&hm->next) {switch (cmp(pn->expn,pm->expn))//比较两指数 {case -1: hn=pn; pn=pn->next; break; case 0: sum=pn->coef +pm->coef ; if(sum!=0) {pn->coef =sum;pm=pm->next =pm;hn=hn->next ;pn=pn->next ; }//if else {hn->next =pn->next ; free(pn); pn=hn->next ; hm->next =pm->next ; free(pm); pm=hm->next ;}break;//else
}/*chongpaixu(l);*/ if(l->next ){ printf("你输入的多项式是 f(x)="); putout(l);}else printf("你输入的多项式是 f(x)=0\n"); } else printf("你输入的多项式 l 不存在\n"); printf("请输入 s 的项数\n"); scanf("%d",&d); if(d>0) {for (i=0;i<d;i++) {printf("请输入第%d 项的系数\n",i+1); scanf("%f",&coe); printf("请输入第%d 项的 r 指数\n",i+1); scanf("%d",&exp);if(coe){ insertlist(hs,q,w);hs=hs->next ;} }if(s->next){ printf("你输入的多项式是 f(x)="); putout(s);} else printf("你输入的多项式是 f(x)=0\n"); } else printf("你输入的多项式不存在\n"); hl=l; hs=s;
;free(hm->next);hm->next
case 1:hm->next =pn;hn=pm;pn=pn->next ;pm=hm->next ;break; }//switch
=pm->next
;hn->next=pm;pm->next
}//while if(hm->next ) //参考书本 43 页算法的思想,将剩余结点插入当前链表中 {hn->next=pm;free(hm);} }//addlist
{list l,s,hl,hs; int i,d,exp;float *q;int *w; float coe; putmessage(); q=&coe; w=&exp; l=initlist(); s=initlist(); hl=l; hs=s; printf("请输入 l 的项数\n"); scanf("%d",&d); if(d>0) {for (i=0;i<d;i++) {printf("请输入第%d 项的系数\n",i+1); scanf("%f",&coe); printf("请输入第%d 项的指数\n",i+1); scanf("%d",&exp); if(coe){ insertlist(hl,q,w);hl=hl->next ;}
#include "stdafx.h"//备注:我用的编译平台所提供的头文件不是“stdio.h" #include"malloc.h" #include<cstdlib> //此头文件用来解决执行可以执行文件 exe 后直接退出的问题, 希望可以帮 到大家了解一个新函数和头文件
typedef struct List{ //定义一个动态链表 float coef; int expn; struct List *next; }*list; list initlist()//初始化,生成新结点 {list l;l=(list)malloc(sizeof(List)); if (!l) printf("error"); l->next=NULL; return l; } void insertlist (list l,float *coe,int *exp)//每次 scanf 后插入结点,从链尾插入,main 函数中定 义一个链表尾指针 {list s; s=(list)malloc(sizeof(List)); if(!s)printf("error"); s->coef=*coe; s->expn=*exp; l->next =s; s->next =NULL; } void putout(list l)//输出链表 {list p; p=l->next; while(p->next) { if(p->expn==0) printf("1+"); else if(p->coef==1&&p->expn==1) {printf("x+");} else printf("%.2fx^%d+",p->coef,p->expn); p=p->next ; } if(p->expn==0) printf("1\n"); else if(p->coef==1&&p->expn==1) {printf("x\n");} else
}//while
} void putmessage(void)//用来表明备注 {printf("备注:该算法经过上课时老师给我们提出的问题进行了修改,不过关于排序的算法 还不能完善\n"); printf("因此,请输入时请将多项式按照升序输入,将相同指数合并!\n"); printf("\n");printf("\n");printf("\n");printf("\n");printf("\n");printf("\n"); } void main()
void chongpaixu(list l)//将输入的多项式按升序排列,并将指数相同的合并(还不能执行) { list s;list q;list k;list w;float sum;k=initlist(); q=l->next ;s=l; while(q->next){ for(w=l ;q->next !=NULL;q=q->next ) {for (s=s->next ;s->next!=NULL;s=s->next ) {switch(cmp(s->expn,q->expn)) {case -1:w=w->next ;break; case 1:k->coef=q->coef; q->coef=s->coef; s->coef=k->coef; k->expn=q->expn ; q->expn =s->expn ; s->expn =k->expn ;free(k);w=w->next ;break; case 0:sum=s->coef+q->coef; if(sum) {s->coef=sum; s->next=q; free(q); q=s->next ;}//if else {w->next=q->next; free(s);free(q); s=w->next ; q=s->next ;}//else break; }//switch } }
if(hl->next &&hs->next ) {addlist(l,s);printf("合并后的多项式 f(x)="); if(l->next){ putout(l);} else printf("0\n");} else printf("no add\n"); system ( "pause" );//不加此语句,exe 文件在执行完毕直接跳出 }
相关文档
最新文档