注册 登录
编程论坛 数据结构与算法

两个一元多项式相加

发布于 2010-05-12 18:40, 643 次点击
程序代码:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define OVERFLOW -2
#define  OK 1

typedef struct LNode
{
    float coef;
    int expn;
    struct LNode *next;
}LNode,*Linklist;

int initlist(LNode *L)
{ L=(LNode *)malloc(sizeof(LNode));
    if(!L)exit(OVERFLOW);
    L->next=NULL;
    return OK;
}

int Creatlist(LNode *L ,int n)
{ LNode *p,*q;
    int i;
    q=L;
    for(i=0;i<n;i++)
    {   
        p=(LNode *)malloc(sizeof(LNode));
        printf("Enter the num of coef and tne num of expn:");
        scanf("%f,%d",&p->coef,&p->expn);
        q->next=p;
        q=p;        
    }
  p->next=NULL;
  return OK;
}

LNode* selsort(LNode *L) {

 LNode *g, *p, *q;
  float f;

 int i, fini = 1;

 if(!L) return NULL;

    for(g = L->next;g->next&&fini;g = g->next) {
  fini = 0;
  for(p = L->next,q = L->next->next;q;p = p->next,q = q->next)
   if (p->expn < q->expn) {
    f = p->coef;i = p->expn;
       p->coef = q->coef;p->expn = q->expn;
       q->coef = f;q->expn = i;
    fini = 1;
   }

 }

 for(g = L->next,p = g->next;p;)
  if(g->expn==p->expn) {
   g->coef += p->coef;
   g->next = p->next;
   q = p;
   p = p->next;
   free(q);
  }
  else if(g->next) {
   g = g->next;
   p = p->next;
  }

 return L;
}


void PrintfPoly(LNode *L) {
  LNode *q = L->next;
  if(!q) {
   putchar('0');
      return;
  }
  if(q->coef!=1) {
     printf("%g",q->coef);
     if(q->expn==1) putchar('X');
     else if(q->expn) printf("X^%d",q->expn);
  }
  else if(!q->expn) putchar('1');
      else if(q->expn==1) putchar('X');
            else printf("X^%d",q->expn);
  q = q->next;
  while (q) {
     if(q->coef > 0) putchar('+');
     if(q->coef!=1) {
        printf("%g",q->coef);
        if(q->expn==1) putchar('X');
        else if(q->expn) printf("X^%d",q->expn);
   }
     else if(!q->expn) putchar('1');
          else if(q->expn==1) putchar('X');
                else printf("X^%d",q->expn);
     q = q->next;
  }

}

Compare(LNode *L, LNode *b) {
  if (L->next->expn < b->expn) return -1;
  if (L->next->expn > b->expn) return 1;
  return 0;
}

LNode* APolyn(LNode *L, LNode *Pb) {

 

 LNode *h, *qa = L, *qb = Pb, *p, *q;
  float sum;
  h=p = (LNode*)malloc(sizeof(LNode));
  p->next= NULL;

  while (qa && qb) {   
    switch (Compare(qa,qb)) {
      case -1:
          p->next= qb;
          p = qb;
    qb = qb->next;
          break;
      case 0:  
          sum = qa->coef + qb->coef;
          if (sum != 0.0) {
     p ->next= qa;
     qa->coef = sum;
              p= qa;
     qa = qa->next;
          }
    else {
     q = qa;
     qa = qa->next;
              free(q);
          }
    q = qb;
    qb = qb->next;
    free(q);
          break;
      case 1:  
          p->next= qa;
          p = qa;
    qa = qa->next;
          break;
    }
  }
  if (qa) p->next= qa;  
  if (qb) p->next= qb;

 
  q=h;
  h=h->next;

  free(q);
  return h;
}

LNode* A(LNode *L, LNode *Pb) {
        LNode *Pa;

 PrintfPoly(L);

 if(Pb->next && Pb->next->coef>0) printf("+");

 PrintfPoly(Pb);
   Pa= APolyn(L,Pb);

 printf(" = ");
selsort(Pa);

 PrintfPoly(Pa);

 return Pa;
}



void Addlist(LNode *L ,int X)
{
    float sum=0.00;
    LNode  *p;
    p=L->next;
    while(p!=NULL)
    {
        sum+=(p->coef)*pow(X,p->expn);
        p=p->next;
    }
    printf("sum=%f",sum);
}

void main()
{
    int X,n,m;
    LNode *L,*Pa ,*Pb;
    initlist(L);
    printf("Enter how many number! n=");
    scanf("%d",&n);
    Creatlist(L,n);
    selsort(L);
       PrintfPoly(L);printf("\n");

      initlist(Pb);
    printf("Enter how many number! m=");
    scanf("%d",&m);
    Creatlist(Pb,m);
    selsort(Pb);
       PrintfPoly(Pb);printf("\n");
         
        Pa=A(L,Pb);
        printf("\n");
    printf("Enter the number  X=");
    scanf("%d",&X);
    Addlist(Pa,X);
        getch();
}
   
   


这个程序是求两个多项式相加。这个程序看起来有点长,但是我自己感觉  错误应该在  LNode* APolyn(LNode *L, LNode *Pb) 这一块
求和之后 总是多出 一项 很大的数?
1 回复
#2
2010-05-12 19:55
感觉问题挺多的,呵呵,等等再细说,有事。。
我大致改 了下,没编你看看吧.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define OVERFLOW 0
#define  OK 1

typedef struct LNode
{
    float coef;
    int expn;
    struct LNode *next;
}LNode,*Linklist;

int initlist(LNode *L)
{ L=(LNode *)malloc(sizeof(LNode));
    if(!L)exit(OVERFLOW);
    L->next=NULL;
    return OK;
}

