用线性表,节点2个部分,一个系数,一个阶。实现就很简单了,最好用链表。
这样根据定义写出4则运算。
还可以再优化,比如用压缩写。
实现九自己做,不能全依赖别人吗
这个是我刚刚调试好的了
作者:张怡
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int datatype;
typedef struct LNode
{
float coef; /*多项式系数*/
int expn; /*多项式指数*/
struct LNode *next;
}LNode,*linklist;
/*---------创建带头结点的多项式链表--------*/
linklist creat()
{ linklist head,s,p,pre;
float coef;
int expn;
head=(linklist)malloc(sizeof(LNode)); /*表头结点*/
head->next=NULL;
printf("输入系数:");
scanf("%f",&coef);
printf("输入指数:");
scanf("%d",&expn);
while (coef!=0.0)
{ printf("当系数等于零的时候结束!");
s=(linklist)malloc(sizeof(LNode)); /*生成新结点*/
s->coef=coef;
s->expn=expn;
s->next=NULL;
pre=head; /*插入到有序的多项式链表中去*/
p=head->next;
while (p && p->expn <s->expn)
{ pre=p;
p=p->next;
}
s->next=p;
pre->next=s;
printf("读下一项:\n");
printf("输入系数:\n");
scanf("%f",&coef);
printf("\n");
printf("输入指数:");
scanf("%d",&expn);
}
return head;
}
/*-----------输出多项式链表-------------*/
void print(linklist head)
{ linklist p;
p=head->next;
while (p)
{
printf("%fX^%d+",p->coef,p->expn);
p=p->next;
}
}
/*-------------多项式相加----------------*/
linklist add(linklist pa,linklist pb)
{
linklist p,q,pre,r,head;
float x;
head=pa;
p=pa->next; //p,q 指向头接点的下一个接点,即多项式的第一个接点
q=pb->next;
pre=pa; //pre指向p的前驱
while((p!=NULL)&&(q!=NULL)) //处理多项式的相加的问题
if(p->expn<q->expn)
{
pre=p;
p=p->next;
}
else if (p->expn==q->expn)
{
x=p->coef+q->coef;
if(x!=0) //系数相加不为0的情况
{
p->coef=x;
pre=p;
p=p->next;
}
else //系数相加为0的情况
{
pre->next=p->next;
free(p);
p=pre->next;
}
r=q;
q=q->next;
free(r);
}
else
{
r=q->next;
q->next=p;
pre->next=q;
pre=q;
q=r;
}
if(q!=NULL)
pre->next=q;
free(pb);
return head;
}
/*----主程序------*/
void main()
{
linklist a,b,c;
printf("请输入第一个多项式:");
a=creat(); /*创建多项式链表a*/
printf("请输入第二个多项式:");
b=creat(); /*创建多项式链表b*/
cout<<"您输入的第一个多项式为:"<<endl;
print(a);
cout<<"您输入的第二个多项式为:"<<endl;
print(b);
cout<<endl;
cout<<"两多项式相加后为:"<<endl;
c=add(a,b); /* 计算多项式a+b */
print(c);
}
#include<stdio.h>
#include<malloc.h>
typedef struct{
float coef;
int expn;
}Elemtype;
typedef struct node{
Elemtype date;
struct node* next;
}node,*polynomial;
void Initpoly(polynomial &p)
{
p=(polynomial)malloc(sizeof(node));
p->date.expn=0;
p->date.coef=0.0;
p->next=NULL;
} //初始化链表
int cmp(Elemtype a1,Elemtype a2)
{
if(a1.expn>a2.expn)return 1;
if(a1.expn<a2.expn)return -1;
else return 0;
}//比较两个结点
void Insertpoly(polynomial p1,Elemtype e)
{
polynomial p=p1,pnew;
pnew=(polynomial)malloc(sizeof(node));
while(p->next!=NULL)
{
if(cmp(p->next->date,e)==-1) p=p->next;
else break;
}
pnew->date=e;
pnew->next=NULL;
if(cmp(p->date,e)==0)p->date.coef+=e.coef;
else
{
pnew->next=p->next;p->next=pnew;
}
}//按序插入结点
void Creatpoly(polynomial & poly)
{
static int i=1;
Elemtype e;
printf("请输入第%d个多项式的系数与指数 以 系数 指数的形式输入\n",i);
scanf("%f %d",&e.coef,&e.expn);
Initpoly(poly);
while(e.coef!=0||e.expn!=0)
{
Insertpoly(poly,e);
scanf("%f %d",&e.coef,&e.expn);
}
i++;
}//创造一个链表
void Appendpoly(polynomial &p1,polynomial &p2)
{
polynomial p=p1;
while(p->next!=NULL)
{p=p->next;}
p->next=p2;
}//两个链表相接
void Addpoly(polynomial p1,polynomial p2)
{
polynomial p=p1->next,h=p2->next;
while(p!=NULL&&h!=NULL)
{
switch(cmp(p->date,h->date))
{
case 1:
{
Insertpoly(p1,h->date);
h=h->next;
break;
}
case 0:
{
p->date.coef+=h->date.coef;
p=p->next;h=h->next;
break;
}
case -1:
{
Insertpoly(p,h->date);
h=h->next;
p=p->next;
break;
}
}
}
if(h!=NULL)Appendpoly(p1,h);
printf("两个多项式的和为:\n");
}//两个多项式相加
void Destroypoly(polynomial p)
{
while(p!=NULL)
{
polynomial p1=p;
p=p->next;
free(p1);
}
}//释放存储空间
void Printpoly(polynomial p)
{
polynomial p1=p->next;
printf("(%f %d)",p1->date.coef,p1->date.expn);
p1=p1->next;
while(p1!=NULL)
{
printf("+(%f %d)",p1->date.coef,p1->date.expn);
p1=p1->next;
}
printf("\n");
}//打印多项式
void main()
{
polynomial p1;
Creatpoly(p1);
Printpoly(p1);
polynomial p2;
Creatpoly(p2);
Printpoly(p2);
Addpoly(p1,p2);
Printpoly(p1);
Destroypoly(p2);
Destroypoly(p1);
}
#include<stdio.h>
#include<malloc.h>
typedef struct{
float coef;
int expn;
}Elemtype;
typedef struct node{
Elemtype date;
struct node* next;
}node,*polynomial;
void Initpoly(polynomial &p)
{
p=(polynomial)malloc(sizeof(node));
p->date.expn=0;
p->date.coef=0.0;
p->next=NULL;
} //初始化链表
int cmp(Elemtype a1,Elemtype a2)
{
if(a1.expn>a2.expn)return 1;
if(a1.expn<a2.expn)return -1;
else return 0;
}//比较两个结点
void Insertpoly(polynomial p1,Elemtype e)
{
polynomial p=p1,pnew;
pnew=(polynomial)malloc(sizeof(node));
while(p->next!=NULL)
{
if(cmp(p->next->date,e)==-1) p=p->next;
else break;
}
pnew->date=e;
pnew->next=NULL;
if(cmp(p->date,e)==0)p->date.coef+=e.coef;
else
{
pnew->next=p->next;p->next=pnew;
}
}//按序插入结点
void Creatpoly(polynomial & poly)
{
static int i=1;
Elemtype e;
printf("请输入第%d个多项式的系数与指数 以 系数 指数的形式输入\n",i);
scanf("%f %d",&e.coef,&e.expn);
Initpoly(poly);
while(e.coef!=0||e.expn!=0)
{
Insertpoly(poly,e);
scanf("%f %d",&e.coef,&e.expn);
}
i++;
}//创造一个链表
void Appendpoly(polynomial &p1,polynomial &p2)
{
polynomial p=p1;
while(p->next!=NULL)
{p=p->next;}
p->next=p2;
}//两个链表相接
void Addpoly(polynomial p1,polynomial p2)
{
polynomial p=p1->next,h=p2->next;
while(p!=NULL&&h!=NULL)
{
switch(cmp(p->date,h->date))
{
case 1:
{
Insertpoly(p1,h->date);
h=h->next;
break;
}
case 0:
{
p->date.coef+=h->date.coef;
p=p->next;h=h->next;
break;
}
case -1:
{
Insertpoly(p,h->date);
h=h->next;
p=p->next;
break;
}
}
}
if(h!=NULL)Appendpoly(p1,h);
printf("两个多项式的和为:\n");
}//两个多项式相加
void Destroypoly(polynomial p)
{
while(p!=NULL)
{
polynomial p1=p;
p=p->next;
free(p1);
}
}//释放存储空间
void Printpoly(polynomial p)
{
polynomial p1=p->next;
printf("(%f %d)",p1->date.coef,p1->date.expn);
p1=p1->next;
while(p1!=NULL)
{
printf("+(%f %d)",p1->date.coef,p1->date.expn);
p1=p1->next;
}
printf("\n");
}//打印多项式
void main()
{
polynomial p1;
Creatpoly(p1);
Printpoly(p1);
polynomial p2;
Creatpoly(p2);
Printpoly(p2);
Addpoly(p1,p2);
Printpoly(p1);
Destroypoly(p2);
Destroypoly(p1);
}