7-2 univariate polynomial multiplication and addition operation (20 minutes)

 

 

#include <cstdio>
#include <cstdlib>
// 多项式相乘 相加
// 数据结构设计
typedef struct PolyNode *Polynomial;
struct PolyNode
{
	int coef;
	int expon;
	Polynomial link;
}; 

Polynomial ReadPoly();
void Attach(int c, int e, Polynomial *pRear);
Polynomial Add(Polynomial P1, Polynomial P2);
Polynomial Mult(Polynomial P1, Polynomial P2);
void PrintPoly(Polynomial P);
int Compare(int a, int b);

// 程序框架搭建
int main()
{
	Polynomial P1, P2, PP, PS;
	
	P1 = ReadPoly();
	P2 = ReadPoly();
	PP = Mult(P1, P2);
	PrintPoly(PP);
	PS = Add(P1, P2);
	PrintPoly (the PS); 
	
	return 0; 
} 

// how to read the polynomial 
to the Polynomial ReadPoly () 
{ 
	to the Polynomial P, Rear, T; 
	int C, E, N; 
	 
	Scanf ( "% D", & N); 
	P = (to the Polynomial) the malloc (sizeof (struct PolyNode)); // empty list head node of 
	the P-> Link = NULL; 
	Rear = P; 
	the while (N -) 
	{ 
		Scanf ( "% D% D", & C, & E); 
		the Attach (C, e, & Rear); // item into the current tail polynomial 
	} 
	T = P; 
	P = the P-> Link; 
	Free (T); // delete the first node generated temporary 
	return P; 
} 

void the Attach (C int, int E, to the Polynomial pRear *) 
{ 
	to the Polynomial P; 
	
	P = (to the Polynomial) the malloc (the sizeof (struct PolyNode)); 
	the P-> Coef = C; // the new node assignment 
	the P-> expon = E; 
	the P-> Link = NULL;
	(*pRear)->link = P;
	*pRear = P;	// 修改pRear的值 
}

int Compare(int a, int b)
{
	if(a > b)	return 1;
	else if(a < b)	return -1;
	else	return 0;
}

// 多项式相加
Polynomial Add(Polynomial P1, Polynomial P2)
{
	Polynomial P, Rear, t, t1, t2;
	t1 = P1; t2 = P2;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->link = NULL;
	Rear = P;
	while(t1 && t2)
	{
		switch(Compare(t1->expon, t2->expon))
		{
			case 1:
				Attach(t1->coef, t1->expon, &Rear);
				t1 = t1->link;
				break;
			case -1:
				Attach(t2->coef, t2->expon, &Rear);
				t2 = t2->link;
				break;
			case 0:
				if(t1->coef + t2->coef)	Attach(t1->coef + t2->coef, t1->expon, &Rear);
				t1 = t1->link;
				t2 = t2->link;
				break;
		}
	}
	for(; t1; t1 = t1->link)	Attach(t1->coef, t1->expon, &Rear);
	for(; t2; t2 = t2->link)	Attach(t2->coef, t2->expon, &Rear);
	Rear->link = NULL;
	t = P;
	P = P->link;
	free(t);
	return P;
} 

// 多项式相乘
Polynomial Mult(Polynomial P1, Polynomial P2)
{
	Polynomial P, Rear, t1, t2, t;
	int c, e;
	
	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;
	}
	t1 = t1->link;
	 
	while(t1)
	{
		t2 = P2; Rear = P;
		while(t2)
		{
			e = t1->expon + t2->expon;
			c = t1->coef * t2->coef;
			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); 
				}	
			}
			coefficient else // index is not equal to 
			{ 
				T = (to the Polynomial) the malloc (the 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; 
	} 
	T2 = P; 
	P = the P-> Link; 
	Free (T2); 
	
	return P; 
} 

// how polynomial output 
void PrintPoly (to the polynomial P) 
{ 
	int In Flag = 0; // adjust the output format with the auxiliary 
	
	IF (P!)	 
	{ 
		the printf ( "0 0 \ n-"); 
		return; 
	} 
	
	the while (P ) 
	{ 
		! IF (In Flag) In Flag =. 1; 
		the else the printf ( "");
		
		printf("%d %d", P->coef, P->expon);
		P = P->link;
	}
	printf("\n");
} 

  

Guess you like

Origin www.cnblogs.com/mjn1/p/11448325.html