//一元多项式
#include
#include
using namespace std;
#define MAX 1000
typedef struct
{
int a[MAX];
int len;
}ST;
void ADD(ST A,ST B,ST *C)
{
int la=A.len,lb=B.len,i;
C->len=la>lb? la:lb;
for(i=0 ; i
{
C->a[i]=A.a[i]+B.a[i];
}
while(i
{
C->a[i]=A.a[i];
i++;
}
while(i
{
C->a[i]=B.a[i];
i++;
}
}
void SUB(ST A,ST B,ST *C)
{
int la=A.len,lb=B.len,i;
C->len=la>lb? la:lb;
for(i=0 ; i
{
C->a[i]=A.a[i]-B.a[i];
}
while(i
{
C->a[i]=A.a[i];
i++;
}
while(i
{
C->a[i]=0-B.a[i];
i++;
}
}
void MUL(ST A,ST B,ST *C)
{
int i,j;
for(i=0 ; i
C->a[i]=0;
for(i=0;i
{
for(j=0;j
{
C->a[i+j]+=A.a[i]*B.a[j];
}
}
C->len=A.len+B.len;
}
void DER(ST A,ST *C)
{
int len=A.len,i=1;
while(i
{
C->a[i-1]=A.a[i]*i;
i++;
}
C->len=A.len-1;
}
void Get(ST *A)
{
int i,coef,ex,maxe=0;
char ch;
printf("请输入系数和指数,当输入指数为-1时结束:\n");
for(i=0;i
A->a[i]=0;
scanf("%d %d",&coef,&ex);
while(ex>=0)
{
if(ex>maxe)
maxe=ex;
if(A->a[ex]!=0)
{
printf("项存在,是否更新?Y/N\n");
cin>>ch;
if(ch=='Y'||ch=='y')
{
A->a[ex]=coef;
printf("已更新,请继续输入\n");}
else
printf("请继续输入\n");;
}
else A->a[ex]=coef;
scanf("%d %d",&coef,&ex);
}
A->len=maxe;
return ;
}
void Print(ST C)
{
int i;
printf(" %dx^%d ",C.a[C.len],C.len);
for(i=C.len-1;i>=1;i--)
{
if(C.a[i]==0) ;
else if(C.a[i]==1)
printf(" + x^%d ",i);
else if(C.a[i]==-1)
printf(" - x^%d ",i);
else
{
if(C.a[i]>0)
printf("+ %dx^%d ",C.a[i],i);
else
printf("- %-dx^%d ",-C.a[i],i);
}
}
if(C.a[0]==0) ;
else if(C.a[0]>0)
printf(" + %d",C.a[0]);
else
printf(" - %d",-C.a[0]);
printf("\n");
}
int main()
{
ST A,B,C;
printf("请输入A(x):\n");
Get(&A);
printf("请输入B(x):\n");
Get(&B);
printf("A(x)=");
Print(A);
DER(A,&C);
printf("A(x)'=");
Print(C);
cout
printf("B(x)=");
Print(B);
DER(B,&C);
printf("B(x)'=");
Print(C);
cout
printf("A(x)+B(x)=");
ADD(A,B,&C);
Print(C);
DER(C,&C);
printf("(A(x)+B(x))'=");
Print(C);
cout
printf("A(x)-B(x)=");
SUB(A,B,&C);
Print(C);
DER(C,&C);
printf("(A(x)-B(x))'=");
Print(C);
cout
printf("A(x)*B(x)=");
MUL(A,B,&C);
Print(C);
DER(C,&C);
printf("(A(x)*B(x))'=");
Print(C);
return 0;
}
//一元多项式
#include
#include
using namespace std;
#define MAX 1000
typedef struct
{
int a[MAX];
int len;
}ST;
void ADD(ST A,ST B,ST *C)
{
int la=A.len,lb=B.len,i;
C->len=la>lb? la:lb;
for(i=0 ; i
{
C->a[i]=A.a[i]+B.a[i];
}
while(i
{
C->a[i]=A.a[i];
i++;
}
while(i
{
C->a[i]=B.a[i];
i++;
}
}
void SUB(ST A,ST B,ST *C)
{
int la=A.len,lb=B.len,i;
C->len=la>lb? la:lb;
for(i=0 ; i
{
C->a[i]=A.a[i]-B.a[i];
}
while(i
{
C->a[i]=A.a[i];
i++;
}
while(i
{
C->a[i]=0-B.a[i];
i++;
}
}
void MUL(ST A,ST B,ST *C)
{
int i,j;
for(i=0 ; i
C->a[i]=0;
for(i=0;i
{
for(j=0;j
{
C->a[i+j]+=A.a[i]*B.a[j];
}
}
C->len=A.len+B.len;
}
void DER(ST A,ST *C)
{
int len=A.len,i=1;
while(i
{
C->a[i-1]=A.a[i]*i;
i++;
}
C->len=A.len-1;
}
void Get(ST *A)
{
int i,coef,ex,maxe=0;
char ch;
printf("请输入系数和指数,当输入指数为-1时结束:\n");
for(i=0;i
A->a[i]=0;
scanf("%d %d",&coef,&ex);
while(ex>=0)
{
if(ex>maxe)
maxe=ex;
if(A->a[ex]!=0)
{
printf("项存在,是否更新?Y/N\n");
cin>>ch;
if(ch=='Y'||ch=='y')
{
A->a[ex]=coef;
printf("已更新,请继续输入\n");}
else
printf("请继续输入\n");;
}
else A->a[ex]=coef;
scanf("%d %d",&coef,&ex);
}
A->len=maxe;
return ;
}
void Print(ST C)
{
int i;
printf(" %dx^%d ",C.a[C.len],C.len);
for(i=C.len-1;i>=1;i--)
{
if(C.a[i]==0) ;
else if(C.a[i]==1)
printf(" + x^%d ",i);
else if(C.a[i]==-1)
printf(" - x^%d ",i);
else
{
if(C.a[i]>0)
printf("+ %dx^%d ",C.a[i],i);
else
printf("- %-dx^%d ",-C.a[i],i);
}
}
if(C.a[0]==0) ;
else if(C.a[0]>0)
printf(" + %d",C.a[0]);
else
printf(" - %d",-C.a[0]);
printf("\n");
}
int main()
{
ST A,B,C;
printf("请输入A(x):\n");
Get(&A);
printf("请输入B(x):\n");
Get(&B);
printf("A(x)=");
Print(A);
DER(A,&C);
printf("A(x)'=");
Print(C);
cout
printf("B(x)=");
Print(B);
DER(B,&C);
printf("B(x)'=");
Print(C);
cout
printf("A(x)+B(x)=");
ADD(A,B,&C);
Print(C);
DER(C,&C);
printf("(A(x)+B(x))'=");
Print(C);
cout
printf("A(x)-B(x)=");
SUB(A,B,&C);
Print(C);
DER(C,&C);
printf("(A(x)-B(x))'=");
Print(C);
cout
printf("A(x)*B(x)=");
MUL(A,B,&C);
Print(C);
DER(C,&C);
printf("(A(x)*B(x))'=");
Print(C);
return 0;
}