❶ C语言程序题:编写程序实现多项式计算

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#defineEPS1E-6

typedefstructitem{
doublecoefficient;
intpower;
structitem*next;
}*POLYNOMIAL,*pItem;

POLYNOMIALCreate(){//创建多项式
pItemhead,p;
doublecoe;
intpwr;
head=p=(pItem)malloc(sizeof(item));
while(1){
printf("系数幂次(00结束):");
scanf("%lf%d",&coe,&pwr);
if(fabs(coe)<=EPS&&!pwr)break;
p->next=(pItem)malloc(sizeof(item));
p->next->coefficient=coe;
p->next->power=pwr;
p=p->next;
}
p->next=NULL;
returnhead;
}

voidSort(POLYNOMIALhead){//按幂次降排序
pItempt,q,p=head;
while(p->next){
q=p->next;
while(q->next){
if(p->next->power<q->next->power){
pt=p->next;
p->next=q->next;
q->next=p->next->next;
p->next->next=pt;
}
elseq=q->next;
}
p=p->next;
}
}

voidShow(POLYNOMIALhead){//显示多项式
POLYNOMIALp=head->next;
intflag=1;
if(p==NULL)return;
while(p){
if(flag){
if(fabs(p->coefficient)>=EPS){
if(p->power==0)printf("%.2lf",p->coefficient);
elseif(p->power==1){
if(p->coefficient==1.0)printf("x");
elseif(p->coefficient==-1.0)printf("-x");
elseprintf("%.2lfx",p->coefficient);
}
elseif(p->coefficient==1.0)printf("x^%d",p->power);
elseif(p->coefficient==-1.0)printf("-x^%d",p->power);
elseprintf("%.2lfx^%d",p->coefficient,p->power);
flag=0;
}
}
elseif(p->coefficient>0.0&&fabs(p->coefficient)>=EPS){
if(p->power==0)printf("+%.2lf",p->coefficient);
elseif(p->power==1){
if(p->coefficient==1.0)printf("+x");
elseprintf("+%.2lfx",p->coefficient);
}
elseif(p->coefficient==1.0)printf("+x^%d",p->power);
elseprintf("+%.2lfx^%d",p->coefficient,p->power);
}
elseif(p->coefficient<0.0&&fabs(p->coefficient)>=EPS){
if(p->power==0)printf("-%.2lf",-p->coefficient);
elseif(p->power==1){
if(p->coefficient==-1.0)printf("-x");
elseprintf("-%.2lfx",-p->coefficient);
}
elseif(p->coefficient==-1.0)printf("-x^%d",p->power);
elseprintf("-%.2lfx^%d",-p->coefficient,p->power);
}
p=p->next;
}
printf(" ");
}

doublePower(doublex,intn){
doublevalue=1.0;
inti;
for(i=0;i<n;++i)value*=x;
returnvalue;
}

doubleValue(POLYNOMIALhead,doublex){//多项式求值
POLYNOMIALp;
doublevalue=0.0;
for(p=head->next;p;p=p->next)
value+=p->coefficient*Power(x,p->power);
returnvalue;
}

POLYNOMIALCopy(POLYNOMIALA){
POLYNOMIALhead,t,p;
head=t=(pItem)malloc(sizeof(item));
for(p=A->next;p;p=p->next){
t->next=(pItem)malloc(sizeof(item));
t->next->coefficient=p->coefficient;
t->next->power=p->power;
t=t->next;
}
t->next=NULL;
returnhead;
}

POLYNOMIALAdditive(POLYNOMIALA,POLYNOMIALB){//多项式相加
POLYNOMIALhead,p,q,t;
head=Copy(A);
for(p=B;p->next;p=p->next){
q=head;
while(q->next){
if(p->next->power==q->next->power){
q->next->coefficient+=p->next->coefficient;
if(fabs(q->next->coefficient)<=EPS){
t=q->next;
q->next=t->next;
free(t);
}
break;
}
q=q->next;
}
if(q->next==NULL){
q->next=(pItem)malloc(sizeof(item));
q->next->coefficient=p->next->coefficient;
q->next->power=p->next->power;
q->next->next=NULL;
}
}
Sort(head);
returnhead;
}

POLYNOMIALSubtract(POLYNOMIALA,POLYNOMIALB){//多项式相减
POLYNOMIALhead,p,q,t;
head=Copy(A);
for(p=B;p->next;p=p->next){
q=head;
while(q->next){
if(p->next->power==q->next->power){
q->next->coefficient-=p->next->coefficient;
if(fabs(q->next->coefficient)<=EPS){
t=q->next;
q->next=t->next;
free(t);
}
break;
}
q=q->next;
}
if(q->next==NULL){
q->next=(pItem)malloc(sizeof(item));
q->next->coefficient=-p->next->coefficient;
q->next->power=p->next->power;
q->next->next=NULL;
}
}
Sort(head);
returnhead;
}

POLYNOMIALMultiplication(POLYNOMIALA,POLYNOMIALB){//多项式相乘
POLYNOMIALhead,t,p,q;
head=t=(pItem)malloc(sizeof(item));
for(p=A->next;p;p=p->next){//完成相乘过程
for(q=B->next;q;q=q->next){
t->next=(pItem)malloc(sizeof(item));
t->next->coefficient=p->coefficient*q->coefficient;
t->next->power=p->power+q->power;
t=t->next;
}
}
t->next=NULL;
Sort(head);//排序
p=head;
while(p->next){//合并同类项
q=p->next;
while(q->next){
if(p->next->power==q->next->power){
p->next->coefficient+=q->next->coefficient;
t=q->next;
q->next=t->next;
free(t);
}
elseq=q->next;
}
p=p->next;
}
returnhead;
}

voidFreeMemory(POLYNOMIALhead){
POLYNOMIALq,p=head;
while(p){
q=p;
p=q->next;
free(q);
}
}

intmain(){
printf("创建多项式A: ");
POLYNOMIALA=Create();
Sort(A);
printf("A(x)=");Show(A);
printf("创建多项式B: ");
POLYNOMIALB=Create();
Sort(B);
printf("B(x)=");Show(B);
POLYNOMIALC=Additive(A,B);
printf("C(x)=");Show(C);
POLYNOMIALD=Subtract(A,B);
printf("D(x)=");Show(D);
POLYNOMIALE=Multiplication(A,B);
printf("E(x)=");Show(E);
printf("A(%.2lf)=%.4lf ",2.0,Value(A,2.0));
printf("B(%.2lf)=%.4lf ",2.0,Value(B,2.0));
printf("C(%.2lf)=%.4lf ",2.0,Value(C,2.0));
printf("D(%.2lf)=%.4lf ",2.0,Value(D,2.0));
printf("E(%.2lf)=%.4lf ",2.0,Value(E,2.0));
FreeMemory(A);
FreeMemory(B);
FreeMemory(C);
FreeMemory(D);
FreeMemory(E);
return0;
}