⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 多项式相加.c

📁 采用c++语言写的小程序
💻 C
字号:
#include <stdio.h>
#include <conio.h>
typedef struct Polynode
{
int coef;
int exp;
struct Polynode *next;        
}Polynode,*Polylist;

Polylist PolyCreat()
{
Polynode *head,*r,*s;
int c,e;
head=(Polylist)malloc(sizeof(Polynode));
head->next=NULL;
r=head;
printf("qing shu ru coef and exp:coef,exp\n");
scanf("%d,%d",&c,&e);
printf("when coef=0 is end\n");         
while(c!=0)
{
s=(Polynode*)malloc(sizeof(Polynode));
s->coef=c;
s->exp=e;
r->next=s;
r=s;  
printf("qing shu ru coef and exp:coef,exp\n");
scanf("%d,%d",&c,&e);
printf("when c=0 is over\n");           
}
r->next=NULL;
printf("sucess create\n");
return (head);
}
void  PolyAdd(Polylist polya,Polylist polyb)
{
 Polynode *p,*q,*pre,*temp;
 int sum;
 p=polya->next;
 q=polyb->next;
 pre=polya;
 while(q!=NULL&&p!=NULL)
  {
    if(p->exp<q->exp)
    {
    pre->next=p;
    p=p->next;               
    }
    else if(p->exp==q->exp)
     {
     sum=p->coef+q->coef;
     if(sum!=0)
       {
       p->coef=sum;pre->next=p;pre=pre->next;
       p=p->next;temp=q;q=q->next;
       free(temp);
       }
       else 
         {
         temp=p->next;free(p);
         p=temp;
         temp=q->next;
         free(q);
         q=temp;         
         }    
     }
     else 
         {
         pre->next=q;pre=pre->next;
         q=q->next;     
         }
     
  }
  if(p!=NULL) pre->next=p;
  else pre->next=q;
}
int Output(Polylist temp,int x)
{
Polylist p;
int t=1,sum=0;
int i;

p=temp->next;
while(p!=NULL)
  {
              t=1;
     for(  i=0;i<p->exp;i++)
       {
       t=t*x;    
       printf("the p->coef and p->exp :%d,%d",p->coef,p->exp);
       }
       sum=sum+p->coef*t;
       p=p->next;
  } 
   
   return sum;
}
int  main()
{
Polynode *ha,*hb;
int x;
printf("creat two Polylist\n");
printf("creat ha\n");
ha=PolyCreat();
printf("creat hb\n");
hb=PolyCreat();
printf("ha+hb\n");
PolyAdd(ha,hb);
printf("input the X's value\n");
scanf("%d",&x);
//printf("\n polya's value is :%d ",Output(ha,x));
printf("\nThe polylist 's value is :%d\n",Output(*ha,x)); 
getch();  
return 0;  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -