📄 多项式.cpp
字号:
#define NULL 0
#include <stdio.h>
#include <math.h>
#include <iostream.h>
struct sq{
double coef;
int expn;
struct sq *next;
}*head[11];
int i,j,k;
//去除零项
int purge(int m){
struct sq *z,*p,*q;
if (head[m]==NULL)
return(0);
p=head[m];
q=p;
while(p!=NULL){
if (p->coef==0){
z=p;
if (p==head[m]){
head[m]=p->next;
p=p->next;
}
else{
q->next=p->next;
p=p->next;
}
delete z;
}
else {
q=p;
p=p->next;
}
}
return (0);
}
//创建
void creat(int m,int n){
struct sq *z,*p,*q;
if(n==0)
return ;
z=new struct sq ;
cin>>z->coef>>z->expn;
z->next=NULL;
head[m]=z;
for(i=1;i<=n-1;i++){
z=new struct sq ;
cin>>z->coef>>z->expn;
p=head[m];
if(z->expn<p->expn){
head[m]=z;
z->next=p;
continue;
}
for (;(p->next!=NULL)&&(z->expn>p->expn);){
q=p;
p=p->next;
}
if(z->expn==p->expn){
p->coef=p->coef+z->coef;
continue;
}
if(p->next==NULL)
if(z->expn>p->expn){
p->next=z;
z->next=NULL;}
else {
q->next=z;
z->next=p;
}
else{
q->next=z;
z->next=p;
}
}
purge (m);
}
//显示
void print(int m){
struct sq *p;
p=head[m];
if (head[m]==NULL){
cout<<"多项式不存在"<<endl;
}
else{
for(;p!=NULL;){
if(p->coef>0&&p->expn>0){
cout<<p->coef<<"x^"<<p->expn<<"+";
p=p->next;
}
else if(p->coef<0&&p->expn>0){
cout<<"\b";
cout<<p->coef<<"x^"<<p->expn<<"+";
p=p->next;
}
else if(p->coef<0&&p->expn<0){
cout<<"\b";
cout<<p->coef<<"x^"<<"("<<p->expn<<")"<<"+";
p=p->next;
}
else {
cout<<p->coef<<"x^"<<"("<<p->expn<<")"<<"+";
p=p->next;
}
}
cout<<"\b "<<endl;
}
}
//复制
int cpy(int m, int mc){
struct sq *z,*p,*q,*y;
if(head[m]==NULL){
head[mc]=NULL;
return 0;
}
z=new struct sq ;
head[mc]=z;
p=head[m];
q=head[mc];
for(;p!=NULL;){
z->coef=p->coef;
z->expn=p->expn;
z->next=p->next;
p=p->next;
y=z;
z=new struct sq ;
y->next=z;
}
y->next=NULL;
delete z;
return 0;
}
//求和(就地运算)
void add(int m1,int m2){ //存在m1处
struct sq *z,*y,*p,*q;
z=p=head[m1];
q=head[m2];
for(;p!=NULL&&q!=NULL;){
if(p->expn<q->expn){
z=p;
p=p->next;
}
else if(p->expn>q->expn){
if(p!=head[m1])
z->next=q;
else
head[m1]=q;
y=q->next;
z=q;
q->next=p;
q=y;
}
else if(p->expn==q->expn){
p->coef+=q->coef;
z=p;
y=q;
if(z==head[m1])
head[m1]=p;
p=p->next;
q=q->next;
delete y;
}
}
if(p==NULL)
if(p!=head[m1])
z->next=q;
else
head[m1]=q;
head[m2]=NULL;
purge(m1);
}
//求和(异地运算)
void add(int m1,int m2,int m3){ //此处用了函数重载
struct sq *z,*y,*p,*q;
z=new struct sq;
head[m3]=z;
p=head[m1];
q=head[m2];
y=NULL;
for(;p!=NULL&&q!=NULL;){
if(p->expn<q->expn){
z->coef=p->coef;
z->expn=p->expn;
p=p->next;
}
else if(p->expn>q->expn){
z->coef=q->coef;
z->expn=q->expn;
q=q->next;
}
else if(p->expn==q->expn){
z->coef=p->coef+q->coef;
z->expn=p->expn;
p=p->next;
q=q->next;
}
z->next=new struct sq;
y=z;
z=z->next;
}
if(p==NULL){
for(;q!=NULL;){
z->coef=q->coef;
z->expn=q->expn;
z->next=new struct sq;
y=z;
z=z->next;
q=q->next;
}
if(q==NULL){
if(head[m3]==y)
y->next=NULL;
else
head[m3]=NULL;
delete z;
}
}
else{
for(;p!=NULL&&q==NULL;){
z->coef=p->coef;
z->expn=p->expn;
z->next=new struct sq;
y=z;
z=z->next;
p=p->next;
}
delete z;
y->next=NULL;
}
purge(m3);
}
//求值
double ans(int m, double x){
double answer=0.0;
struct sq *p;
if(head[m]==0)
return(0);
p=head[m];
for(;p!=NULL;){
answer+=p->coef*pow(x,p->expn);
p=p->next;
}
return answer;
}
//求差(就地运算) //存在m1处
void dev(int m1,int m2){
struct sq *z,*y,*p,*q;
z=p=head[m1];
q=head[m2];
for(;p!=NULL&&q!=NULL;){
if(p->expn<q->expn){
z=p;
p=p->next;
}
else if(p->expn>q->expn){
if(p!=head[m1])
z->next=q;
else
head[m1]=q;
y=q->next;
q->coef=-q->coef;
q->next=p;
z=q;
q=y;
}
else if(p->expn==q->expn){
p->coef-=q->coef;
z=p;
y=q;
if(z==head[m1])
head[m1]=p;
p=p->next;
q=q->next;
delete y;
}
}
if(p==NULL){
if(z==head[m1])
head[m1]=q;
else
z->next=q;
}
for(;q!=NULL;){
q->coef=-q->coef;
q=q->next;
}
head[m2]=NULL;
purge(m1);
}
//求差(异地运算) //此处用了函数重载
void dev(int m1,int m2,int m3){
struct sq *z,*y,*p,*q;
z=new struct sq;
head[m3]=z;
p=head[m1];
q=head[m2];
for(;p!=NULL&&q!=NULL;){
if(p->expn<q->expn){
z->coef=p->coef;
z->expn=p->expn;
p=p->next;
}
else if(p->expn>q->expn){
z->coef=-q->coef;
z->expn=q->expn;
q=q->next;
}
else if(p->expn==q->expn){
z->coef=p->coef-q->coef;
z->expn=p->expn;
p=p->next;
q=q->next;
}
z->next=new struct sq;
y=z;
z=z->next;
}
if(p==NULL){
for(;q!=NULL;){
z->coef=-q->coef;
z->expn=q->expn;
z->next=new struct sq;
y=z;
z=z->next;
q=q->next;
}
if(q==NULL){
if(head[m3]==y)
y->next=NULL;
else
head[m3]=NULL;
delete z;
}
}
else{
for(;p!=NULL&&q==NULL;){
z->coef=p->coef;
z->expn=p->expn;
z->next=new struct sq;
y=z;
z=z->next;
p=p->next;
}
delete z;
y->next=NULL;
}
purge(m3);
}
//N阶导数(就地运算)
int derivative(int m1, int N){ //存在m1处
struct sq *p;
int i;
if(head[m1]==NULL){
cout<<"多项是不存在"<<endl;
return(0);
}
p=head[m1];
for(;p!=NULL;){
for(i=0;i<N;i++)
p->coef*=(p->expn-i);
p->expn-=N;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -