数据结构——多项式的加减乘法以及合并同类项的算法

测试数据(这里只测试了一组数据,小伙伴们可以自行测试其他数据喔)

2 2 3 3 4 4 0 0
1 2 2 2 0 0

1 2 1 1 0 0 
1 2 1 3 0 0

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

2 2 4 4 3 3 6 6 5 5 0 0 
8 7 4 5 6 1 1 3 3 5 0 0

1 2 3 4 5 6 7 8 0 0 
2 2 4 4 6 6 8 8 0 0

1 2 3 4 5 6 7 8 0 0
1 1 3 3 5 5 7 7 0 0

这里插入链表使用的是尾插法

加减法(其实减法和加法的思路差不多,都是判断指数相同与否,然后对系数实现加减法)

乘法主要是遍历两遍链表(因为一个多项式中每一项都要乘一遍另一个多项式的每一项,因此需要遍历两遍),系数相乘,指数相加

合并同类项(主要采用了迭代的思想,用head保存p1节点的下一节点,这样在p2遍历完后还可以回到p1的下一加点处)

打印多项式(只需要判断第一项的正负就可以啦,后面的都可以直接遍历)

​
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
struct node {
	int exp;//指数
	int coe;//系数
	struct node *next;
} ;
typedef struct node *List;

struct node *create();
struct node *insert(List L);
struct node *add(List L1,List L2,List L3);
struct node *sub(List L1,List L2,List L3);
struct node *mul(List L1,List L2,List L3);
void print(List L);
List merge(List L);

int main() {
	List L1=create();
	List L2=create();
	List L3;
	printf("请输入多项式1(输入以0 0为结尾):\n");
	insert(L1);
	printf("多项式1为:\n");
	print(merge(L1));
	printf("\n");

	printf("请输入多项式2(输入以0 0为结尾):\n");
	insert(L2);
	printf("多项式2为:\n");
	print(merge(L2));
	printf("\n");

	printf("相加后的多项式为:\n");
	print(add(L1,L2,L3));
	printf("\n");

	printf("相减后的多项式为:\n");
	print(sub(L1,L2,L3));
	printf("\n");

	printf("相乘后的多项式为:\n");
	print(merge(mul(L1,L2,L3)));
	printf("\n");
}

//初始化链表
struct node *create() {
	List head=(List)malloc(sizeof(struct node));
	if(head!=NULL) {
		head->coe=0;
		head->exp=0;
		head->next=NULL;
	}
	return head;
}
//插入链表
struct node *insert(List L) {
	List s,t;
	int e,c;
	int count=0;
	t=L;
	scanf("%d %d",&c,&e);
	while(e!=0&&c!=0) {
		if(c<0){
			count++;
		}
		s=(List)malloc(sizeof(struct node));
		s->coe=c;
		s->exp=e;
		s->next=NULL;
		t->next=s;
		t=s;
		scanf("%d %d",&c,&e);
	}
	printf("系数为负的个数为:%d\n",count);
	return L;
}
//加法
struct node *add(List L1,List L2,List L3) {
	List a=L1;
	List b=L2;
	List t;
	struct node *s;
	L3=(List)malloc(sizeof(struct node));
	L3->next=NULL;
	t=L3;
	//同时输入了L1,L2链表
	while(a->next!=NULL&&b->next!=NULL) {
		if(a->next->exp==b->next->exp) {
			s=(List)malloc(sizeof(struct node));//创建一个新节点存放L1与L2相加后的数据
			s->coe=a->next->coe+b->next->coe;
			s->exp=a->next->exp;
			s->next=NULL;
			t->next=s;
			t=s;
			a=a->next;
			b=b->next;
		} else if(a->next->exp < b->next->exp) {
			s=(List)malloc(sizeof(struct node));
			s->coe=a->next->coe;
			s->exp=a->next->exp;
			s->next=NULL;

			t->next=s;
			t=s;
			a=a->next;
		} else  {
			s=(List)malloc(sizeof(struct node));
			s->coe=b->next->coe;
			s->exp=b->next->exp;
			s->next=NULL;
			t->next=s;
			t=s;
			b=b->next;
		}
	}
	//只输入了L1链表
	while(a->next!=NULL) {
		s=(List)malloc(sizeof(struct node));
		s->coe=a->next->coe;
		s->exp=a->next->exp;
		s->next=NULL;
		t->next=s;
		t=s;
		a=a->next;
	}
	//只输入了L2链表
	while(b->next!=NULL) {
		s=(List)malloc(sizeof(struct node));
		s->coe=b->next->coe;
		s->exp=b->next->exp;
		s->next=NULL;
		t->next=s;
		t=s;
		b=b->next;
	}

