| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 665 人关注过本帖
标题:求找错QAQ
取消只看楼主 加入收藏
littlebella
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-10-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
求找错QAQ
#include<stdio.h>
#include<malloc.h>
   
typedef struct PolyNode *Poly;
struct PolyNode{
       int coef;
       int expon;
       struct PolyNode *next;
  };
  
  void attach(int c,int e,Poly *p);
  Poly ReadPoly();
  Poly Add(Poly p1,Poly p2);
  void PrintPoly(Poly p);
 
  int main()
  {
      Poly p1,p2,pa;
      p1=ReadPoly();
      p2=ReadPoly();
     
     
   
      pa=Add(p1,p2);
   
      PrintPoly(pa);
     
      return 0;
  }
  
   
  Poly ReadPoly()
   {
       int N,c,e;
      Poly p,rear,t;
     scanf("%d",&N);
      
      p=(Poly)malloc(sizeof(struct PolyNode));
      p->next=NULL;
      rear=p;
     
      while(N--){
          scanf("%d %d",&c,&e);
         attach(c,e,&rear);
      }
      
      t=p;
      p=p->next;
      free(t);
  
      return p;
   }
  
  
   void attach(int c,int e,Poly *p)
   {
       Poly addnode;
       addnode=(Poly)malloc(sizeof(struct PolyNode));
      addnode->coef=c;
       addnode->expon=e;
      addnode->next=NULL;
       (*p)->next=addnode;
       *p=addnode;
   }
   
  
   
   Poly Add(Poly p1,Poly p2)
   {
       Poly p,t1,t2,rear,temp;
       t1=p1;
      t2=p2;
       p=(Poly)malloc(sizeof(struct PolyNode));
       p->next=NULL;
       rear=p;
      
      while(t1&&t2){
          if(t1->expon==t2->expon){
              if((t1->coef+t2->coef)!=0){
               attach(t1->coef+t2->coef,t1->expon,&rear);
              }
             t1=t1->next;
             t2=t2->next;
          }
         else if(t1->expon>t2->expon){
              attach(t1->coef,t1->expon,&rear);
              t1=t1->next;
          }
         else {
          attach(t2->coef,t2->expon,&rear);
          t2=t2->next;
         }
      }
      for(;t1;t1=t1->next)attach(t1->coef,t1->expon,&rear);
     for(;t2;t2=t2->next)attach(t2->coef,t2->expon,&rear);
      rear->next=NULL;
      temp=p;
      p=p->next;
      free(temp);
      return p;
  }
  void PrintPoly(Poly p)
  {
      int flag=0;
     
      if(!p){
         printf("0 0");
      }
     
     while(p){
          if(!flag)flag=1;
          else printf(" ");
         
          printf("%d %d",p->coef,p->expon);
         p=p->next;
      }
     printf("\n");
   }
搜索更多相关主题的帖子: include return 
2016-10-06 22:46
littlebella
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-10-6
收藏
得分:0 
2016-10-06 22:51
快速回复:求找错QAQ
数据加载中...
 
   



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

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