02-线性结构1 一元多项式的乘法与加法运算[网易云课堂-数据结构]

来源:互联网 发布:windows 2012 kms激活 编辑:程序博客网 时间:2024/05/17 08:26

这道题从自己写,参考其他的人的代码,调了几天才拿到满分,居然是一对大括号的问题,最大的收获是编程的严谨性.


#include <stdio.h>

#include <stdlib.h>

typedef struct PolyNode *Polynomial;
struct PolyNode{
int coef;
int expon;
Polynomial link;
};
int compare(int n1,int n2);
void Attach (int coef,int expon,Polynomial *PtrRear);
Polynomial PolyRead(int N);
Polynomial PolyMult(Polynomial P1, Polynomial P2);
Polynomial PolyAdd (Polynomial P1,Polynomial P2);
void PrintPoly(Polynomial P);


int main()
{  
int n1,n2;
scanf("%d",&n1);
Polynomial P1,P2,PP,PS;
P1 = PolyRead(n1);
scanf("%d",&n2);

P2 = PolyRead(n2);


PP = PolyMult(P1,P2);
PrintPoly(PP);
PS = PolyAdd(P1,P2);
PrintPoly(PS);
    return 0;
}



Polynomial PolyRead(int N)
{
int c,e;
Polynomial P,Rear,t;
if (N  == 0) P = NULL;
else{
P = (Polynomial)malloc(sizeof(struct PolyNode));
Rear = P;
Rear->link = NULL;
for(int i = 0;i < N;i++)
{
scanf("%d %d",&c,&e);
Attach(c,e,&Rear);
}

t = P;
P = P->link;
free(t); 
}


return P;
}


void Attach (int coef,int expon,Polynomial *PtrRear)
{
if(coef != 0){
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->coef = coef;
P->expon = expon;
P->link = NULL;
(*PtrRear)->link = P;
*PtrRear = P;
}
}


Polynomial PolyAdd (Polynomial P1,Polynomial P2)
{

if(!P1 || !P2)
{
   if(!P1)
     return P2;
   else
     return P1;
  }
Polynomial P,rear, temp;
int sum;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
rear = P;

while ( P1 && P2) 
switch (compare(P1->expon, P2->expon))
{
case 1:
   Attach(P1->coef, P1->expon, &rear);
   P1 = P1 -> link;
   break;
case -1:
   Attach(P2->coef, P2->expon, &rear);
   P2 = P2 -> link;
   break;
case 0:
if(sum = P1->coef + P2->coef)
   Attach(sum, P1->expon, &rear);
   P1 = P1 -> link;
   P2 = P2 -> link;
   break;
}

for(;P1;P1 = P1->link){
Attach(P1->coef,P1->expon,&rear);}
for(;P2;P2 = P1->link) {
Attach(P1->coef,P1->expon,&rear);}

temp = P;
P = P -> link;
free(temp);
return P;
}


int compare(int n1,int n2)
{
if(n1 > n2) return 1;
else if (n1 < n2) return -1;
else return 0;
}
Polynomial PolyMult(Polynomial P1, Polynomial P2)
{
if(!P1 || !P2) return NULL;


Polynomial t1 = P1,t2 = P2;
Polynomial temp,P,Rear,t;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;
while(t2)
{
if(t1->coef*t2->coef) Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
t2 = t2 -> link;
}
t1 = t1->link;
while(t1)
{
t2 = P2;Rear = P;
while(t2)
{
int e = t1->expon + t2->expon ;
int c = t1->coef * t2->coef;
if(c){
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->link = t->link;
free(t);
}
}
else 
{
Polynomial 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;
}
temp = P;
P = P->link;
free(temp);
return P;
}
void PrintPoly(Polynomial 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->link;
}
printf("\n");
}
0 0
原创粉丝点击