两个一元多项式相加

两个一元多项式相加需要考虑三种情况:指数相同、多项式一指数大于多项式二、多项式一小于多项式二,其中指数相同又可分成指数相加不为0和相加为0两种情况。
创建单链表用来存储一元多项式,主要思想是p2指数与p1指数作比较,p1>p2,删除p2连在p1前面;p1<p2,p1后移;p1=p2,删除p2更改p1的。(p1多项式一工作指针,p2多项式二工作指针)

结构体定义

typedef struct TagLnode{
	int index;//指数域
	int coef;//系数域
	struct TagLnode *next;
}Lnode,*LinkList;

(1)指数相同

		if (p1->index==p2->index){//指数相同

			int e=p1->coef+p2->coef;//记录系数之和

			temp=p2;//暂存p2的地址
			L2->next=p2->next;//删除p2结点
			p2=L2->next;//指针后移
			delete temp;//释放temp

			if (e==0){//系数为0
				
				temp=p1;//暂存p1结点
				p1front->next=p1->next;//删除p1结点
				p1=p1front->next;//指针后移
				delete temp;//释放temp
			}
			else{//系数不为0
				p1->coef=e;
			}
		}

(2)多项式一的指数大于多项式二的指数

else if (p1->index>p2->index){//p1指数大于p2指数

			L2->next=p2->next;//删除p2结点
			p2->next=p1;//p2接在p1前面
			p1front->next=p2;//p1前驱接上p2
			p1front=p2;//指针后移
			p2=L2->next;//p2指向新的L2地址
		}

(3)多项式一的指数小于多项式二的指数

else if (p1->index<p2->index){//p1指数小于p2指数
			//L1链表指针后移
			p1=p1->next;
			p1front=p1front->next;
		}

(4)多项式二仍有剩余,可以确定多项式二的剩余项均大于多项式一的项,直接连在多项式一后面。

if (p2!=NULL){//链2中仍有项,全连在链1的后面
		p1front->next=p2;
		L2->next=NULL;
	}

源代码

#include <iostream>
using namespace std;
#include <stdlib.h>

typedef struct TagLnode{
	int index;//指数域
	int coef;//系数域
	struct TagLnode *next;
}Lnode,*LinkList;

void InitList(LinkList &L){
	L=new Lnode;
	L->next=NULL;
}

void RearCreateList(LinkList &L,int n){
	LinkList q,r;
	r=L;

	for (int i=1;i<=n;i++){
		InitList(q);

		cout<<"请输入第"<<i<<"个结点的系数和指数";
		cin>>q->coef>>q->index;

		r->next=q;
		r=q;
	}
}

void PrintList(LinkList &L){
	LinkList p;
	p=L->next;

	while(p!=NULL){
		cout<<p->coef<<"x^"<<p->index;
		if (p->next!=NULL){
			cout<<'+';
		}
		p=p->next;
	}
}

void AdditionDoubleList(LinkList &L1,LinkList &L2){

	LinkList p1,p2,p1front,temp;//p1是L1的工作指针,p2是L2的工作指针,p1front是p1的前驱,temp暂存需释放的结点

	p1front=L1;
	p1=L1->next;
	p2=L2->next;

	while (p1!=NULL&&p2!=NULL){//两条链都不为空
		if (p1->index==p2->index){//指数相同

			int e=p1->coef+p2->coef;//记录系数之和

			temp=p2;//暂存p2的地址
			L2->next=p2->next;//删除p2结点
			p2=L2->next;//指针后移
			delete temp;//释放temp

			if (e==0){//系数为0
				
				temp=p1;//暂存p1结点
				p1front->next=p1->next;//删除p1结点
				p1=p1front->next;//指针后移
				delete temp;//释放temp
			}
			else{//系数不为0
				p1->coef=e;
			}
		}
		else if (p1->index>p2->index){//p1指数大于p2指数

			L2->next=p2->next;//删除p2结点
			p2->next=p1;//p2接在p1前面
			p1front->next=p2;//p1前驱接上p2
			p1front=p2;//指针后移
			p2=L2->next;//p2指向新的L2地址
		}
		else if (p1->index<p2->index){//p1指数小于p2指数
			//L1链表指针后移
			p1=p1->next;
			p1front=p1front->next;
		}
	}
	if (p2!=NULL){//链2中仍有项,全连在链1的后面
		p1front->next=p2;
		L2->next=NULL;
	}
}

