标题:大侠帮我解决下,是求函数相加的,F(X1)+F(X)2=y
只看楼主
wangzhenchuan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-4-23
 问题点数:0 回复次数:0 
大侠帮我解决下,是求函数相加的,F(X1)+F(X)2=y
#include<malloc.h>
typedef struct polynomial
{
 int coef;
 int expn;
 struct polynomial *next;
}JD;
void printfpolyn(JD *pa)
{
 while(pa)
 {
  printf("%dX^%d   ",pa->coef,pa->expn);
   pa=pa->next;
 }
}
JD *creatpolyn(int n)
{

 JD *l,*p;
 l=(JD *)malloc(sizeof(JD));
 l->next=NULL;
 for(;n>0;n--)
 {
   p=(JD *)malloc(sizeof(JD));
   scanf("%d,%d",&p->coef,&p->expn);
   p->next=l->next;
   l->next=p;
 }
 return(l);
}
int cmd(int a,int b)
{
  if(a<b)
  return(-1);
  else if(a==b)
  return(0);
  else
  return(1);
}
JD *Addpolyn(JD *pa,JD *pb)
{
 JD *ha,*hb,*p,*heard;
 int sum;
 ha=pa;
 hb=pb;
 heard=pa=pa->next;
 pb=pb->next;
 while(pa&&pb)
 {
   switch(cmd(pa->expn,pb->expn))
   {
     case -1: ha=pa;pa=pa->next;break;
     case 0: sum=pa->coef+pb->coef;
             if(sum!=0)
             {
             pa->coef=sum;
             ha=pa;
             pa=pa->next;
             }
             else
             {
             ha->next=pa->next;
             p=pa;
             pa=pa->next;
             free(p);
             }
             hb->next=pb->next;
             p=pb;
             pb=pb->next;
             free(p);
             break;
     case 1: hb->next=pb->next;p=pb;ha->next=p;
             p->next=pa;pb=pb->next;ha=ha->next;break;


   }
 }
 if(pa)
 ha=pb;
 free(hb);
 return(heard);
}
main()
{
 int n;
 JD *pa,*pb;
 printf("\nshu ru pa xiang shu:");
 scanf("%d  ",&n);
 pa=creatpolyn(n);
 printf("\nshu ru pb xiang shu:");
 scanf("%d  ",&n);
 pb=creatpolyn(n);
 pa=Addpolyn(pa,pb);
 printfpolyn(pa);

 getch();

}
为什么求的 时候有错啊
搜索更多相关主题的帖子: int next 函数 相加 
2008-04-23 22:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-210744-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.428200 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved