一元多项式计算(链表)

//今天不太想写的,但是也不想断掉(已经断了

/*案例1:一元多项式的计算 P(x)=10+5x-4x^2+3x^3+2x^4
[分析]下标表示指数,数据域贮存系数*/
#include<stdio.h>
#include<iostream.h>//流
#include<malloc.h>
#include<cstdlib>//包含exit头文件
#include<math.h>
#include<string.h>
#define MAXSIZE 100
#define LENGTH 5
#define X 3//x的取值,方便更换
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct 
{
int *elem;
int length;
}SqList;
Status InitList(SqList &L)
{
L.elem=(int *)malloc(MAXSIZE*sizeof(int));
if(!L.elem)exit(OVERFLOW);
L.length=0;
return OK;
}
Status CalcuList(SqList &L)
{
int sum=0;
int x=1;
for(int i=0;i<L.length;i++)
{
sum=sum+L.elem[i]*x;
x=X*x;
}
return sum;
}
void RecordList(SqList &L)
{
for(int i=0;i<LENGTH;i++)
{
cin>>L.elem[i];
L.length++;
}
}
void ListShow(SqList L)
{
int i=0;
while(i<L.length)
{
cout<<L.elem[i]<<endl;
}
}
void main()
{
SqList *L;
L=(SqList *)malloc(sizeof(SqList));
InitList(*L);
cout<<"录入系数:"<<endl;
RecordList(*L);
cout<<"计算结果:"<<endl;
cout<<CalcuList(*L)<<endl;
}
一元多项式求和链表是一种用于表示和计算一元多项式的数据结构。一元多项式是指只有一个变量(如x)的多项式。通过使用链表结构,可以方便地表示和操作这些多项式。 ### 一元多项式链表表示 一元多项式可以用链表来表示,每个节点包含一个系数和一个指数。例如,多项式 `3x^4 + 2x^2 + 5` 可以表示为链表: ``` (3, 4) -> (2, 2) -> (5, 0) ``` 其中,每个节点包含一个系数和一个指数,指数按降序排列。 ### 实现步骤 1. **定义节点结构**:每个节点包含系数、指数和指向下一个节点的指针。 2. **创建链表**:根据多项式的系数和指数创建链表。 3. **多项式求和**:遍历两个多项式链表,将相同指数的系数相加,不同指数的节点直接添加到结果链表中。 4. **显示结果**:将结果链表转换回多项式的字符串表示形式。 ### 代码示例 ```python class Node: def __init__(self, coeff, exp): self.coeff = coeff self.exp = exp self.next = None class Polynomial: def __init__(self): self.head = None def addTerm(self, coeff, exp): newNode = Node(coeff, exp) if self.head is None or self.head.exp < exp: newNode.next = self.head self.head = newNode else: current = self.head while current.next is not None and current.next.exp > exp: current = current.next if current.next is not None and current.next.exp == exp: current.next.coeff += coeff else: newNode.next = current.next current.next = newNode def add(self, poly1, poly2): p1 = poly1.head p2 = poly2.head while p1 is not None and p2 is not None: if p1.exp > p2.exp: self.addTerm(p1.coeff, p1.exp) p1 = p1.next elif p1.exp < p2.exp: self.addTerm(p2.coeff, p2.exp) p2 = p2.next else: self.addTerm(p1.coeff + p2.coeff, p1.exp) p1 = p1.next p2 = p2.next while p1 is not None: self.addTerm(p1.coeff, p1.exp) p1 = p1.next while p2 is not None: self.addTerm(p2.coeff, p2.exp) p2 = p2.next def display(self): current = self.head result = "" while current is not None: if current.coeff != 0: if current.exp == 0: result += f"{current.coeff}" elif current.exp == 1: result += f"{current.coeff}x + " else: result += f"{current.coeff}x^{current.exp} + " current = current.next return result[:-3] if result else "0" # 示例使用 poly1 = Polynomial() poly1.addTerm(3, 4) poly1.addTerm(2, 2) poly1.addTerm(5, 0) poly2 = Polynomial() poly2.addTerm(1, 3) poly2.addTerm(4, 2) poly2.addTerm(6, 0) poly3 = Polynomial() poly3.add(poly1, poly2) print(poly3.display()) # 输出: 3x^4 + x^3 + 6x^2 + 11 ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值