多项式加减法C++源码
数据结构多项式加法减c语言代码
#include<stdio.h>#include<malloc.h>typedefstruct node{int coef;int exp;struct node *link;}PNode,*Plinklist;Plinklist Creat(int n){Plinklist p,r=NULL,list=NULL;int coef;int exp;int i;for(i=1;i<=n;i++){scanf("%d %d",&coef,&exp);p=(Plinklist)malloc(sizeof(PNode));p->coef=coef;p->exp=exp;p->link=NULL;if(list==NULL)list=p;elser->link=p;r=p;}return(list);}Plinklist ATTACH(int coef,int exp,Plinklist r) {Plinklist w;w=(Plinklist)malloc(sizeof(PNode));w->exp=exp;w->coef=coef;r->link=w;return w;}Plinklist PADD(Plinklist a,Plinklist b){Plinklist c;Plinklist r,p=a,q=b;int x;c=(Plinklist)malloc(sizeof(PNode));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;}elseif(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 Output(Plinklist t){Plinklist m;m=t;if(t->exp==0)printf("%d",t->coef);else printf("%d*x^%d",t->coef,t->exp);t=t->link;while(t!=NULL){if(t->coef>0&&t->exp!=0)printf("+%d*x^%d",t->coef,t->exp);elseif(t->coef>0&&t->exp==0)printf("+%d",t->coef);elseif(t->coef<0&&t->exp!=0)printf("%d*x^%d",t->coef,t->exp);elseif(t->coef<0&&t->exp==0)printf("%d",t->coef);t=t->link;}}void main(){Plinklist a,b,c=NULL;int a1,b1;printf("\na的项数是:");scanf("%d",&a1);a=Creat(a1);printf("a=");Output(a);printf("\nb的项数是:");scanf("%d",&b1);b=Creat(b1);printf("b=");Output(b);printf("\n");printf("c=");c=PADD(a,b);Output(c);printf("\n运行结束O(∩_∩)O!\n");}。
多项式加法(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++代码多项式加减乘#includeusing namespace std;typedef struct Elem{double coeff;double exp;}Pair;typedef struct poly{Pair data;struct poly *next;}*pPolyList,PolyList;//比较两个数的大小,按结果返回int cmp(double a,double b){if(a > b )return 1;else if(a==b)return 0;return -1;}//初始化多项式链表,按组输入,直到输入的为两个0 0 表示链接结束void Init_Poly( pPolyList* ha){double coeff;double exp;pPolyList pa;coeff=0;exp=0;(*ha)=(pPolyList)malloc( sizeof(struct poly) ); memset(*ha,0,sizeof(PolyList));(*ha)->next=NULL;while(1){scanf("%lf %lf",&coeff,&exp);if(coeff ==0.0 && exp==0.0)break;pa=(pPolyList)malloc(sizeof(struct poly));(pa->data).coeff=coeff;(pa->data).exp = exp;pa->next=(*ha)->next;(*ha)->next=pa;}}//打印多项式链表void Print_List(pPolyList ha){pPolyList pa;pa=ha;if(!ha || !ha->next )return;if(ha->data.coeff==0.0)pa=ha->next;printf(" \n");while(pa){printf("%0.2lfX%0.2lf ",pa->data.coeff,pa->data.exp); pa=pa->next;}//释放多项式链表void FreeList(pPolyList* ha){pPolyList pa,t;pa=*ha;if(!pa)return;t=pa;do{t=pa;pa=pa->next;free(t);}while(pa);*ha=NULL;t=NULL;}//将两个多项式链表(已经排序),归并排序void MergeList(pPolyList ha,pPolyList hb,pPolyList* hc) { pPolyList pa,pb,pc;*hc=(pPolyList)malloc(sizeof(PolyList));memset((*hc),0,sizeof(PolyList));pa=ha->next;pb=hb->next;pc=*hc;while(pa && pb){if( pa->data.exp < pb->data.exp )pc->next=pa;pa=pa->next;}else{pc->next=pb;pb=pb->next;}pc=pc->next;}pc->next=pa ? pa:pb;}//将多项式链表按照指数的大小排序void Sort_List(pPolyList ha){pPolyList t,p,middle;middle=NULL;p=ha;if(!ha || !ha->next)return;while(ha->next->next != middle){t=ha;p=t->next;while(1){if(t->next->data.exp > p->next->data.exp ) {t->next=p->next;p->next=t->next->next;t->next->next=p;}else{p=p->next;}t=t->next;if( (p->next) == middle ){middle=p;break;}}}}//两多项式相加,void Add_Polynomial(pPolyList ha,pPolyList hb){pPolyList pa,pb,qa,qb,t;int flag;double sum;pa=ha; //pa为得到的相加之后的链表的游标pb=hb;qa=ha->next;qb=hb->next;while( qa && qb )//开始的时候qa,qb分别指向ha,hb的两个链表的第一个数据元素,{//比较两个系数的大小,进行不同的操作switch( cmp(qa->data.exp,qb->data.exp) ){case -1:pa->next=qa;qa=qa->next;pa=pa->next;break;case 0:sum=qa->data.coeff+qb->data.coeff;if( 0.0 != sum){pa->next=qa;pa->next->data.coeff=sum;qa=qa->next;pa=pa->next;//系数相同,将系数放到qa上,删除当前qb节点t=qb->next;free(qb);qb=t;}else //如果两个指数相同,且系数加起来为0,那么就的删除两个节点{t=qa->next;free(qa);qa=t;t=qb->next;free(qb);qb=t;}break;case 1:pa->next=qb;qb=qb->next;pa=pa->next;break;}//switch}//whilepa->next=qb?qb:NULL;}void main(){pPolyList hc=NULL;pPolyList ha=NULL;pPolyList hb=NULL;Init_Poly(&ha); //初始化两个链表Init_Poly(&hb);Print_List(ha);Print_List(hb);Sort_List(ha); //对两个链表排序Sort_List(hb);Print_List(ha); //打印排序后的两个多项式链表Print_List(hb); //MergeList(ha,hb,&hc); //对两个Add_Polynomial(ha,hb);Print_List(ha);FreeList(&ha);}。
数据结构C语言实现多项式源代码
一、一元稀疏多项式计算器#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define OK 1#define ERROR 0#define OVERFLOW -1typedef int Status;#define LIST_INIT_SIZE 51 //假设次数不超过50typedef struct{//coef表示系数,expo表示指数。
float coef;int expo;}ElemType;typedef struct LNode{ //结点类型ElemType data;struct LNode *next;}LNode,*LinkList;//记:Link与Position均为指向LNode的指针类型typedef LinkList Polynomail;//在单链表的基础上定义并实现//多项式用链表存储Status CreateList_L(LinkList &L){int n;printf("请输入需创建的多项式的长度:\n");scanf("%d",&n);LNode *CurPtr,*PrePtr;//创建递增链表L=(LinkList) malloc(sizeof(LNode));L->next=NULL;if(!L) exit(OVERFLOW);PrePtr=L;for(int i=1;i<=n;++i){CurPtr=(LNode *)malloc(sizeof(LNode));printf("请输入不同的多项式的非0系数:");scanf("%f",&(CurPtr->data).coef );printf("请输入不同的多项式的指数:");scanf("%d",&(CurPtr->data).expo );CurPtr->next=NULL;PrePtr->next=CurPtr;PrePtr=PrePtr->next;//插入到表头}CurPtr->next =L;//冒泡排序法对多项式按指数由大到小排列。
一元多项式加法、减法、乘法实现源代码
⼀元多项式加法、减法、乘法实现源代码////链接程序:#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();}。
多项式运算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语言程序
#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++
#include<iostream>#include<cmath>using namespace std;//多项式节点的定义struct Term{float coef; // 系数int exp; // 指数Term * link;Term(float c,int e,Term *next=NULL){coef=c;exp=e;link=next;}Term* InsertAfter(float c,int e);friend ostream& operator<<(ostream&,const Term&);};//多项式类的定义class Polynomial{public:Polynomial(); //构造函数Polynomial(Polynomial& R); //复制构造函数int maxOrder(); // 计算最大阶数,并返回Term * getHead() const { return first;} //取得多项式单链表的表头指针private:Term *first;friend ostream& operator<<(ostream&,const Polynomial&); //重载运算符<< friend istream& operator>>(istream&,const Polynomial&); //重载运算符>> friend Polynomial operator + (Polynomial&,Polynomial&); //重载运算符+ friend Polynomial operator * (Polynomial&,Polynomial&); //重载运算符* };//在当前由this指针指示的项后面插入一个新项Term* Term::InsertAfter(float c,int e){link=new Term(c,e,link); //创建一个新节点,自动链接return link; // 插入到this后面}// Term的友元函数,输出一个节点x的内容到输出流out中ostream& operator<<(ostream& out,const Term& x){if(x.coef==0) //系数为零,不输出return out;out<<x.coef; //输出系数switch(x.exp){ //输出指数case 0: break;case 1: out<<"X";break;default:out<<"X"<<x.exp;break;}return out;}//Polynomial类的构造函数,建立一个空链表Polynomial::Polynomial(){first=new Term(0,-1);}//复制构造函数,用已有的对象初始化当前对象Polynomial::Polynomial(Polynomial& R){first=new Term(0,-1);Term *destptr=first,*srcptr=R.getHead()->link;while(srcptr!=NULL){destptr->InsertAfter(srcptr->coef,srcptr->exp);srcptr=srcptr->link;destptr=destptr->link;}}//计算最大阶数,当多项式按升序排列时,最后一项是指数最大的int Polynomial::maxOrder(){Term* current=first;while(current->link!=NULL)current=current->link;return current->exp;}//Polynomial类的友元函数,从输入流in输入各项,用尾插法建立一个多项式istream& operator>>(istream& in,Polynomial& x){Term* rear=x.getHead(); // rear是尾指针int c,e;while(1){cout<<"Input a term(coef,exp):"<<endl;in>>c>>e;if(e<0) break; //e<0,退出循环,输入结束rear=rear->InsertAfter(c,e);}return in;}//Polynomial类的友元函数:输出带附加头结点的多项式链表xostream& operator<<(ostream& out,Polynomial& x){Term* current=x.getHead()->link;cout<<"The polynomal is:"<<endl;bool h=true;while(current!=NULL){if(h==false&¤t->coef>0)out<<"+";h=false;out<<*current; //调用Term类的重载运算符<<current=current->link;}out<<endl;return out;}//Polynomial类的友元函数:两个带附加头结点的按升幂排列的多项式链表的头指针分别是A.first和B.first//返回的是A+B所得的结果多项式链表Polynomial operator + (Polynomial &A,Polynomial &B){Term *pa,*pb,*pc,*p;float temp;Polynomial C;pc=C.first;pa=A.getHead()->link;pb=B.getHead()->link;while(pa!=NULL&&pb!=NULL){if( pa->exp == pb->exp ) //对应系数相等,系数相加{temp=pa->coef+pb->coef;if(fabs(temp)>0.001)pc=pc->InsertAfter(temp,pa->exp); //系数相加后不为零,将结果保存pa=pa->link;pb=pb->link;}else if(pa->exp<pb->exp){pc=pc->InsertAfter(pa->coef,pa->exp);pa=pa->link;}else{pc=pc->InsertAfter(pb->coef,pb->exp);pb=pb->link;}}if(pa!=NULL)p=pa;elsep=pb;while(p!=NULL) //处理链剩余部分{pc=pc->InsertAfter(p->coef,p->exp);p=p->link;}return C;}//Polynomial类的友元函数:将一元多项式A和B相乘,乘积用带附加头结点的单链表存储//返回值为指向存储乘积多项式的单链表的头指针Polynomial operator * (Polynomial &A,Polynomial &B){Term *pa,*pb,*pc;int AL,BL,i,k,maxExp;Polynomial C; //结果多项式pc=C.getHead();AL=A.maxOrder();BL=B.maxOrder();if(AL!=-1||BL!=-1){maxExp=AL+BL;float *result=new float[maxExp+1];for(i=0;i<=maxExp;i++)result[i]=0;pa=A.getHead()->link;while(pa!=NULL){pb=B.getHead()->link;while(pb!=NULL){k=pa->exp+pb->exp;result[k]=result[k]+pa->coef*pb->coef;pb=pb->link;}pa=pa->link;}for(i=0;i<=maxExp;i++)if(fabs(result[i])>0.001)pc=pc->InsertAfter(result[i],i);delete []result;}pc->link=NULL;return C;}int main(){Polynomial A,B,C;cout<<"请输入多项式A:(系数幂数(幂数小于0结束))"<<endl;cin>>A;cout<<"请输入多项式B:(系数幂数(幂数小于0结束))"<<endl;cin>>B;C=A+B;cout<<"A+B为:"<<C<<endl;C=A*B;cout<<"A*B为:"<<C<<endl;system("pause");return 0;}。
多项式计算器C语言代码
printf("x^%d",p->exp);
}
else if(p->exp!=1){printf("+x^%d",p->exp);}
else if(p->coef!=1){
do{
printf("输入有误!已输入过常数项,请输入非常数项:");
printf("输入系数%d:",i);
scanf("%d",&c);
printf("输入指数%d:",i);
scanf("%d",&e);
}while(e==0);
}
else if(p->exp!=1&&p->coef==1){printf("+x^%d",p->exp);}
else if(p->coef!=1&&p->exp==1){
printf("%d",p->coef);
printf("+x",p->exp);}
Node* q;
q=L;
while(q->next!=NULL&&q->exp!=e){
q=q->next;
}
if(q->next==NULL)return 1;
else return 0;
}
int compare(int a,int b){
(2023)一元稀疏多项式计算器实验报告c编写,附源代码(一)
(2023)一元稀疏多项式计算器实验报告c编写,附源代码(一)实验报告:(2023)一元稀疏多项式计算器实验目的本实验旨在编写一款一元稀疏多项式计算器,实现对两个多项式的加、减、乘、求导、求值等操作。
实验环境•操作系统:Windows 10•开发工具:Visual Studio Code•编程语言:C实验过程1. 首先定义多项式的结构体typedef struct PolyTerm {int coef;// 系数unsigned int power;// 指数struct PolyTerm* next;// 下一项}PolyTerm;typedef struct Poly {int degree;// 多项式最高次项PolyTerm* head;// 首项指针}Poly;2. 实现多项式的输入与输出void inputPoly(Poly* poly);// 输入多项式void outputPoly(Poly* poly);// 输出多项式3. 实现多项式的加、减、乘操作Poly* addPoly(Poly* p1, Poly* p2);// 多项式加法Poly* subPoly(Poly* p1, Poly* p2);// 多项式减法Poly* multPoly(Poly* p1, Poly* p2);// 多项式乘法4. 实现多项式求导void derivative(Poly* poly);// 多项式求导5. 实现多项式求值int evaluate(Poly* poly,int x);// 多项式求值6. 主函数的实现主函数通过简单的菜单方式,实现用户输入选项,选择需要进行的操作。
实验结果通过对多项式加、减、乘、求导、求值等操作的实现,成功完成了一元稀疏多项式计算器的编写,实现了对多项式运算的基本掌握。
实验总结在本次实验中,我们通过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语言版#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多项式加减
多项式加减#include"stdio.h"#include"string.h"#include"stdlib.h"#include"ctype.h"#include"math.h"typedef struct//定义链表数据的类型{float coef;//系数int expn;//指数}DataType;typedef struct node //定义结点{DataType data;struct node *next; //结点的指针域}ListNode;typedef ListNode * LinkList; // 自定义LinkList单链表类型int q; //定义一个全局变量LinkList CreatListR1(); //函数,用尾插入法建立带头结点的单链表int LocateNode(LinkList head, DataType e); //函数,按值查找结点void DeleteList(LinkList head,char *key); //函数,删除指定值的结点void printlist(LinkList head); //函数,打印链表中的所有值LinkList jianPolyn(LinkList la,LinkList lb);LinkList AddPolyn(LinkList la,LinkList lb);LinkList InsertNode(LinkList head,DataType e);//==========主函数==============void main(){char a;printf("是否开始");scanf("%c",&a);while(a=='y'||a=='Y'){LinkList la,lb,lc;printf("输入第一个多项式的系数,指数输入0,0结束\n");la=CreatListR1();printf("第一个多项式:");printlist(la);printf("输入第二个多项式的系数,指数输入0,0结束\n");lb=CreatListR1();printf("第二个多项式:");printlist(lb);printf("相加输入‘+’相减输入‘-’:");scanf("%c",&a);scanf("%c",&a);if(a=='+'){lc= AddPolyn(la,lb);printf("\n相加:");printlist(lc);}if(a=='-'){lc=jianPolyn(la,lb);printf("\n相减:");printlist(lc);}printf("是否继续");scanf("%c",&a);scanf("%c",&a);}}//多项式链表的建立LinkList CreatListR1(){DataType e;LinkList pa=(LinkList)malloc(sizeof(ListNode)); //生成头结点pa->next=NULL;scanf("%f,%d",&e.coef,&e.expn);pa=InsertNode(pa,e);while(e.coef&&e.expn){scanf("%f,%d",&e.coef,&e.expn);if(!LocateNode(pa,e)&&e.coef&&e.expn){pa=InsertNode(pa,e);}}return pa; //返回头指针}//按值查找结点,找到则返回该结点的位置,否则返回NULL========== int LocateNode(LinkList head, DataType e){ListNode *p=head->next; //从开始结点比较q=0;while(p&&e.expn<p->data.expn) //直到p为NULL或p-> data为key止{p=p->next; //扫描下一个结点q++; //记录要插入的位置}if(p==NULL||e.expn!=p->data.expn)return 0; //若p=NULL则查找失败,数据不存在,返回0elsereturn 1;}//===========打印链表=======void printlist(LinkList head){ListNode *p=head->next; //从开始结点打印while(p){printf("%c %fx^%d",(p->data.coef>0?'+':' '),p->data.coef,p->data.expn); p=p->next;}printf("\n");}//有序的链表节点插入LinkList InsertNode(LinkList head,DataType e){ListNode *s,*p=head;int i=0;while(p->next&&i<q){p=p->next;i++;}s=(ListNode*)malloc(sizeof(ListNode));s->data.coef=e.coef;s->data.expn=e.expn;s->next=p->next;p->next=s;return head;}//多项式链表的相加LinkList AddPolyn(LinkList la,LinkList lb){ListNode *pa,*pb,*pc,*s;LinkList lc;float sum;pa=la->next;pb=lb->next;lc=pc=la;while(pa&&pb){if(pa->data.expn>pb->data.expn){pc->next=pa;pc=pa;pa=pa->next;}else if(pa->data.expn<pb->data.expn){pc->next=pb;pc=pb;pb=pb->next;}else{sum=pa->data.coef+pb->data.coef;if(fabs(sum)>1e-6){pa->data.coef=sum;pc->next=pa;pc=pa;pa=pa->next;s=pb;pb=pb->next;free(s);}else{s=pa;pa=pa->next;free(s);s=pb;pb=pb->next;free(s);}}}pc->next=pa?pa:pb;free(lb);return lc;}LinkList jianPolyn(LinkList la,LinkList lb) {ListNode *pa,*pb,*pc,*s;LinkList lc;float sub;pa=la->next;pb=lb->next;lc=pc=la;while(pa&&pb){if(pa->data.expn>pb->data.expn) {pc->next=pa;pc=pa;pa=pa->next;}else if(pa->data.expn<pb->data.expn) {pc->next=pb;pc=pb;pb=pb->next;}else{sub=pa->data.coef-pb->data.coef;if(fabs(sub)>1e-6){pa->data.coef=sub;pc->next=pa;pc=pa;pa=pa->next;s=pb;pb=pb->next;free(s);}else{s=pa;pa=pa->next;free(s);s=pb;pb=pb->next;free(s);}}}pc->next=pa?pa:pb;free(lb);return lc;}。
多项式相加的函数(数据结构_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语言 多项式的加减
多项式的加减详细设计#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);}测试结果。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《程序设计实践》课程实验报告学号: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.。