#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct polyn{
float coef;
int expn;
struct polyn * next;
}polyn, *linkpolyn;
linkpolyn initlist( ){ //创建一个带头结点的链表
struct polyn * L;
L=(linkpolyn)malloc(sizeof(polyn));
if(!L) exit(OVERFLOW);
L->coef=0.0;L->expn=-1;
L->next=NULL;
return(L);
}
void destroylist(linkpolyn L){ //销毁链表
linkpolyn a,b;
a=b=L;
if(a!=NULL)
{a->next=a;
free(b);
b=a;
}
}
int listinsert(linkpolyn L,int i,linkpolyn e){ //将结点e插入第i个元素之后
linkpolyn p,t;
p=L;
if(i==0){
t=p->next;
e->next=t;
p->next=e;
}
else{
int j=0;
while(j<i){
p=p->next;
j++;
}
t=p->next;
if(t==NULL){
p->next=e;
e->next=NULL;
}
e->next=t;
p->next=e;
}
return(OK);
}
void printpolyn(linkpolyn L){ //以类数学形式打印多项式链表
linkpolyn p;
p=L->next;
if(p==NULL) printf("0");
else{
if(p->expn==0) printf("%g",p->coef);
else{
if(p->expn==1)printf("%gx",p->coef);
else if(p->coef==1) printf("x^%d",p->expn);
else if(p->coef==-1) printf("-x^%d",p->expn);
else printf("%gx^%d",p->coef,p->expn);
}
p=p->next;
while(p){
if(p->coef>0){
if(p->expn==0) printf("+%g",p->coef);
else if(p->expn==1)printf("+%gx",p->coef);
else if(p->coef==1) printf("+x^%d",p->expn);
else printf("+%gx^%d",p->coef,p->expn);
}
else{
if(p->expn==0) printf("%g",p->coef);
else if(p->expn==1)printf("%gx",p->coef);
else if(p->coef==-1) printf("-x^%d",p->expn);
else printf("%gx^%d",p->coef,p->expn);
}
p=p->next;
}
}
}
linkpolyn createpolyn(int m){ //创建多项式链表
linkpolyn L,e,p;
int i,j;
L=initlist();
for(j=0;j<m;++j){
e=(linkpolyn)malloc(sizeof(polyn));
printf("请输入第%d项系数和指数:",j+1);
scanf("%f%d",&e->coef,&e->expn);
e->next=NULL;
if(j==0) listinsert(L,0,e);
else{
p=L->next;
for(i=0;e->expn>p->expn;){
i++;
p=p->next;
if(p==NULL) break;
}
if(p!=NULL){
if(e->expn==p->expn){
p->coef=p->coef + e->coef;
free(e);
}
else listinsert(L,i,e);
}
else listinsert(L,i,e);
}
}
return(L);
}
linkpolyn addpolyn(linkpolyn L1,linkpolyn L2){ //加法
linkpolyn p1,p2,p,e;
int i=0;
p=initlist();
p1=L1->next;p2=L2->next;
while(p1&&p2){
if(p1->expn>p2->expn){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p2->coef;
e->expn=p2->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p2=p2->next;
}
else if(p1->expn==p2->expn){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p1->coef+p2->coef;
if(e->coef==0){
free(e);
p1=p1->next;
p2=p2->next;
}
else{
e->expn=p1->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p1=p1->next;p2=p2->next;
}
}
else{
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p1->coef;
e->expn=p1->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p1=p1->next;
}
}
if(p1==0){
while(p2){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p2->coef;
e->expn=p2->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p2=p2->next;
}
}
if(p2==0){
while(p1){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p1->coef;
e->expn=p1->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p1=p1->next;
}
}
return(p);
}
linkpolyn decpolyn(linkpolyn L1,linkpolyn L2){ //减法
linkpolyn p1,p2,p,e;
int i=0;
p=initlist();
p1=L1->next;p2=L2->next;
while(p1&&p2){
if(p1->expn>p2->expn){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p2->coef*(-1);
e->expn=p2->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p2=p2->next;
}
else if(p1->expn==p2->expn){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p1->coef-p2->coef;
if(e->coef==0){
free(e);
p1=p1->next;
p2=p2->next;
}
else{
e->expn=p1->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p1=p1->next;p2=p2->next;
}
}
else{
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p1->coef;
e->expn=p1->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p1=p1->next;
}
}
if(p1==0){
while(p2){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p2->coef*(-1);
e->expn=p2->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p2=p2->next;
}
}
if(p2==0){
while(p1){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=p1->coef;
e->expn=p1->expn;
e->next=NULL;
listinsert(p,i,e);
i++;p1=p1->next;
}
}
return(p);
}
linkpolyn multiplypolyn(linkpolyn L1,linkpolyn L2){ //乘法
linkpolyn p1,p2,e,n1,n2;
int i;
p1=initlist();
p2=initlist();
n1=L1->next;
n2=L2->next;
while(n1){
i=0;
while(n2){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=n1->coef * n2->coef;
e->expn=n1->expn + n2->expn;
e->next=NULL;
listinsert(p1,i,e);
i++;n2=n2->next;
}
p2=addpolyn(p1,p2);
p1->next=NULL;
n1=n1->next;
n2=L2->next;
}
return(p2);
}
linkpolyn derivationpolyn(linkpolyn L){ //多项式求导
linkpolyn p,l,e;
int i=0;
p=initlist();
l=L->next;
while(l){
if(l->expn==0){
l=l->next;
continue;
}
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=l->coef * l->expn;
e->expn=l->expn-1;
listinsert(p,i,e);
i++;
l=l->next;
}
return(p);
}
linkpolyn integratepolyn(linkpolyn L){ //多项式求积分
linkpolyn p,l,e;
int i=0;
p=initlist();
l=L->next;
while(l){
e=(linkpolyn)malloc(sizeof(polyn));
e->coef=l->coef/(l->expn+1);
e->expn=l->expn+1;
listinsert(p,i,e);
i++;
l=l->next;
}
return(p);
}
float calculatepolyn(linkpolyn L,float x){ //计算多项式在x处的值
float t=0;
linkpolyn p;
p=L->next;
while(p){
t=t + ((p->coef) * (pow(x,p->expn)));
p=p->next;
}
return(t);
}
int main( ){
linkpolyn L1, L2,addp,decp,mulp,derp1,derp2,inte1,inte2;
int i,m;
float x;
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
start:
system("cls");
printf("********************************************************************************\n");
printf("\t\t\t 一元稀疏多项式计算器\n\n");
printf("***************************************