| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 461 人关注过本帖
标题:多项式加法,不知道哪里有问题,找了很久没找出来
只看楼主 加入收藏
NeQhk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:164
专家分:85
注 册:2014-7-19
结帖率:69.23%
收藏
已结贴  问题点数:5 回复次数:5 
多项式加法,不知道哪里有问题,找了很久没找出来
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct node{
   ElemType exp;
   ElemType coef;
   struct node *next;
}linklist;
//创建空链表,再主函数里输入数值,
void creat(linklist *s)
{
   s=(linklist*)malloc(sizeof(linklist));
   s->next=NULL;
}
//删除整个链表
void deletelinklist(linklist *s)
{
   linklist *pre,*p=s->next;
   while(p!=NULL)
   {
       free(pre);
       pre=p;
       p=p->next;
   }
   free(pre);
}
//插入某一个结点
void insert(linklist *s,ElemType e1,ElemType e2)
{
   linklist *p=NULL,*q=NULL;
   q=s;
   p=(linklist*)malloc(sizeof(linklist));
   p->coef=e1;
   p->exp=e2;
   p->next=q->next;
   q->next=p;
}//删除某一个结点
void deletenode(linklist *s,ElemType e1,ElemType e2)
{
  linklist *p=s,*q;
  q=p->next;
  while(p!=NULL && q->coef!=e1 &&q->exp!=e2)
  {
    p=p->next;
    q=q->next;
  }
   if(q->coef==e1 && q->exp==e2 )
   {
      p->next=q->next;
      free(q);
   }
}
//加法
linklist* jiafa(linklist *s1,linklist *s2,linklist *s3)
{
     linklist *p=s1,*q=s2,*p3=s3;;
 
   while(p!=NULL&&q!=NULL)
   {
     if(p->exp<q->exp)
     {  
        insert(s3,p->coef,p->exp);
        p=p->next;
     }
     if(p->exp>q->exp)
     {
        insert(s3,q->coef,q->exp);
        q=q->next;
     }
     if(p->exp==q->exp)
     {
        if(p->coef+q->coef==0)
        {
            p=p->next;
            q=q->next;
        }
        else
        {
               p->coef=p->coef+q->coef;
            insert(s3,p->coef,p->exp);
            p=p->next;
            q=q->next;
        }
     }
   }
   return s3;
}
void print(linklist *s)
{
   linklist *p=s;
   while(p!=NULL)
   {
      printf("%d %d",p->coef,p->exp);
      p=p->next;
   }
}
int main()
{
  linklist s1,s2,*s3=NULL;
  int n,t,i;
  scanf("%d",&n);
  creat(&s1);
  creat(&s2);
   creat(s3);
  for(i=0;i<n;i++)
  {
    while((t=scanf("%d%d",&s1.coef,&s1.exp))==2  &&  s1.exp>=0 )
    {
      insert (&s1,s1.coef,s1.exp);
    }
 
    while((t=scanf("%d%d",&s2.coef,&s2.exp))==2 &&  s2.exp>=0 )
    {
      insert (&s2,s2.coef,s2.exp);
    }
  
    s3=jiafa(&s1,&s2,s3);
     print(s3);
  }
  return 0;
     
}
搜索更多相关主题的帖子: 多项式 insert include 
2015-04-08 12:22
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:5 
唉,本想替你改改,但错误太多,再改就成我重写一遍代码了。看起来你的链表是一个带头结点的链表。

1、结点创建函数不对,要么形参使用二级指针,要么通过返回值返回创建的结点。

2、结点删除函数不对,pre指针没有初始化。

3、deletenode不对。如果while循环以p == NULL而结束,那你之后的if中q指向哪里?

4、main函数中creat的操作方式不能得到你想要的结果。

5、虽然这不影响代码的正确性,但还是希望能将单词拼对了。也不建议英文与拼音混用。

6、虽然目前电脑的内存足够大,但还是建议你在动态申请内存时加上检查代码,形成良好的习惯。

7、动态内存使用完毕记得释放,这个习惯很重要,尤其当你进行系统编程或嵌入式开发时。

至于逻辑冗余问题就不说了,继续努力吧。

重剑无锋,大巧不工
2015-04-08 14:39
NeQhk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:164
专家分:85
注 册:2014-7-19
收藏
得分:0 
回复 2楼 beyondyf
不然能不能写下插入的,我改了以后插入的还是不行
insert(linklist *s,elemtype coef,elemtype exp)
2015-04-08 15:36
NeQhk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:164
专家分:85
注 册:2014-7-19
收藏
得分:0 
回复 2楼 beyondyf
void insert(linklist *s,ElemType e1,ElemType e2)
{
  linklist *p,*q=s;
  p=(linklist *)malloc(sizeof(linklist));
  p->coef=e1;
  p->exp=e2;
  q->next=p;
  q=p;
  q->next=NULL;
}

