/**copyright of Matter 2014/10/13**/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<windows.h>
//多项式中的一项的结构
typedef struct term{
double coef;//系数
int expn;//指数
struct term* next;
}PolyNode ,*pPolyNode;
/*****声明区域******/
pPolyNode CreatePoly();//创建一个一元多项式,按降序排列
pPolyNode Reverse(pPolyNode head);//多项式升序排列
pPolyNode multiply(pPolyNode A,pPolyNode B);//两个多项式相乘,并转换成升序排列
void Printpoly(pPolyNode head);//打印
pPolyNode DestroyPoly(pPolyNode head);//销毁一元多项式
int PolynLength(pPolyNode head);//遍历出多项式的项数
/*******************/
int main()
{
int val,temp1,temp2;
printf("****输入每项的系数和次数,同时输入0时表示结束******\n");
pPolyNode A,B,C;
A=CreatePoly();
printf("A(x)=");
Printpoly (A);
printf("\n");
printf("A(x)的项数为:");
temp1=PolynLength(A);
printf("\n");
B=CreatePoly();
printf("B(x)=");
Printpoly (B);
printf("\n");
printf("B(x)的项数为:");
temp2=PolynLength(B);
printf("\n");
C=multiply(A,B);
//C=CreatePoly();
printf("A(x)*B(X)的结果为:\n");
printf("C(x)=");
Printpoly (C);
printf("\n");
printf("请问是否销毁一元多项式A(x)?是请输入1,否请输入0:");
scanf("%d",&val);
if(val)
{
DestroyPoly(A);
printf("已经成功销毁!\n");
}
system("pause");
return 0;
}
//创建一个保存多项式的链表,返回指向头结点的指针。多项式按指数降序排列
pPolyNode CreatePoly()
{
PolyNode *p,*q,*s,*head=NULL;
double coef;
int expn;
//int i; //计数器
head=(pPolyNode)malloc(sizeof(PolyNode));//头结点
if(head==NULL)
{
printf("error!\n");
return NULL;
}
//初始化,头节点置空
head->coef =0.0;
head->expn =0;
head->next =NULL;
printf("请输入第1项的系数:");
scanf("%lf",&coef);
printf("请输入第1项的指数:");//这是起始输入的是第二个节点的值,也就是除头结点第一个节点的值
scanf("%d",&expn);
/*printf("请输入项数:");
scanf("%d",&i);*/
int m=2;
while( (long)coef !=0 && expn !=0) //当coef和expn同时等于0时,输入结束
{
s = (pPolyNode)malloc(sizeof(PolyNode));//构造一个新节点
s->coef = coef;
s->expn = expn;
q=head->next ;
p=head;
//算法,先使多项式降序排列
while(q && expn <q->expn )
{
p=q;
q=q->next;
}
if(q== NULL || expn > q->expn )
{
p->next =s;
s->next =q;
}
else
{
q->coef+=coef;
}
//下一个节点的值更新输入
printf("请输入第%d项的系数:",m);
scanf("%lf",&coef);
printf("请输入第%d项的指数:",m);
scanf("%d",&expn);
m++;
}
return head;
}
//将多项式逆置,按升幂排列
pPolyNode Reverse(pPolyNode head)
{
PolyNode *p,*q,*t;
p=NULL;
q=head->next;
while( q!=NULL )
{
t=q->next;
q->next =p;
p=q;
q=t;
}
head->next =p;
return head;
}
//两个多项式相乘,把结果传给C多项式
pPolyNode multiply(pPolyNode A,pPolyNode B)
{
PolyNode *pa,*pb,*pc,*u,*head;
int k ,maxExp;
double coef;
//设置C多项式的头结点
head=(pPolyNode)malloc( sizeof (PolyNode) );
if(head==NULL)
{
printf("error!\n");
return NULL;
}
head->coef=0.0;
head->expn =0;
head->next =NULL;
if(A->next !=NULL && B->next != NULL)
{
maxExp=(A->next) ->expn +(B->next )->expn ;
}
else
{
return head;
}
pc=head;
B=Reverse (B);
for(k=maxExp; k>=0; k--)
{
pa = A->next ;
while(pa != NULL && pa->expn >k)
pa=pa->next ;
pb = B->next ;
while( pb != NULL && pa != NULL && (pa->expn + pb->expn) < k )
pb=pb->next;
coef=0.0;
while(pa != NULL && pb != NULL )
{
if( (pa->expn +pb->expn )==k )
{
coef+=pa->coef * pb->coef;
pa=pa->next;
pb=pb->next;
}
else
{
if(( pa->expn + pb->expn ) > k )
{
pa=pa->next;
}
else
{
pb=pb->next;
}
}
}
if( coef != 0.0 )
{
u=(pPolyNode)malloc(sizeof(PolyNode));
u->coef =coef;
u->expn =k;
u->next =pc->next;
pc->next=u;
pc=u;
}
}
B=Reverse(B);
return head;
}
//打印
void Printpoly(pPolyNode head)
{
PolyNode *p=head->next;
while(p)
{
printf("%1.1f",p->coef);
if(p->expn)
printf("*x^%d",p->expn );
if(p->next && p->next->coef >0)
printf("+");
p=p->next;
}
}
//销毁多项式
pPolyNode DestroyPoly(pPolyNode head)
{
pPolyNode p=head;
pPolyNode q;
q=p->next;
while(q)
{
free(p);
p=NULL;
p=q;
q=q->next;
}
}
//输出项数长度
int PolynLength(pPolyNode head)
{ int length=0;
pPolyNode p=head->next;
while(p)
{
length++;
p=p->next;
}
printf("%d",length);
return length;
}
一元多项式的相乘
最新推荐文章于 2023-04-12 23:24:33 发布