| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 373 人关注过本帖
标题:刚忙改正一下 代码
只看楼主 加入收藏
yxy1008611
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-9-8
结帖率:100%
收藏
 问题点数:0 回复次数:0 
刚忙改正一下 代码
代码存在逻辑错误,帮忙改正一下,谢谢,


#include<stdio.h>
#include<malloc.h>
typedef struct LNode{
        float coef;  //多项式的系数
        int expn;    //多项式的指数
        struct LNode *next;  //指向下一个多项式的结点的指针
}LNode,*polynomial; //定义一个类型变量名LNode,定义一个新的类型Linklist,该类型变量是指向
                  //多项式结点的指针

int main()
{
    void creat(polynomial);//输入并建立多项式L
    void display(polynomial);//输出多项式L
    void sort(polynomial);//多项式按指数排序
    void add(polynomial,polynomial,polynomial);
    void sub(polynomial,polynomial,polynomial);

    polynomial La,Lb,Lc,Ld;

    printf("输入第一个多项式:");
    La=(polynomial)malloc(sizeof(LNode));
    creat(La);
    sort(La);
    display(La);

    printf("输入第二个多项式:");
    Lb=(polynomial)malloc(sizeof(LNode));
    creat(Lb);
    sort(Lb);
    display(Lb);
   
    printf("多项式之和:");
    Lc=(polynomial)malloc(sizeof(LNode));
    add(La,Lb,Lc);
    printf("多项式之差:");
    Ld=(polynomial)malloc(sizeof(LNode));
    sub(La,Lb,Ld);

    return 0;
   
}

void creat(polynomial L) //输入并建立多项式
{
      int i,n;
      static struct LNode *p;
      printf("\n请输入项数:");
      scanf("%d",&n);//项数
      L=(polynomial)malloc(sizeof(LNode));//建立头结点
      L->next = NULL;
      for(i=0;i<n;i++)
      {
         p=(polynomial)malloc(sizeof(LNode));
         scanf("%f,%d",&p->coef,&p->expn);
         p->next = L->next;
         L->next = p;
       }
}

void display(polynomial L) //输出多项式
{
      static struct LNode *p,*q;
      int j = 0;
      int k = 0;
      q = L->next;
      while(q)
      {
         if(q->coef != 0)
            k++;
            q = q->next;
      }
      printf("%d",k);
      p = L->next;
      if(p->coef != 0)
      {
        printf("%f,%d",p->coef,p->expn);
        j++;
      }
      for(p = p->next;p;p = p->next)
      {
        if(p->coef != 0)
        {
            printf("%f,%d",p->coef,p->expn);
            j++;
        }
      }
      if(j==0)
           printf("%d\n",j);
      else
          printf("\n");

}

void sort(polynomial L)//多项式L按指数排序
{
polynomial p, q, r, u;
p = L->next;
L->next = NULL;
while(p != NULL)
{
   r = L;
   q = L->next;
   while((q != NULL) && (q->expn <= p->expn))
   {
    r = q;
    q = q->next;
   }
   u = p->next;
   r->next = p;
   p->next = q;
   p = u;
}
}

void add(polynomial x,polynomial y,polynomial z) //多项式相加
{
     static struct LNode *pa,*pb,*pc;

     z=(polynomial)malloc(sizeof(LNode));
     pa = x->next;
     pb = y->next;
     z->next = NULL;
     while(pa && pb)
     {
        pc=(polynomial)malloc(sizeof(LNode));
        if(pa->expn < pb->expn)
        {
           pc->next = z->next;
           z->next = pc;
           pc->coef = pa->coef;
           pc->expn = pa->expn;
           pa = pa->next;

        }
        else if(pa->expn == pb->expn)
        {
           pc->next = z->next;
           z->next = pc;
           pc->expn = pa->expn;
           pc->coef = pa->coef + pb->coef;
           pa = pa->next;
           pb = pb->next;

        }
        else
        {
         pc->next = z->next;
         z->next = pc;
         pc->coef = pb->coef;
         pc->expn = pb->expn;
         pb = pb->next;
         }
  }
     while(pa)
     {
        pc = (polynomial)malloc(sizeof(LNode));
        pc->next = z->next;
        z->next = pc;
        pc->coef = pa->coef;
        pc->expn = pa->expn;
        pa = pa->next;
      }
     while(pb)
     {
      pc = (polynomial)malloc(sizeof(LNode));
      pc->next = z->next;
      z->next = pc;
      pc->coef = pb->coef;
      pc->expn = pb->expn;
      pb = pb->next;
      }
}

 void sub(polynomial x, polynomial y, polynomial z)//多项式的差
{
struct LNode *pa, *pb;
static struct LNode *pd;
z = (polynomial)malloc(sizeof(LNode));
pa = x->next;
pb = y->next;
z->next = NULL;
while(pa && pb)
{
   pd = (polynomial)malloc(sizeof(LNode));
   if(pa->expn < pb->expn)
   {
    pd->next = z->next;
    z->next = pd;
    pd->coef = pa->coef;
    pd->expn = pa->expn;
    pa = pa->next;
   }
   else
    if(pa->expn == pb->expn)
    {
     pd->next = z->next;
     z->next = pd;
     pd->expn = pa->expn;
     pd->coef = pa->coef - pb->coef;
     pa = pa->next;
     pb = pb->next;
    }
    else
    {
     pd->next = z->next;
     z->next = pd;
     pd->coef = pb->coef;
     pd->expn = pb->expn;
     pb = pb->next;
    }
}
while(pa)
{
   pd = (polynomial)malloc(sizeof(LNode));
   pd->next = z->next;
   z->next = pd;
   pd->coef = pa->coef;
   pd->expn = pa->expn;
   pa = pa->next;
}
while(pb)
{
   pd = (polynomial)malloc(sizeof(LNode));
   pd->next = z->next;
   z->next = pd;
   pd->coef = -pb->coef;
   pd->expn = pb->expn;
   pb = pb->next;
}
}

搜索更多相关主题的帖子: 代码 
2009-10-11 15:29
快速回复:刚忙改正一下 代码
数据加载中...
 
   



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

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