求大神解决关于多项式问题
#include<stdlib.h>#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1
#define INFEASIBLE -1
#define OVERFLOW -2
#define NULL 0
#define LESS -1
#define EQUAL 0
#define MORE 1
typedef int Status;
typedef struct
{
float coef;//多项式的系数
int expn;//多项式的指数
}Element;
typedef struct LNode
{
Element element;
struct LNode *Next,*Prev;
}LNode,*LPNode;
typedef struct Header
{
int length;//一元多项式的项
LPNode header,tail;
}*LHeader;
Status cmp(Element e1,Element e2)
{
if(e1.expn < e2.expn ) return LESS;
if(e1.expn ==e2.expn ) return EQUAL;
return MORE;
}
Status InitPolyn(LHeader &Lh)
{//初始化多项式,这里只是申表一个多项式的头。
Lh=(LHeader)malloc(sizeof(Header));//申请一个节点。
if(!Lh) return OVERFLOW;
Lh->length =0; //链表的初始长度为0
Lh->header =Lh->tail =NULL; //初始时,节点的直接后继元素为空
return OK;
}//InitPolyn
Status DeletePolynItem(LHeader &Lh,LPNode lp)
{
if(lp==Lh->header)
{
Lh->header =lp->Next ;
lp->Next ->Prev =NULL;
}
else if(lp==Lh->tail)
{
lp->Prev->Next =NULL;
Lh->tail =lp->Prev ;
}
else
{
lp->Prev->Next =lp->Next ;
lp->Next ->Prev =lp->Prev ;
}
free(lp);
Lh->length --;
return OK;
}//DeletePolynItem
Status DestroyPolyn(LHeader &Lh)
{
LPNode p=Lh->tail ;
while(p!=Lh->header )
{
p=p->Prev ;
free(p->Next );
}
free(Lh->header );
Lh->header =Lh->tail =NULL;
Lh->length =0;
return OK;
}
Status InsertElement(LHeader &Lh,Element e)
{//插入一元多项式的一个项
LPNode s=(LPNode)malloc(sizeof(LNode));
s->element =e;
s->Next =s->Prev =NULL;
if(Lh->header==NULL)//如果多项式为空多项式,则元素就成为线性表中的唯一的表头
{
Lh->header =s;
Lh->tail =s;
s->Prev =NULL;
s->Next =NULL;
return OK;
}
LPNode p=Lh->header ;//从表头开始,循环找到要插入幂项的地方
while(cmp(p->element ,s->element)==LESS)//比较插入的幂指和多项式中的幂指大小
{
if(p!=Lh->tail )
p=p->Next ;
else
break;
}
switch(cmp(p->element ,s->element ))
{
case LESS://如果查找结束,如果插入项比当前项小,则插入项一定是插入到表尾
p->Next=s;s->Prev =p;Lh->tail =s;break;
case MORE://如果查找结果,如果插入项比当前项大,则有可能是表头项或是表中项
if(p==Lh->header)//判断是否是表头
Lh->header =s;//插入表头
else //如果不是表头项,则是表中项
{
s->Prev =p->Prev ;//把项链结到表中项的直接前驱上
p->Prev ->Next =s;
}
s->Next =p;//链结直接后继
p->Prev =s;
break;
case EQUAL://如果相等,则把幂项相加
if((p->element.coef +s->element.coef )==0)//相加结果为0,则删除该项
DeletePolynItem(Lh,p);
else
p->element.coef +=s->element.coef ;//如果不为0,则把系数相加作为被加后的新系数
break;
}
Lh->length ++;
return OK;
}//InsertElement
Status AddPolynItem(LHeader &Lh,Element e)
{
return InsertElement(Lh,e);
}//AddPolynItem
Status MubPolynItem(LHeader &LR,LHeader Lh,Element e)
{
LPNode p=Lh->header ;
Element t;
while(p)
{
t.coef =p->element.coef *e.coef ;
t.expn =p->element.expn +e.expn ;
InsertElement(LR,t);
p=p->Next ;
}
return OK;
}
Status AddPolyn(LHeader &La,LHeader &Lb)
{
LPNode p=Lb->header ; //取得Lb线性表的表头
while(p)//依序取得线性表中的每一个元素(即一元多项式的每一个项)
{
AddPolynItem(La,p->element );//把每一个项加到(插入到)线性表La中去
p=p->Next ;
}
return OK;
}//AddPolyn
Status SubPolyn(LHeader &La,LHeader &Lb)
{
LPNode p=Lb->header ;//取得线性表的表头
Element e;
while(p)//依序取得一元多项式的每一个幂项
{ e.coef =-p->element.coef ;//系数取反
e.expn =p->element.expn ;
AddPolynItem(La,e );//把取反以后的系数加到一元多项式La上去
p=p->Next ;
}
return OK;
}//SubPolyn
Status MulPolyn(LHeader &La,LHeader &Lb)
{
LHeader T;
InitPolyn(T);
LPNode p=Lb->header ;
while(p)
{
MubPolynItem(T,La,p->element );//把每一个被乘的多项式相加
p=p->Next ;
}
DestroyPolyn(La);
La->header =T->header ;
La->length =T->length ;
La->tail =T->tail ;
return OK;
}
void ListPolyn(LHeader Lh,void (*f)(LPNode lp))
{
LPNode p=Lh->header ;
while(p)
{
(*f)(p);
p=p->Next ;
}
}//ListPolyn
#include<stdio.h>
#include"Polyn.h"
void PrintList(LPNode p)
{
if(p->Prev ==NULL)
printf("%.2fX^%d",p->element.coef ,p->element.expn );
else
printf(" + %.2fX^%d",p->element.coef ,p->element.expn );
}
void main()
{
LHeader Polyn;
InitPolyn(Polyn);
Element e;
e.coef =4;
e.expn =9;
InsertElement(Polyn,e);
e.coef =4;
e.expn =7;
InsertElement(Polyn,e);
e.coef =4;
e.expn =5;
InsertElement(Polyn,e);
e.coef =4;
e.expn =1;
InsertElement(Polyn,e);
e.coef =4;
e.expn =6;
InsertElement(Polyn,e);
e.coef =4;
e.expn =2;
InsertElement(Polyn,e);
e.coef =4;
e.expn =2;
InsertElement(Polyn,e);
e.coef =4;
e.expn =2;
InsertElement(Polyn,e);
e.coef =4;
e.expn =5;
InsertElement(Polyn,e);
e.coef =4;
e.expn =100;
InsertElement(Polyn,e);
e.coef =4;
e.expn =11;
InsertElement(Polyn,e);
e.coef =4;
e.expn =-5;
InsertElement(Polyn,e);
e.coef =-4;
e.expn =-5;
InsertElement(Polyn,e);
e.coef =4;
e.expn =2;
InsertElement(Polyn,e);
e.coef =4;
e.expn =2;
InsertElement(Polyn,e);
e.coef =4;
e.expn =2;
InsertElement(Polyn,e);
e.coef =4;
e.expn =115;
InsertElement(Polyn,e);
ListPolyn(Polyn,PrintList);
printf("\n");
AddPolyn(Polyn,Polyn);
ListPolyn(Polyn,PrintList);
printf("\n");
MulPolyn(Polyn,Polyn);
ListPolyn(Polyn,PrintList);
printf("\n");
}
求大神帮下:
要求:
数据要求从文件读入,把结果输出到文件
多组测试数据。
多项式从文件输入
可以实现多项式:连续运算,如多项式a,b,c,d.计算:a*b+c*d-a-c;
希望大神能帮我下,在原代码上改动达到要求。