#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
typedef struct Polynomial
{
float coef; //系数;
int expn; //指数;
struct Polynomial *next;
}Polynomial,*Poly;
Poly derivative(Poly p)
{
Poly pf=p->next;
Poly pg,ph,pn;
pg=pn=(Poly)malloc(sizeof(Polynomial));
ph=(Poly)malloc(sizeof(Polynomial));
ph->next=pg;
pg->next=NULL;
while(pf!=NULL)
{
if(pf->expn==0)
continue;
pg->expn=pf->expn-1;
pg->coef=pf->expn*pf->coef;
pn=(Poly)malloc(sizeof(Polynomial));
pn->next=NULL;
pg->next=ph;
pg=pn;
}
return ph;
}
float dragin(Poly p,float x) //带入求值;
{
Poly pd=p->next;
float sum=0;
while(pd!=NULL)
{
sum+=pd->coef*pow(x,pd->expn);
pd=pd->next;
}
return sum;
}
void SortPoly(Poly p) //多项式的排序;
{
Poly q=p->next;
Poly qa;
Polynomial t;
for(q;q!=NULL;q=q->next)
{
for(qa=q;qa->next!=NULL;qa=qa->next)
{
if(qa->expn>qa->next->expn)
{
t.coef=qa->coef;
qa->coef=qa->next->coef;
qa->next->coef=t.coef;
t.expn=qa->expn;
qa->expn=qa->next->expn;
qa->next->expn=t.expn;
}
}
}
}
Poly CreatPoly() //多项式的建立;
{
Polynomial *head,*rear,*s;
int c,e;
head=(Polynomial *)malloc(sizeof(Polynomial));
rear=head;
scanf("%d,%d",&c,&e);
while(c!=0)
{
s=(Polynomial *)malloc(sizeof(Polynomial));
s->coef=c;
s->expn=e;
rear->next=s;
rear=s;
scanf("%d,%d",&c,&e);
}
rear->next=NULL;
return(head);
}
void PrintPoly(Poly p) //输出多项式;
{
Poly q;
int flag=1;
SortPoly(p);
q=p->next;
if(!q)
{
putchar('0');
printf("\n");
return;
}
while(q)
{
if(q->coef>0&&flag!=1) putchar('+');
if(q->coef!=1&&q->coef!=-1)
{
printf("%g",q->coef);
if(q->expn==1) printf("X");
else if(q->expn) printf("X^%d",q->expn);
}
else
{
if(q->coef==1)
{
if(!q->expn) putchar('1');
else if(q->expn==1) putchar('X');
else printf("X^%d",q->expn);
}
if(q->coef==-1)
{
if(!q->expn) printf("-1");
else if(q->expn==1) printf("-X");
else printf("-X^%d",q->expn);
}
}
q=q->next;
flag++;
}
printf("\n");
}
Poly AddPoly(Poly pa,Poly pb) //多项式的加法;
{
Poly qa=pa->next;
Poly qb=pb->next;
Poly headc,pc,qc;
pc=(Poly)malloc(sizeof(Polynomial));
pc->next=NULL;
headc=pc;
while(qa!=NULL&&qb!=NULL)
{
qc=(Poly)malloc(sizeof(Polynomial));
if(qa->expn<qb->expn)
{
qc->coef=qa->coef;
qc->expn=qa->expn;
qa=qa->next;
}
else if(qa->expn==qb->expn)
{
qc->coef=qa->coef;
qc->expn=qa->expn;
qa=qa->next;
qb=qb->next;
}
else
{
qc->coef=qb->coef;
qc->expn=qb->expn;
qb=qb->next;
}
if(qc->coef!=0)
{
qc->next=pc->next;
pc->next=qc;
pc=qc;
}
else free(qc);
}
while(qa!=NULL)
{
qc=(Poly)malloc(sizeof(Polynomial));
qc->coef=qa->coef;
qc->expn=qa->expn;
qa=qa->next;
qc->next=pc->next;
pc->next=qc;
pc=qc;
}
while(qb!=NULL)
{
qc=(Poly)malloc(sizeof(Polynomial));
qc->coef=qb->coef;
qc->expn=qb->expn;
qb=qb->next;
qc->next=pc->next;
pc->next=qc;
pc=qc;
}
return headc;
}
Poly SubtractPoly(Poly pa,Poly pb) //多项式的减法;
{
Poly h=pb;
Poly p=pb->next;
Poly pd;
while(p)
{
p->coef*=-1;
p=p->next;
}
pd=AddPoly(pa,h);
for(p=h->next;p;p=p->next)
p->coef*=-1;
return pd;
}
Poly multipcation(Poly pa,Poly pb) //多项式的乘法;
{
Poly qa=pa->next;
Poly qb=pb->next;
Poly headc,pc,qc;
pc=(Poly)malloc(sizeof(Polynomial));
pc->next=NULL;
headc=pc;
while(qa!=NULL)
{
while(qb!=NULL)
{
qc=(Poly)malloc(sizeof(Polynomial));
qc->coef=(qa->coef)*(qb->coef);
qc->expn=(qa->expn)+(qb->expn);
qc->next=pc->next;
pc->next=qc;
pc=qc;
qb=qb->next;
}
qb=pb->next;
qa=qa->next;
}
return headc;
}
void textPoly()
{
Poly pa,pb;
float x;
printf("输入多项式A(形如“系数,指数”):\n");
pa=CreatPoly();
printf("A(x)=");
PrintPoly(pa);
printf("\n");
printf("输入多项式B:\n");
pb=CreatPoly();
printf("B(x)=");
PrintPoly(pb);
printf("\n");
printf("A+B=");
PrintPoly(AddPoly(pa,pb));
printf("\n");
printf("A-B=");
PrintPoly(SubtractPoly(pa,pb));
printf("\n");
printf("A*B=");
PrintPoly(multipcation(pa,pb));
printf("\n");
printf("带入求值,输入X:");
scanf("%g",&x);
printf("A(x) = %g\n",dragin(pa,x));
printf("A`(x)=");
PrintPoly(derivative(pa));
}
main()
{
textPoly();
}
程序员日记一号:多项式运算(加减乘)
最新推荐文章于 2022-11-12 10:18:18 发布