要求:
1 输入并建立多项式
2 输出多项式,输出形式外为整数序列:c1,e1;c2,e2;.......cn,en;其中n为多项式的项数,ci和ei分别为第i项的系数和指数,序列按指数降序排列
3 多项式a和b相加,建立多项式a+b
4 多项式a和b相减,建立多项式a-b
代码如下:
#include<stdio.h>
#include<malloc.h>
#define MAX 20 //多项式最多项数
typedef struct //定义存放多项式的数组类型
{
float coef; //系数
int exp; //指数
}PolyArray[MAX];
typedef struct pnode //定义单链表结点类型
{
float coef; //系数
int exp; //指数
struct pnode *next;
}PolyNode;
void DispPoly(PolyNode *L) //输出多项式
{
PolyNode *p=L->next;
while (p!=NULL)
{
printf("%g,%d;",p->coef,p->exp);
p=p->next;
}
printf("\n");
}
void CreateListR(PolyNode * &L,PolyArray a,int n) //尾插入法建表
{
PolyNode *s,*r;int i;
L=(PolyNode *)malloc(sizeof(PolyNode)); //创建头结点
L->next=NULL;
// L->exp=n;
// printf("%d\n",L->exp);
r=L; //r始终指向终端结点,开始时指向头结点
for(i=0;i<n;i++)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //创建新结点
s->coef=a[i].coef;
s->exp=a[i].exp;
r->next=s; //将*s插入*r之后
r=s;
}
r->next=NULL; //将终端结点next域置为NULL
}
void Sort(PolyNode * &head) //按exp域的值递减排序
{
PolyNode *p=head->next,*q,*r;
if(p!=NULL) //当原单链表不为空时
{
r=p->next; //r保存*p结点后继结点的指针
p->next=NULL; //构造只含一个数据结点的有序表
p=r;
while(p!=NULL)
{
r=p->next; //r保存*p结点后继结点的指针
q=head;
while(q->next!=NULL && q->next->exp>p->exp)
q=q->next; //在有序表中找插入*p的前驱结点*q
p->next=q->next; //将*p插入到*q之后
q->next=p;
p=r;
}
}
}
void Add(PolyNode *ha,PolyNode *hb,PolyNode *&hc)//求两个有序表的并
{
PolyNode *pa=ha->next,*pb=hb->next,*s,*tc;
float c;
hc=(PolyNode *)malloc(sizeof(PolyNode)); //创建头结点
tc=hc;
while(pa!=NULL && pb!=NULL)
{
if(pa->exp>pb->exp)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
else if(pa->exp<pb->exp)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pb->exp;s->coef=pb->coef;
tc->next=s;tc=s;
pb=pb->next;
}
else //pa->exp=pb->exp时
{
c=pa->coef+pb->coef;
if(c!=0) //系数之和不为0时创建新结点
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->exp=pa->exp;s->coef=c;
tc->next=s;tc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL) //复制余下结点
pa=pb;
while(pa!=NULL)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
tc->next=NULL;
}
void Subs(PolyNode *ha,PolyNode *hb,PolyNode *&hc) //求两个有序表的差
{
PolyNode *pa=ha->next,*pb=hb->next,*s,*tc;
float c;
hc=(PolyNode *)malloc(sizeof(PolyNode)); //创建头结点
tc=hc;
while(pa!=NULL && pb!=NULL)
{
if(pa->exp>pb->exp)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
else if(pa->exp<pb->exp)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pb->exp;s->coef=-pb->coef; //如果前个多项式中的指数小于后个多项式指数加上负号
tc->next=s;tc=s;
pb=pb->next;
}
else //pa->exp=pb->exp时
{
c=pa->coef-pb->coef;
if(c!=0) //系数之差不为0时创建新结点
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->exp=pa->exp;s->coef=c;
tc->next=s;tc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL) //复制余下结点
pa=pb;
while(pa!=NULL)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
tc->next=NULL;
}
void main()
{
PolyNode *ha,*hb,*hc;
//PolyArray a={{1.2,0},{2.5,1},{3.2,3},{-2.5,5}};
//PolyArray b={{-1.2,0},{2.5,1},{3.2,3},{2.5,5},{5.4,10}};
int m=4,n=5;
PolyArray a,b;
for(int i=0;i<m;i++)
{
printf("请输入A多项式中第%d项的系数和指数",i);
scanf("%f%d",&a[i].coef,&a[i].exp);
}
for(int j=0;j<n;j++)
{
printf("请输入B多项式中第%d项的系数和指数",j);
scanf("%f%d",&b[j].coef,&b[j].exp);
}
CreateListR(ha,a,4);
CreateListR(hb,b,5);
printf("原来A:\n");DispPoly(ha);
printf("原来B:\n");DispPoly(hb);
Sort(ha);
Sort(hb);
printf("排序后A:\n");DispPoly(ha);
printf("排序后B:\n");DispPoly(hb);
Add(ha,hb,hc);
printf("相加后:\n");DispPoly(hc);
Subs(ha,hb,hc);
printf("相减后:\n");DispPoly(hc);
}
posted on 2006-10-04 16:18
matthew 阅读(4412)
评论(5) 编辑 收藏 所属分类:
数据结构与算法设计