	return L3;
}
//减法
struct node *sub(List L1,List L2,List L3) {
	List a=L1;
	List b=L2;
	List t;
	struct node *s;
	L3=(List)malloc(sizeof(struct node));
	L3->next=NULL;
	t=L3;
	//同时输入了L1,L2链表
	while(a->next!=NULL&&b->next!=NULL) {
		if(a->next->exp==b->next->exp) {
			s=(List)malloc(sizeof(struct node));//创建一个新节点存放L1与L2相加后的数据
			s->coe=a->next->coe-b->next->coe;
			s->exp=a->next->exp;
			s->next=NULL;
			if(s->coe==0) {
				free(s);
			}
			t->next=s;
			t=s;
			a=a->next;
			b=b->next;

		} else if(a->next->exp < b->next->exp) {
			s=(List)malloc(sizeof(struct node));
			s->coe=a->next->coe;
			s->exp=a->next->exp;
			s->next=NULL;
			t->next=s;
			t=s;
			a=a->next;
		} else  if(a->next->exp > b->next->exp) {
			s=(List)malloc(sizeof(struct node));
			s->coe=-b->next->coe;
			s->exp=b->next->exp;
			s->next=NULL;
			t->next=s;
			t=s;
			b=b->next;
		}
	}
	//只输入了L1链表
	while(a->next!=NULL) {
		s=(List)malloc(sizeof(struct node));
		s->coe=a->next->coe;
		s->exp=a->next->exp;
		s->next=NULL;
		t->next=s;
		t=s;
		a=a->next;
	}
	//只输入了L2链表
	while(b->next!=NULL) {
		s=(List)malloc(sizeof(struct node));
		s->coe=b->next->coe;
		s->exp=b->next->exp;
		s->next=NULL;
		t->next=s;
		t=s;
		b=b->next;
	}

	return L3;
}
//乘法
struct node *mul(List L1,List L2,List L3) {
	List a,b,t,head;
	a=L1->next;
	b=L2->next;
	head=L2->next;
	struct node *s;
	L3=(List)malloc(sizeof(struct node));
	L3->next=NULL;
	t=L3;
	while(a!=NULL) {
		while(b!=NULL) {
			s=(List)malloc(sizeof(struct node));//辅助节点,暂时存储数据 
			s->coe=a->coe*b->coe;
			s->exp=a->exp+b->exp;
			s->next=NULL;
			t->next=s;
			t=s;
			b=b->next;
		}
		a=a->next;
		b=head;//b链表返回第一个多项式 
	}
	return L3;
}
//多项式合并同类项
List merge(List L) {
	List p1,p2,temp,head;
	p1=L->next;
	
	while(p1!=NULL) {
		p2=p1->next;
		head=p1->next;
		while(p2!=NULL) {
			if(p1->exp==p2->exp) {
				p1->coe=p1->coe+p2->coe;
				temp=p2;
				p1->next=p2->next;
				free(temp);
				p2=p1->next;
			} else {
				p2=p2->next;
			}
		}
		p2=head;//让p2回到 p1节点的下一个节点处 
		p1=p1->next;
	}
	return L;
}
//打印多项式
void print(List L) {
	List p;
	p=L;
	if(p->next->coe>0) {
		printf("%dx^%d",p->next->coe,p->next->exp);
		p = p->next;
	}
	while ( p->next!=NULL) {
		if(p->next->coe>0) {
			printf("+%dx^%d",p->next->coe,p->next->exp);
			p = p->next;
		} else if(p->next->coe<0) {
			printf("%dx^%d",p->next->coe,p->next->exp);
			p = p->next;
		}
	}
}



​

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值