我这样子写,用尾插法的对不对
2015-04-08 15:40
NeQhk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:164
专家分:85
注 册:2014-7-19
收藏
得分:0 
回复 2楼 beyondyf
改了一部分,好像还是有问题,你有没有空在帮个忙看下
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct node{
   ElemType exp;
   ElemType coef;
   struct node *next;
}linklist;
struct node *creat( ) //创建链表。
{
struct node *p1,*head1,*p2;
head1=(struct node*)malloc(sizeof(struct node));
p1=(struct node*)malloc(sizeof(struct node));
scanf("%d %d",&p1->coef,&p1->exp);
head1->next=p1;
p1->next=NULL;
while(1)
{
  p2=(struct node*)malloc(sizeof(struct node));
  scanf("%d %d",&p2->coef,&p2->exp);
    if(p2->exp<0) //当p2的指数小于0退出
        break;
  p1->next=p2;
  p2->next=NULL;
  p1=p2;
}
return(head1);
}
linklist *add(linklist *s1,linklist *s2)//加法函数
{
   linklist *s=NULL;
   linklist *p,*q;
   linklist *p3;
   linklist *rearc;
   rearc=s;
   p=s1->next;
   q=s2->next;
   while(q!=NULL&&p!=NULL)
   {
       if(p->exp==q->exp)
       {
          if(p->coef+q->coef!=0)
          {
             p3=(linklist *)malloc(sizeof(linklist));
             p3->coef=p->coef+q->coef;
             p3->exp=p->exp;
             p3->next=NULL;
             rearc->next=p3;
             rearc=p3;
          }
          p=p->next;
          q=q->next;
       }
       if(p->exp<q->exp)
       {
           p3=(linklist*)malloc(sizeof(linklist));
           p3->coef=p->coef;
           p3->exp=p->exp;
           p3->next=NULL;
           rearc->next=p3;
           rearc=p3;
           p=p->next;
       }
       if(p->exp>q->exp)
       {
            p3=(linklist*)malloc(sizeof(linklist));
            p3->coef=q->coef;
            p3->exp=q->exp;
            p3->next=NULL;
           rearc->next=p3;
           rearc=p3;
           q=q->next;
       }
   }
    while(p!=NULL)
    {  
         p3=(linklist*)malloc(sizeof(linklist));
         p3->coef=p->coef;
           p3->exp=p->exp;
           p3->next=NULL;
           rearc->next=p3;
           rearc=p3;
           p=p->next;
    }
   while(q!=NULL)
   {
        p3=(linklist*)malloc(sizeof(linklist));
           p3->coef=q->coef;
        p3->exp=q->exp;
       p3->next=NULL;
         rearc->next=p3;
         rearc=p3;
          q=q->next;
   }
   return s;
}
void BubbleSort(linklist *head)  //冒泡排序链表
{  
    linklist *i=NULL,*j=NULL;  
    int temp_exp;  
    int  temp_coef;  
    for(i = head->next; i!= NULL; i = i -> next)  
        for(j=i->next; j!=NULL; j=j->next)  
            if(i->exp>j->exp)  
            {  
                temp_exp=j->exp;  
                temp_coef=j->coef;  
                j->coef=i->coef;  
                j->exp=i->exp;  
                i->exp=temp_exp;  
                i->coef=temp_coef;  
            }  
}

void print(linklist *head)//打印链表
{
   linklist *current;
   current = head->next;
   while(current!=NULL)
   {
      if(current->coef!=0)
      {
        printf("%d %d ",current->coef,current->exp);
      }
      current=current->next;
   }
   printf("\n");
}
int main()//主函数
{
  linklist *s1,*s2,*s3;
  int n,i;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    s1=creat();
    BubbleSort(s1);
    print(s1);
    s2=creat();
    BubbleSort(s2);
    print(s2);
    s3=add(s1,s2);
    BubbleSort(s3);
    print(s3);
  }
  return 0;
}
2015-04-08 18:47
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
1、你的代码冗余逻辑太多。实现你想要的功能根本不需要这么长的代码,实在看不下去。有时间挑错不如替你重写一个来的快。

2、你给的分太少,结贴率又那么低。没有动力。只能说报歉了

重剑无锋,大巧不工
2015-04-08 19:11
快速回复:多项式加法,不知道哪里有问题,找了很久没找出来
数据加载中...
 
   



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

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