多项式相加运算



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


//实现多项式的表示及相加 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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一元多项式相加、相减运算可以使用上面的链表实现。假设有两个多项式 $p_1$ 和 $p_2$,它们的单项式按照指数从大到小排列,可以按照以下方式实现相加、相减运算: ```python def add(p1, p2): result = Polynomial() current_p1 = p1.head.next current_p2 = p2.head.next while current_p1 is not None and current_p2 is not None: if current_p1.exp == current_p2.exp: result.insert(current_p1.coef + current_p2.coef, current_p1.exp) current_p1 = current_p1.next current_p2 = current_p2.next elif current_p1.exp > current_p2.exp: result.insert(current_p1.coef, current_p1.exp) current_p1 = current_p1.next else: result.insert(current_p2.coef, current_p2.exp) current_p2 = current_p2.next while current_p1 is not None: result.insert(current_p1.coef, current_p1.exp) current_p1 = current_p1.next while current_p2 is not None: result.insert(current_p2.coef, current_p2.exp) current_p2 = current_p2.next return result def sub(p1, p2): result = Polynomial() current_p1 = p1.head.next current_p2 = p2.head.next while current_p1 is not None and current_p2 is not None: if current_p1.exp == current_p2.exp: result.insert(current_p1.coef - current_p2.coef, current_p1.exp) current_p1 = current_p1.next current_p2 = current_p2.next elif current_p1.exp > current_p2.exp: result.insert(current_p1.coef, current_p1.exp) current_p1 = current_p1.next else: result.insert(-current_p2.coef, current_p2.exp) current_p2 = current_p2.next while current_p1 is not None: result.insert(current_p1.coef, current_p1.exp) current_p1 = current_p1.next while current_p2 is not None: result.insert(-current_p2.coef, current_p2.exp) current_p2 = current_p2.next return result ``` 以上代码中,`add` 函数和 `sub` 函数分别实现了多项式的加法和减法,返回一个新的多项式。在循环中,比较两个多项式当前单项式的指数大小,如果相同则将系数相加(或相减),否则插入指数较大的单项式。最后,如果有一个多项式已经遍历完,则将另一个多项式的剩余单项式全部插入到结果多项式中。 使用示例: ```python p1 = Polynomial() # 2x^3+3x^2-4x+1 p1.insert(2, 3) p1.insert(3, 2) p1.insert(-4, 1) p1.insert(1, 0) p2 = Polynomial() # -x^2+2x+1 p2.insert(-1, 2) p2.insert(2, 1) p2.insert(1, 0) p3 = add(p1, p2) # 2x^3+2x^2-2x+2 print(p3) # 2x^3+2x^2-2x+2 p4 = sub(p1, p2) # 2x^3+4x^2-6x print(p4) # 2x^3+4x^2-6x ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值