void main(){
	LinkList L1,L2;
	int n1,n2;
	InitList(L1);
	InitList(L2);

	cout<<"请输入L1的长度:";cin>>n1;
	RearCreateList(L1,n1);
	cout<<"请输入L2的长度:";cin>>n2;
	RearCreateList(L2,n2);
	
	cout<<"**********************************************";cout<<endl;
	cout<<"初始L1:";
	PrintList(L1);
	cout<<endl;
	cout<<"初始L2:";
	PrintList(L2);
	cout<<endl;
	cout<<"**********************************************";cout<<endl;

	AdditionDoubleList(L1,L2);
	PrintList(L1);

	system("pause");
}

在这里插入图片描述

好的,这是一个关于多项式相加的编程问题。以下是一个可能的实现: ```python class Node: def __init__(self, coef, exp, next=None): self.coef = coef self.exp = exp self.next = next class Polynomial: def __init__(self): self.head = None def add_term(self, coef, exp): if coef == 0: return if self.head is None: self.head = Node(coef, exp) return if exp > self.head.exp: self.head = Node(coef, exp, self.head) return if exp == self.head.exp: self.head.coef += coef if self.head.coef == 0: self.head = self.head.next return curr = self.head while curr.next is not None and curr.next.exp > exp: curr = curr.next if curr.next is not None and curr.next.exp == exp: curr.next.coef += coef if curr.next.coef == 0: curr.next = curr.next.next return curr.next = Node(coef, exp, curr.next) def __add__(self, other): result = Polynomial() curr1, curr2 = self.head, other.head while curr1 is not None and curr2 is not None: if curr1.exp > curr2.exp: result.add_term(curr1.coef, curr1.exp) curr1 = curr1.next elif curr1.exp < curr2.exp: result.add_term(curr2.coef, curr2.exp) curr2 = curr2.next else: result.add_term(curr1.coef + curr2.coef, curr1.exp) curr1, curr2 = curr1.next, curr2.next while curr1 is not None: result.add_term(curr1.coef, curr1.exp) curr1 = curr1.next while curr2 is not None: result.add_term(curr2.coef, curr2.exp) curr2 = curr2.next return result def __str__(self): terms = [] curr = self.head while curr is not None: if curr.exp == 0: terms.append(str(curr.coef)) elif curr.exp == 1: terms.append(f"{curr.coef}x") else: terms.append(f"{curr.coef}x^{curr.exp}") curr = curr.next if not terms: return "0" return " + ".join(terms) p1 = Polynomial() p1.add_term(2, 3) p1.add_term(3, 2) p1.add_term(4, 1) p1.add_term(5, 0) p2 = Polynomial() p2.add_term(3, 4) p2.add_term(2, 2) p2.add_term(1, 1) p2.add_term(6, 0) p3 = p1 + p2 print(p1) print(p2) print(p3) ``` 这个实现使用了单链表来存储多项式的每一项,其中每个节点包含了系数和指数。`add_term` 方法用于向多项式中添加一项,它会按照指数从大到小的顺序插入节点。`__add__` 方法用于实现多项式相加,它会遍历两个多项式的节点,按照指数从大到小的顺序将相同指数的项相加,并将结果存储在一个新的多项式中。`__str__` 方法用于将多项式转换为字符串,它会遍历多项式的节点,将每一项转换为字符串后拼接起来。 在上面的代码中,我们创建了两个多项式 `p1` 和 `p2`,分别为 $2x^3 + 3x^2 + 4x + 5$ 和 $3x^4 + 2x^2 + x + 6$。然后我们将它们相加得到了一个新的多项式 `p3`,它为 $3x^4 + 2x^3 + 5x^2 + 5x + 11$。最后我们分别输出了 `p1`、`p2` 和 `p3` 的字符串表示。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值