注:本文使用C语言实现
定义一个单项式的结构体及其创建:
typedef struct node{
int xi; //系数
int index; //指数
struct node*pre; //前驱单项式
struct node* next; //后继单项式
}Node,*pNode;
//创建单项式
pNode createNode(){
pNode s=(pNode)malloc(sizeof(Node));
if(s){
s->next=NULL;
s->pre=NULL;
}
return s; //返回创建的单项式的地址
}
一元多项式的建立及输出
建立
//建立多项式(指数由小到大建立)
int setFormula(int n,pNode head){ //传入单项式的个数n,及多项式的头指针head
if(!head){
printf("未初始化");
return 0;
}
char ch;
for(int i=0;i<n;i++){
pNode temp=(pNode)malloc(sizeof(Node));
if(temp){
scanf("(%d,%d)",&temp->xi,&temp->index); //输入(1,2)即为单项式X^2
while((ch=getchar())!='\n'&&ch!=EOF); //处理输入流中的多余字符
if(temp->xi==0) { //系数为0,直接跳过
free(tmp);
continue;
}
temp->next=NULL;
temp->pre=NULL;
pNode cur=head->next;
if(!cur) { //若该多项式为空则直接插入
head->next=temp;
temp->pre=head;
continue;
}
while(cur->next&&temp->index>cur->index){ //寻找该单项式在多项式中的正确位置
cur=cur->next;
}
if(cur->index==temp->index){ //若指数相等则合并
cur->xi+=temp->xi;
}
else if((cur->index)>(temp->index)){ //若新建立单项式的指数小于当前指数则插入
temp->next=cur;
temp->pre=cur->pre;
temp->pre->next=temp;
cur->pre=temp;
}
else if(!cur->next){ //若遍历至最后一个单项式且temp->index>cur->index,则插入至多项式末端
cur->next=temp;
temp->pre=cur;
}
}
}
}
输出
void Print(pNode head){
if(!head) {
printf("0");
return ;
}
pNode p=head->next;
if(!p) printf("0"); //若多项式为空则输出0
while(p){
if(p->xi==0) { //系数为0,则跳过
p=p->next;
continue;
}
if(p->xi!=1)
printf("%d",p->xi);
if(p->index==1){ //系数为1,指数为1,则输出“X”
printf("X");
}
else if(p->index==0){ //指数为0
if(p->xi==1) //系数为1,则输出“1”
printf("1");
}
else printf("X^%d",p->index);
p=p->next;
if(p&&p->xi>0) printf("+"); //若为最后一个则不输出“X”
}
}
加法
//该方法会改变h1所指多项式
pNode add(pNode h1,pNode h2){
if(!h1) return h2;
if(!h2) return h1;
pNode p1=h1->next;
pNode p2=h2->next;
pNode subP=h1; //辅助指针来记录当前所指节点的上一个节点
while(p1&&p2){
if(p1->index>p2->index){ //若p2->index<p1->index,则将p2插入p1
pNode temp=p2;
p2=p2->next;
temp->next=subP->next;
subP->next=temp;
subP=subP->next;
}
else if(p1->index==p2->index){ //指数相等则合并
p1->xi+=p2->xi;
if(p1->xi==0){ //若和为0,则跳过该结点
subP->next=p1->next;
p1=p1->next;
p2=p2->next;
}
else{
subP=p1;
p1=p1->next;
p2=p2->next;}
}
else{
subP=p1; //若p2->index>p1->index,则将p2插入p1
p1=p1->next;
}
}
while(p2){ //将p2剩余节点插入至p1
subP->next=p2;
p2=p2->next;
subP=subP->next;
}
return h1;
}
减法
实现思路基本与加法相同
//该方法会破坏h1所指多项式内容
pNode subs(pNode h1,pNode h2){
pNode p1=h1->next;
pNode p2=h2->next;
pNode subP=h1;
while(p1&&p2){
if(p1->index>p2->index){
pNode temp=p2;
p2=p2->next;
temp->next=subP->next;
subP->next=temp;
temp->xi=-temp->xi;
subP=subP->next;
}
else if(p1->index==p2->index){
p1->xi-=p2->xi;
if(p1->xi==0){
subP->next=p1->next;
}
else{
subP=p1;
}
p1=p1->next;
p2=p2->next;
}
else{
subP=p1;
p1=p1->next;
}
}
while(p2){
p2->xi=-p2->xi; //将p2剩余单项式插入p1时,需将p2系数置为其相反数
subP->next=p2;
p2=p2->next;
subP=subP->next;
}
return h1;
}
乘法
void addData(pNode head,pNode temp){ //将当前乘积项temp加入至结果多项式head中
pNode cur=head->next;
if(!cur) {
head->next=temp;
temp->pre=head;
return ;
}
while(cur->next&&temp->index>cur->index){
cur=cur->next;
}
if(cur->index==temp->index){
cur->xi+=temp->xi;
free(temp);
if(cur->xi==0) {
if(cur->next){
cur->next->pre=cur->pre;
cur->pre->next=cur->next;
}else{
cur->pre->next=cur->next;
}
free(cur);
}
}
else if((cur->index)>(temp->index)){
temp->next=cur;
temp->pre=cur->pre;
temp->pre->next=temp;
cur->pre=temp;
}
else if(!cur->next){
cur->next=temp;
temp->pre=cur;
}
}
//相乘处理
pNode multiply(pNode head1,pNode head2){
if(!head1->next||!head2->next) return NULL;
pNode p1=head1->next;
pNode p2=head2->next;
pNode h=createNode(); //创建结果多项式h
pNode r1=h;
while(p1){
if(p1->xi==0) continue; //系数0则跳过
while(p2){
if(p2->xi==0) continue;
pNode tmp=createNode();
tmp->xi=p1->xi*p2->xi;
tmp->index=p1->index+p2->index;
addData(h,tmp); //将乘积结果tmp并入结果多项式h
p2=p2->next;
}
p1=p1->next;
p2=head2->next;
}
return h;
}
求值
//求值的完整代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct node{
int xi;
int index;
struct node* next;
}Node,*pNode;
pNode createNode(){
pNode s=(pNode)malloc(sizeof(Node));
if(s){
s->next=NULL;
}
return s;
}
void setFormula(pNode head){
int n=0;
char ch;
pNode p=head;
scanf("%d",&n);
while((ch=getchar())!='\n'&&ch!=EOF);
for(int i=0;i<n;i++){
pNode tmp=(pNode)malloc(sizeof(Node));
scanf("(%d,%d)",&tmp->xi,&tmp->index);
if(tmp->xi==0) {
free(tmp);
continue;
}
tmp->next=p->next;
p->next=tmp;
p=p->next;
}
while((ch=getchar())!='\n'&&ch!=EOF);
}
//计算多项式结果
int answer(pNode head,int x){
pNode p=head;
int sum=0;
while(p){
sum+=p->xi*((int)pow(x,p->index));
p=p->next;
}
return sum;
}
int main(){
int X_val=0;
pNode head=createNode();
setFormula(head);
scanf("%d",&X_val);
int res=answer(head,X_val);
printf("%d",res);
}
求导
void TakeDerivation(pNode head){
pNode p=head->next;
while(p){
p->xi=p->xi*p->index;
p->index-=1;
p=p->next;
}
}