首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c语言有关问题

2012-03-16 
c语言问题多项式相加:#includestdlib.h #includestdio.h structlist{intcoefintexpstructlist*next

c语言问题
多项式相加:
#   include   "stdlib.h "
#   include   "stdio.h "
struct   list
{   int   coef;
    int   exp;
struct   list   *next;
};
typedef   struct   list   node;
void   main()
{   node   *ptr1,*ptr2;
    node   *creatlist(node   *ptr1);
    node   *creatlist(node   *ptr2);
    node   *order(ptr1);
    node   *order(ptr2);
    node   *addpoly(node   *ptr1,node   *ptr2);
    ptr1=creatlist(ptr1);
    ptr2=creatlist(ptr2);
    ptr1=order(ptr1);
    ptr2=order(ptr2);
    view(ptr1);
    view(ptr2);
    ptr1=addpoly(ptr1,ptr2);
    view(ptr1);
}
node   *order(node   *pre)
{   node   *p=pre,*q,*l;
    int   j,i=0,n;
    q=(node   *)malloc(sizeof(node));
    while(p-> next!=NULL)
    {   i++;
        p=p-> next;
    }
    for(j=0;j <i-1;j++)
    {   p=pre-> next;
        for(n=1;n <=i-j;n++)
        {   l=p-> next;
            if((p-> exp) <(l-> exp))
            {   q-> exp=p-> exp;q-> coef=p-> coef;
p-> exp=l-> exp;p-> coef=l-> coef;
l-> exp=q-> exp;l-> coef=q-> coef;
            }
            p=p-> next;
        }       free(q);
    }
    return(pre);
}
node   *creatlist(node   *ptr)
{   node   *r,*s;
    int   num,i,n,m;
    ptr=(node   *)malloc(sizeof(node));
    ptr-> next=NULL;
    r=ptr;
    printf( "please   in   put   the   length   of   the   list   you   want   to   creat:\n ");
    scanf( "%d ",&n);
    printf( "please   input   %d   pairs   numbers==> \n ",n);
    for(i=0;i <n;i++)
    {   s=(node   *)malloc(sizeof(node));
        scanf( "%d%d ",&num,&m);
        s-> coef=num;
        s-> exp=m;
        r-> next=s;
        r=s;
      }
      r-> next=NULL;
      return(ptr);
}
view(node   *ptr)
{   node   *p;
    p=ptr-> next;
    printf( "The   poly   is   ==> \n ");
    while(p!=NULL)
{
        printf( "%dx^%d     ",p-> coef,p-> exp);
        p=p-> next;
}printf( "\n ");
}
node   *addpoly(node   *ptr1,node   *ptr2)
{   node   *p,*q,*r,*s,*pre;
    p=ptr1-> next;
    q=ptr2-> next;
    pre=(node   *)malloc(sizeof(node));
    pre-> next=NULL;
    r=pre;


    while(p!=NULL&&q!=NULL)
    {   if(p-> exp==q-> exp)
        {   if(p-> coef+q-> coef!=0)
            {s=(node   *)malloc(sizeof(node));
            s-> coef=p-> coef+q-> coef;
            s-> exp=p-> exp;
            r-> next=s;
            r=s;}
            p=p-> next;
            q=q-> next;
        }
        else   if(p-> exp> q-> exp)
        {     s=(node   *)malloc(sizeof(node));
              s-> coef=p-> coef;
              s-> exp=p-> exp;
              r-> next=s;
              r=s;
              p=p-> next;
        }
        else   if(p-> exp <q-> exp)
        {     s=(node   *)malloc(sizeof(node));
              s-> coef=q-> coef;
              s-> exp=q-> exp;
              r-> next=s;
              r=s;
              q=q-> next;;
        }
    }
    if(p!=NULL&&q==NULL)
        r-> next=p;
    else   if(p==NULL&&q!=NULL)
        r-> next=q;
    else   if(p==NULL&&q==NULL)
        r-> next=NULL;
    return(pre);
}
谁帮忙改下   随便教下指针传递之类的东西     谢谢啦

[解决办法]
node *p=pre,*q,*l;=====> 这里的声明有问题,改为node *p=pre;node *q,*l;

你的函数声明是这样的node *order(ptr1);===> 没有说明形参的类型,改为node *order(node *ptr1);和node *order(ptr2);===> node *order(node *ptr2);

还有你的指针变量ptr1,ptr2使用前未声明

热点排行