首页 > 代码库 > 多项式相加运算

多项式相加运算



多项式相加运算,使用链表实现,代码仍需要改善,这里先初步做个记录


//实现多项式的表示及相加 by Denis
#include <stdio.h>
#include <malloc.h>
#define ture 1
#define false 0

typedef int ElemType;
typedef struct LNode{
	ElemType coef;		//系数
	int expn;           //指数
	struct LNode *next;
}polynomia;

polynomia *CreatPolyn(int nLength)  //m 表示多项式的项数
{
	int i;
	polynomia *Ln_Head,*Ln_tmp1,*Ln_tmp2;
	printf("Creat a polynomia that have %d term\nPlease input the coef and expn for every term\n",nLength);
	Ln_Head = (polynomia *)malloc(sizeof(polynomia));
	Ln_tmp1 = Ln_Head;   /*Save the Head pointor*/
	
	for(i=0;i<nLength;i++)
	{	
		Ln_tmp2 = (polynomia *)malloc(sizeof(polynomia));
		scanf("%d %d",&(Ln_tmp2->coef), &(Ln_tmp2->expn));//Input the data

		//Insert the data to the end odf list
		Ln_tmp1->next = Ln_tmp2;
		Ln_tmp1 = Ln_tmp2;
		Ln_tmp2->next = NULL;


	}
	return Ln_Head;
}

//Insert a term to polynomia
int ListInsert(polynomia *Head, ElemType coef, int expn)
{
	polynomia *P, *Tail;
	P = Head->next;
	//Get the tail pointor of the list
	while(P->next)
	{
		P = P->next;
	}
	Tail = P;
	P = (polynomia *)malloc(sizeof(polynomia));
	P->coef = coef;
	P->expn = expn;
	Tail->next = P;
	P->next = NULL;
	//ListLength++;
	return 0;
}

//Get the how many term in the polynomia
int ListLength(polynomia *Head)
{
	int length=0;
	polynomia *P;
	P = Head->next;
	while(P)
	{
		length++;
		P = P->next;
	}

	return length;
}

//Get the number i data ,and return the pointor
//i must be >0,and <= List's length
polynomia *GetElem(polynomia *Head, int i)
{
	int j=0;
	polynomia *P, *tmp;
	P = Head->next;	
	while(P && j!=i)
	{
		tmp = P;//Save the P
		P = P->next;
		j++;
	}
	return tmp;
}

polynomia *LocateElem(polynomia *Head, ElemType E)
{
	polynomia *P;
	P = Head->next;
	while(P)
	{
		if(P->expn== E)//E is in the list
			return P;
		else
			P = P->next;
	}
	return false;//E is not in the list
}

//Union the polynomia_1 and polynomia_2
polynomia *AddPolyn(polynomia *Head1, polynomia *Head2)
{
	
	int L1_len = ListLength(Head1);
	int L2_len = ListLength(Head2);//计算多项式的项数
	ElemType E1=Head1->next->expn;
	ElemType E2=Head2->next->expn;
	int i=1, j=1; 
	polynomia *Tmp_1, *Tmp_2;

	Tmp_1 = Head1->next;
	Tmp_2 = Head2->next;


	printf("Union list_1 and list_2\n");
	for(i=1; i<=L2_len; i++)
	{
		Tmp_2 = GetElem(Head2,i);//获取L2中的第i个元素,并获取该元素地址,打算查看是否该元素在L1中出现
		if(!LocateElem(Head1,Tmp_2->expn))//如果元素不在L1中,则插入该元素
				ListInsert(Head1,Tmp_2->coef, Tmp_2->expn);
		else//否则,存在相同指数项的元素,元素系数相加
		{
			Tmp_1 = LocateElem(Head1,Tmp_2->expn);
			Tmp_1->coef = Tmp_2->coef + Tmp_1->coef;
			//if(Tmp_1->coef == 0)//系数为0的时候,略过该项
				//	Tmp_1 = Tmp_1->next;
					
		}
			
	}

	return Head1; 

}


void PrintPolyn(polynomia *Head)
{
	polynomia *P= Head->next;
	int i=0;
	printf("The polynomia is:\n F(X) = ");
	while(P)
	{
		if(P->coef == 0)//系数为0时,跳过该项
		{
			P=P->next;
		}

		if(i == 0)
		{		
			//如果系数为 1或者-1,则不显示1
			if(P->coef == 1)
			{
				printf("X^%d", P->expn);
				i++;
			}
			if(P->coef == -1)
			{
				printf("-X^%d", P->expn);
				i++;
			}
			else
			{
				printf("%d*X^%d",P->coef, P->expn);
				i++;
			}
		}
		else
		{
			if(P->coef > 0 && P->coef!=1)
				printf(" + %d*X^%d",P->coef, P->expn);
			if(P->coef < 0 && P->coef!= -1)
				printf("%d*X^%d",P->coef, P->expn);
			//如果系数为 1或者-1,则不显示1		
			if(P->coef == 1)	
				printf("+X^%d", P->expn);				
			if(P->coef == -1)
				printf("-X^%d", P->expn);
		}
		

		P = P->next;
	}
	printf("\n");
	
}


void main()
{
	polynomia *Polyn_1, *Polyn_2, *Polyn_3;
	Polyn_1 = CreatPolyn(2);
	PrintPolyn(Polyn_1);
	//ListInsert(Polyn_1,10,10);
//	PrintPolyn(Polyn_1);

	Polyn_2 = CreatPolyn(2);
	Polyn_3 = AddPolyn(Polyn_1, Polyn_2);
	PrintPolyn(Polyn_3);

	getch();

}

多项式相加运算