栈的建立,删除,-----多项式相加

来源:互联网 发布:烈焰遮天全套源码 编辑:程序博客网 时间:2024/06/05 07:15

#include
#include
typedef struct polylist{
 double coef;
 int exp;
 struct polylist *next;
}polylist;
polylist *Createlist(){
 polylist *head,*tail,*p;
 int n;
 double m;
 scanf("%lf%d",&m,&n);
 head=(polylist*)malloc(sizeof(polylist));
 tail=head;
 while(m!=0){
  p=(polylist*)malloc(sizeof(polylist));
  p->coef=m;
  p->exp=n;
  tail->next=p;
  tail=p;
  scanf("%lf%d",&m,&n);

 }
 tail->next=NULL;
 return head;
}
void polyAdd(polylist *polya,polylist *polyb){
 polylist *p,*q,*tail,*temp;
 double sum;
 p=polya->next;;
 q=polyb->next;
 tail=polya;
 while(p!=NULL&&q!=NULL){
  if(p->expexp)
  {tail->next=p;tail=p;p=p->next;}
  else if(p->exp==q->exp)
  {sum=p->coef+q->coef;
  if(sum!=0)
  {p->coef=sum;tail->next=p;tail=p;p=p->next;temp=q;q=q->next;free(temp);}
  else
  {temp=p;p=p->next;free(temp);
  temp=q;q=q->next;free(temp);}
  }
  else
  {tail->next=q;tail=q;q=q->next;}
  
 }
 if(p!=NULL)
  tail->next=p;
 else
  tail->next=q;
}
void print(polylist *p){
 while(p->next!=NULL){
  p=p->next;
  printf("%.2f(x)%d+",p->coef,p->exp);
 }

}
void main()
{
 polylist *polya,*polyb;
 polya=Createlist();
 polyb=Createlist();
 polyAdd(polya,polyb);
 print(polya);
}

0 0