Multiplication and addition of data structure-02-linear structure 2-ary polynomial (20 points)

Posted by matt_wonders on Sat, 30 Nov 2019 09:36:18 +0100

Multiplication and addition of 02-linear structured 2-ary polynomial (20 points)

The design function calculates the product sum of two univariate polynomials.

Input format:

The input is divided into two lines, each giving the number of nonzero polynomial terms, and then exponentially decreasing a polynomial nonzero coefficient and exponent (absolute values are integers not exceeding 1000).Numbers are separated by spaces.

Output format:

The output is divided into two lines, exponentially decreasing the coefficients and exponents of the product polynomial and the nonzero terms of the polynomial.Numbers are separated by spaces, but no extra spaces are allowed at the end.Zero polynomial should output 0.

Input sample:

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

Output sample:

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

Analysis

1. Are you a pat revamped wow? There are some problems with both questions. Test point 3 has errors when you input a polynomial with zero and a constant polynomial, whether it outputs or does not output a polynomial with zero coefficients.

Code

#include<stdio.h>
#include<stdlib.h>
 
typedef struct PolyNode *Polynomial;
int length=0;
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->coef=c;
  P->expon=e;
  P->link=NULL;
  (*pRear)->link=P;
  *pRear=P;
}

Polynomial ReadPoly (void)
{
  Polynomial P,Rear,t;
  int n;
  int c;
  int e;
  scanf("%d", &n);
  P=(Polynomial)malloc(sizeof(struct PolyNode));
  P->link=NULL;
  Rear=P;
  while(n--)
  {
    scanf("%d %d", &c, &e);
 
    Attach(c, e, &Rear);
  }
  t=P;
  P=P->link;
  free(t);
  return P;
}

Polynomial Mult(Polynomial P1, Polynomial P2)
{
  Polynomial t1,t2,P,Rear,t;
  int e;
  int c;
  if(!P1||!P2) return NULL;
  t1=P1;
  t2=P2;
  P=(Polynomial)malloc(sizeof(struct PolyNode));
  P->link=NULL;
	Rear=P;
  while(t2)
  {
    Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
    t2=t2->link;
    length++;
  }
  t1=t1->link;
  while(t1)
  {
    Rear=P;
    t2=P2;
    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)//Rear->link╢Макр╩мМио
      {
        if(Rear->link->coef+c) Rear->link->coef+=c;
        else{
          t=Rear->link;
          Rear->link=t->link;
          free(t);
          length--;
        }
      }
      else{
        t=(Polynomial)malloc(sizeof(struct PolyNode));
        t->coef=c;
        t->expon=e;
        t->link=Rear->link;
        Rear->link=t;
        Rear=Rear->link;//?
        length++;
      }
      t2=t2->link;
    }
    t1=t1->link;
  }
  t=P;
  P=P->link;
  free(t);
  return P;

}

void Pint(Polynomial P)
{
	int flag=0;
  int cnt = 0;
  if (!P) {printf("0 0\n"); return;}
  while(P)
  {
	if(!flag) flag=1;
	else{
		printf(" ");
	}
	printf("%d %d", P->coef ,P->expon );
	cnt ++;
	P=P->link;
  }
	printf("\n");
}
Polynomial Add(Polynomial P1, Polynomial P2) {
  Polynomial t1,t2,P,Rear,t;
  int e;
  int c;
  if(!P1||!P2) return NULL;
  t1=P1;
  t2=P2;
  
  P=(Polynomial)malloc(sizeof(struct PolyNode));
  Rear = P;
  while(t1 && t2) {
    if(t1 -> expon == t2 -> expon){
      if(t1 -> coef != -(t2 -> coef)) Attach(t1 -> coef + t2 -> coef, 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;
    }
  }
  Rear -> link = t1 ? t1 : t2;
  t=P;
  P=P->link;
  free(t);
  return P;
}

int main()
{
  Polynomial P1,P2,Pmulti, Pplus;
  P1=ReadPoly();
  P2=ReadPoly();

  Pmulti=Mult(P1,P2);
  Pplus = Add(P1,P2);

  Pint(Pmulti);
  Pint(Pplus);

  return 0;
}