一元多项式加法和乘法的链表实现 10C

02-线性结构2 一元多项式的乘法与加法运算 (20分)
设计函数分别求两个一元多项式的乘积与和。

输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。

输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

我的答案是

#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode *PolyNomial;
struct PolyNode{
  int coef;
  int expon;
  PolyNomial link;
};
void Attach(int c,int e,PolyNomial* prear){
  PolyNomial P;
  P=(PolyNomial)malloc(sizeof(struct PolyNode));
  P->link=NULL;
  P->coef=c;
  P->expon=e;
  (*prear)->link=P;
  *prear=P;
}
PolyNomial Add(PolyNomial L1,PolyNomial L2){
  PolyNomial t1,t2,rear,p,front;
  p=(PolyNomial)malloc(sizeof(struct PolyNode));
  p->link=NULL;
  rear=p;
  front=rear;
  if(!L1||!L2)return NULL;
  t1=L1;
  t2=L2;
  while(t1!=NULL&&t2!=NULL){
    if(t1->expon==t2->expon){
      int sum=t1->coef+t2->coef;
      Attach(sum,t1->expon,&rear);
      t1=t1->link;
      t2=t2->link;
    }
    else if(t1->expon<t2->expon){
      Attach(t1->coef,t1->expon,&rear);
      t1=t1->link;
    }
    else{
      Attach(t2->coef,t2->expon,&rear);
      t2=t2->link;
    }
  }
  while(t1){
    Attach(t1->coef,t1->expon,&rear);
    t1=t1->link;
  }
  while(t2){
     Attach(t2->coef,t2->expon,&rear);
     t2=t2->link;
  }
  return front;
}
PolyNomial Multi(PolyNomial L1,PolyNomial L2){
  PolyNomial t1,t2,rear,p,front,t;
  int c,e;
  p=(PolyNomial)malloc(sizeof(struct PolyNode));
  p->link=NULL;
  rear=p;
  front=rear;
  t1=L1;
  t2=L2;
  while(t2){
    Attach(t1->coef*t2->coef,t1->expon+t2->expon,&rear);
  }
  t1=t1->link;
  while(t1){
    t2=L2;
    rear=p;
    while(t2){
      c=t1->coef*t2->coef;
      e=t1->expon+t2->expon;
      while(rear->link&&rear->link->expon>e){
        rear=rear->link;
      }
      if(rear->link&&rear->link->expon==e){
        if(rear->link->coef+c){
          rear->link->coef+=c;
        }
        else{
          t=rear->link;
          rear=rear->link;
          free(t);
        }
      }
      else{
        t=(PolyNomial)malloc(sizeof(struct PolyNode));
        t->coef=c;
        t->expon=e;
        t->link=rear->link;
        rear->link=t;
        rear=rear->link;
      }
      t2=t2->link;
    }
    t1=t1->link;
  }
  return front;
}
PolyNomial ReadPoly(){
  PolyNomial P,rear,t;
  P=(PolyNomial)malloc(sizeof(struct PolyNode));
  P->link=NULL;
  rear=P;
  int n,c,e;
  scanf("%d",&n);
  while(n--){
    scanf("%d %d",&c,&e);
    Attach(c,e,&rear);
  }
  t=P;
  P=P->link;
  free(t);
  return P;
}
void PrintPoly(PolyNomial P){
  printf("%d %d",P->coef,P->expon);
  P=P->link;
  while(P){
    printf(" %d %d",P->coef,P->expon);
  }  
}
int main(){
  PolyNomial L1,L2,m1,m2;
  L1=ReadPoly();
  L2=ReadPoly();
  m1=Multi(L1,L2);
  PrintPoly(m1);
  m2=Add(L1,L2);
  PrintPoly(m2);
}

编译后报错为

a.cpp: In function ‘PolyNode* ReadPoly()’:
a.cpp:105:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&n);
                 ^
a.cpp:107:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&c,&e);

求大神指点为何scanf说返回值被忽视

1个回答

编译的时候加一个-w就行了。

ychen4_13
ychen4_13 具体如何实现,加在什么位置
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问