int Creatlist(LinkList L ,int n)//这个函数有问题.
{ LNode *p,*q;
    int i;
    float x;int y;
    q=L;
    scanf("%f",&x);
    scanf("%d",&y);
    for(i=0;i<n;i++)
    {  
        p=(LNode *)malloc(sizeof(LNode));
        p->expn=x;p->coef=y;
        /* printf("Enter the num of coef and tne num of expn:"); 不用反复这句话吧*/
        /* scanf("%f,%d",&p->coef,&p->expn); 这句最好分开写,而且中间不应该有","号 */
        q->next=p;
        q=p;
     scanf("%f",&x);
     scanf("%d",&y);      
    }
  p->next=NULL;
  return OK;
}

LinkList selsort(LinkList L)
{
 LNode *g, *p, *q;
  float f;
 int i, fini=1;
 if(!L) return NULL;
 for(g=L->next;g->next&&fini;g=g->next)
 {
  fini=0;
  for(p=L->next,q=L->next->next;q;p=p->next,q=q->next)
   if (p->expn < q->expn)
   {
    f=p->coef;i=p->expn;
    p->coef=q->coef;p->expn = q->expn;
    q->coef = f;q->expn = i;
    fini = 1;
   }
  }
for(g=L->next,p=g->next;p;g=g->next,p=p->next;)
  if(g->expn==p->expn)
  {
   g->coef+=p->coef;
   g->next=p->next;
   q=p;
   p=p->next;
   free(q);
  }
  /*else if(g->next)
  {
   g = g->next;
   p = p->next;
  }*/
return L;
}

void PrintfPoly(LinkList L) //恩,这个怎么看怎么复杂........
 {
  /* LNode *q=L->next;
  if(!q)
  {
   putchar('0');
      return;
  }
  if(q->coef!=1) {
     printf("%g",q->coef);
     if(q->expn==1) putchar('X');
     else if(q->expn) printf("X^%d",q->expn);
  }
  else if(!q->expn) putchar('1');
      else if(q->expn==1) putchar('X');
            else printf("X^%d",q->expn);
  q = q->next;
  while (q) {
     if(q->coef > 0) putchar('+');
     if(q->coef!=1) {
        printf("%g",q->coef);
        if(q->expn==1) putchar('X');
        else if(q->expn) printf("X^%d",q->expn);
   }
     else if(!q->expn) putchar('1');
          else if(q->expn==1) putchar('X');
                else printf("X^%d",q->expn);
     q = q->next;
  }
  */
  //下面的是我的,你看一下,在这基础上再改改
  LinkList p;p=L->next;
  printf("\nf(x)=");
  if(p->expn==0) printf("%f",p->coef);
  else printf("%fX^%d",p->coef,p->expn);
  p=p->next;
  while(p)
  {
    if(p->expn==0) printf("+%f",p->coef);
     else if(p->coef>0) {printf("+%fX^%d",p->coef,p->expn);}
      else {printf("-%fX^%d",p->coef,p->expn);}
    p=p->next;
  }
}
Compare(LNode *L, LNode *b)
{
  if(L->next->expn<b->expn) return -1;
  if(L->next->expn>b->expn) return 1;
  return 0;
}
LNode* APolyn(LNode *L, LNode *Pb)
{
LNode *h, *qa = L, *qb = Pb, *p, *q;
  float sum;
  p = (LNode*)malloc(sizeof(LNode));
  h = (LNode*)malloc(sizeof(LNode));
  p->next= NULL;
  while (qa && qb)
  {  
    switch (Compare(qa,qb))
   {
      case -1:
          p->next= qb;
          p = qb;
          qb = qb->next;
          break;
      case 0:
          sum = qa->coef + qb->coef;
          if (sum != 0.0)
            {
             p->next= qa;
             qa->coef=sum;
             p=qa;
             qa=qa->next;//忘把qb删除
             q=qb->next;free(q);qb=q;
             }
          else
           {
           q=qa;
           qa=qa->next;
           free(q);
           q=qb;
           qb=qb->next;
           free(q);
            }
           break;
      case 1:
          p->next=qa;
          p=qa;
          qa=qa->next;
          break;
    }
  }
  if (qa) p->next= qa;
  if (qb) p->next= qb;
 /* q=h;
  h=h->next;
  free(q); */这几句不需要
  return p;
}
/* *LNode* A(LNode *L, LNode *Pb)
 {
        LNode *Pa;
PrintfPoly(L);
if(Pb->next && Pb->next->coef>0) printf("+");
PrintfPoly(Pb);
   Pa= APolyn(L,Pb);
printf(" = ");
selsort(Pa);
PrintfPoly(Pa);
return Pa;
} */

void Addlist(LNode *L ,int X)
{
    float sum=0.00;
    LNode  *p;
    p=L->next;
    while(p!=NULL)
    {
        sum+=(p->coef)*pow(X,p->expn);
        p=p->next;
    }
    printf("sum=%f",sum);
}
void main()//我把A和APolyn合在一起了
{
    int X,n,m;
    LNode *L,*Pa ,*Pb;
    initlist(L);
    printf("Enter how many number! n=");
    scanf("%d",&n);
    Creatlist(L,n);
    selsort(L);
       PrintfPoly(L);printf("\n");
    initlist(Pb);
    printf("Enter how many number! m=");
    scanf("%d",&m);
    Creatlist(Pb,m);
    selsort(Pb);
    PrintfPoly(Pb);printf("\n");
        
    Pa=APolyn(L,Pb);
    PrintfPoly(Pa);
    printf("\n");
    printf("Enter the number  X=");
    scanf("%d",&X);
    Addlist(Pa,X);
     getch();
}


[ 本帖最后由 LegendofMine 于 2010-5-13 22:12 编辑 ]
1