一元多项式加减乘除源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//Polynomial_list
//功能:输入两个多项式(不要求是升幂排列,程序将自动合并同类项和排序),进行加减乘除运算
#include
#include
#include
//结点定义
typedef struct PolyNode
{
int exp; //指数
float coef; //系数
PolyNode* next;
}PolyNode , * PolyList;
//函数声明
PolyList CreatePolyList(); //创建多项式链表,返回头指针
void DisplayPolyList(PolyList Poly);//显示多项式
void DestroyPolyList(PolyList L);//释放链表所用存储空间
void MergePoly(PolyList Poly);//将多项式和并同类项
void SortPoly(PolyList Poly);//将多项式按升序排列
PolyList PolyAdd(PolyList PolyA , PolyList PolyB);//多项式相加,返回和多项式链表头指针
PolyList PolySub(PolyList polya , PolyList polyb);//多项式相减,返回差多项式链表头指针
PolyList PolyMutiply(PolyList PolyA , PolyList PolyB);//多项式相乘,结果由PolyC返回
PolyList PolyDivide(PolyList PolyA , PolyList PolyB);//多项式相除,结果存到PolyC中,商和余数用系数为0的结点分开
//函数实现
//创建多项式链表,返回头指针
PolyList CreatePolyList()
{
PolyNode *s,*rear,*head;
int e; //指数
float c; //系数
int n=1; //计数器
head =(PolyNode *) malloc(sizeof(PolyNode));
rear = head;
//输入多项式的系数和指数,若输入系数为0退出
printf("请输入多项式的第%d项的系数和指数(用逗号分开):" , n++);
scanf("%f,%d" , &c , &e);
while(fabs(c) > 1e-6)
{ s = (PolyNode *)malloc(sizeof(PolyNode));
s->exp = e;
s->coef = c;
rear->next = s;
rear = s;
printf("请输入多项式的第%d项的系数和指数:" , n++);
scanf("%f,%d" , &c , &e);
}
rear->next = NULL;
return head;
}
//计算两个多项式(可不按顺序排列) , 结果存到链表PolyC中,并返回
PolyList PolyAdd(PolyList PolyA , PolyList PolyB)
{
PolyList PolyC ;
SortPoly(PolyA);
SortPoly(PolyB);
float sum=0;//存储两项系数和
PolyNode *pa , *pb , *rear , *s ;
PolyC = (PolyNode *)malloc(sizeof(PolyNode));
pa = PolyA->next;
pb = PolyB->next;
rear = PolyC;
rear->next = NULL;
while(pa && pb)
{
if(pa->exp == pb->exp)
{
sum = pa->coef+pb->coef;
if(fabs(sum)>1e-6) //如果两两系数不为0,则将两项和存入s中,并插入PolyC尾部
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = sum;
s->exp = pa->exp;
rear->next = s;
rear = s;
}
//pa,pb指针后移
pa = pa->next;
pb = pb->next;
}
else if(pa->exp>pb->exp) //若pa指数大于pb指数,将pa结点副本插入到PolyC尾部
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pa->coef;
s->exp = pa->exp;
rear->next = s ;
rear = s ;
pa = pa->next;
}
else //若pb指数大于pa指数,将pb结点副本插入到PolyC尾部
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pb->coef;
s->exp = pb->exp;
rear->next = s;
pb = pb->next;
rear = s ;
}
}
//插入剩余结点
while(pa)
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pa->coef;
s->exp = pa->exp;
rear->next = s;
pa = pa->next;
rear = s ;
}
while(pb)
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pb->coef;
s->exp = pb->exp;
rear->next = s;
pb = pb->next;
rear = s ;
}
rear->next = NULL;
return PolyC;
}
//释放链表所用存储空间
void DestroyPolyList(PolyList L)
{
PolyNode * p , *temp;
p = L;
while(p!=NULL)
{temp = p ; p = p->next; free(temp); }
}
//将多项式和并同类项
void MergePoly(PolyList Poly)
{
PolyNode * p , *q , * rear ,*pre ,* temp;
rear = Poly;
p = Poly->next ;
while(rear->next!=NULL)
{
q = p->next;
pre = p;
temp = p;
while(q)
{
if(p->exp == q->exp) //两项指数相等,合并两项,并释放结点q
{
p->coef+=q->coef;
if(fabs(p->coef)>1e-6)
{
pre->next = q->next;
temp = q;
q = temp->next;
free(temp);
}
else //两项系数和为0,释放结点p和q
{
rear->next = p->next;
temp = p;
p = temp->next;
free(temp);
pre->next = q->next;
temp = q;
q = temp->next;
free(temp);
}
}
else{pre= q ; q = q->next;} //指数不等,指针q后移
}
//与p指数相同的节点合并完毕,或者没有找到,p后移
rear = p;
p = rear->next;
}
rear->next = NULL;
}
//将多项式按升序排列
void SortPoly(PolyList Poly)
{
PolyList rear , p ,temp , prior;
if(!Poly->next) return; //若多项式为空,返回
MergePoly(Poly);
rear = Poly;
int exp;//记录当前啊搜索项中的最小指数
while(rear->next!=NULL)
{
exp = rear->next->exp;
p = rear->next ;
prior = rear;
temp = prior->next ;
while(p!=NULL)
{
if(p->exp > exp)
{
exp = p->exp ;
temp = p ;
p = temp->next ;
}
else
{
p = p->next ;
if(rear->next->next ==NULL) return; //p为最后一个元素且指数最小,提前返回
}
}
while(prior->next != temp) prior = prior->next ;
prior->next = temp->next;
temp->next = rear->next ;
rear->next = temp;
rear = rear->next ;
}
}
//多项式相减,返回差多项式链表头指针
PolyList PolySub(PolyList PolyA , PolyList PolyB)
{
PolyList PolyC ;
SortPoly(PolyA);
SortPoly(PolyB);
float sum =0 ;//存储两项系数差
PolyNode *pa , *pb , *rear , *s ;
PolyC = (PolyNode *)malloc(sizeof(PolyNode));
pa = PolyA->next;
pb = PolyB->next;
rear = PolyC;
rear->next = NULL;
while(pa && pb)
{
if(pa->exp == pb->exp)
{
sum = pa->coef-pb->coef;
if(fabs(sum)>1e-6) //如果两两系数不为0,则将两项和存入s中,并插入PolyC尾部
{
s = (Po
lyNode *)malloc(sizeof(PolyNode));
s->coef = sum;
s->exp = pa->exp;
rear->next = s;
rear = s;
}
//pa,pb指针后移
pa = pa->next;
pb = pb->next;
}
else if(pa->exp>pb->exp) //若pa指数大于pb指数,将pa结点副本插入到PolyC尾部
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pa->coef;
s->exp = pa->exp;
rear->next = s ;
rear = s ;
pa = pa->next;
}
else //若pb指数大于pa指数,将pb结点副本插入到PolyC尾部
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = -pb->coef;
s->exp = pb->exp;
rear->next = s;
pb = pb->next;
rear = s ;
}
}
//插入剩余结点
while(pa)
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pa->coef;
s->exp = pa->exp;
rear->next = s;
pa = pa->next;
rear = s ;
}
while(pb)
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = -pb->coef;
s->exp = pb->exp;
rear->next = s;
pb = pb->next;
rear = s ;
}
rear->next = NULL;
return PolyC;
}
//输出多项式
void DisplayPolyList(PolyList Poly)
{
if(Poly == NULL) {printf("\n");return;}
PolyNode *p=Poly->next ;
if(p == NULL){printf("0\n"); return;} //如果链表为空提前退出
while(p->next!=NULL)
{
if(fabs(p->coef)>1e-6)
{
if(fabs(p->next->coef)>1e-6) printf("%f*x**%d + " , p->coef , p->exp);
else printf("%f*x**%d " , p->coef , p->exp);
}
else printf("......");//输出分割点
p = p->next ;
}
if(fabs(p->coef)>1e-6)printf("%f*x**%d" , p->coef , p->exp);
printf("\n");
}
//多项式相乘,结果由PolyC返回
PolyList PolyMutiply(PolyList PolyA , PolyList PolyB)
{
PolyList PolyC;
PolyNode *pa , *pb , *pc_pre , *pc , *s;
if(PolyA==NULL || PolyB==NULL) return NULL; //若某一个多项式为空,返回
PolyC = (PolyNode*)malloc(sizeof(PolyNode));
pc = PolyC ;
pc->next = NULL;
if(PolyA->next==NULL || PolyB->next==NULL) return PolyC;
SortPoly(PolyA);
SortPoly(PolyB);
pa = PolyA->next ;
pb = PolyB->next;
s = (PolyNode*)malloc(sizeof(PolyNode));
s->coef = pa->coef * pb->coef ;
s->exp = pa->exp + pb->exp ;
if(pc->next == NULL)
{
pc->next = s ;
pc =s ;
pc->next = NULL ;
} //直接插入第一个结点
while(pa)
{
pb = PolyB->next ;
while(pb)
{
//两项对应相乘,结果存入到s中
pc = PolyC->next;
if(pa == PolyA->next && pb==PolyB->next) //避免重复插入第一个结点
{
pb=pb->next;
if(pb == NULL) break;
}
s = (PolyNode*)malloc(sizeof(PolyNode));
s->coef = pa->coef * pb->coef ;
s->exp = pa->exp + pb->exp ;
//查找s合适的插入位置,使得插入后PolyC仍为升序排列
while( pc && pc->exp > s->exp) { pc_pre = pc ; pc=pc_pre->next ;}
if(pc ==NULL){pc_pre->next=s ; s->next =NULL;pb=pb->next;}
else if( pc->exp < s->exp){pc_pre->next = s ; s->next = pc ; pb=pb->next; }
else if(s->exp == pc->
exp )
{
pc->coef += s->coef ;
free(s);
if(fabs(pc->coef)<1e-6 ){pc_pre->next = pc->next ; free(pc);}
pb = pb->next;
}
}
pa = pa->next;
}
return PolyC;
}
//多项式相除,结果存到PolyC中,商和余数用系数为0的结点分开
PolyList PolyDivide(PolyList PolyA , PolyList PolyB)
{
if(!PolyA || !PolyB) return NULL;
if(PolyB->next == NULL){printf("Error:除项为空!\n");return NULL;}
PolyList PolyT1 , PolyT2 , pt , s , PolyC , p , s_pre;
PolyC = (PolyList)malloc(sizeof(PolyNode));
PolyC->next=NULL;
if(PolyA->next==NULL) return PolyC;
p = PolyA->next;
PolyT1 = (PolyList)malloc(sizeof(PolyNode));
pt = PolyT1;
s_pre=(PolyList)malloc(sizeof(PolyNode));
while(p) //将PollyA复制到PolyT中
{
s = (PolyList)malloc(sizeof(PolyNode));
s->coef = p->coef ;
s->exp = p->exp ;
pt->next = s;
pt = s;
p = p->next ;
}
pt->next=NULL;
//将商存入到PolyC中
p = PolyC;
while(PolyT1->next && PolyT1->next->exp >= PolyB->next->exp)
{
s = (PolyList)malloc(sizeof(PolyNode));
s_pre->next = s;
s->next=NULL;
s->coef = PolyT1->next->coef/PolyB->next->coef;
s->exp = PolyT1->next->exp - PolyB->next->exp;
p->next = s;
p = s;
//PolyT2 = (PolyList)malloc(sizeof(PolyNode));
PolyT2 = PolySub(PolyT1 , PolyMutiply(PolyB , s_pre));
DestroyPolyList(PolyT1);
PolyT1 = PolyT2;
}
//设置分隔结点
s =(PolyList)malloc(sizeof(PolyNode));
s->coef = 0;
s->exp = 0;
p->next = s;
p = s;
p->next = PolyT1->next; //将余项PolyT复制到PolyC中
free(PolyT1);
return PolyC;
}
void main()
{
PolyList PolyA, PolyB , PolyC;
//初始化PolyA,PolyB,以0结束
printf("************************************************************************\n");
PolyA = CreatePolyList();
printf("输入的PolyA: ");
DisplayPolyList(PolyA);
printf("************************************************************************\n");
PolyB = CreatePolyList();
printf("输入的PolyB: ");
DisplayPolyList(PolyB);
printf("************************************************************************\n");
SortPoly(PolyA);
printf("合并排序后的PolyA: ");
DisplayPolyList(PolyA);
SortPoly(PolyB);
printf("合并排序后的PolyB: ");
DisplayPolyList(PolyB);
printf("************************************************************************\n");
//PolyA + PolyB
PolyC = PolyAdd(PolyA , PolyB);
printf("PolyA+PolyB: ");
DisplayPolyList(PolyC);
DestroyPolyList(PolyC);
printf("************************************************************************\n");
//PolyA - PolyB
PolyC = PolySub(PolyA , PolyB);
printf("PolyA-PolyB: ");
DisplayPolyList(PolyC);
DestroyPolyList(PolyC);
//PolyA*PolyB
printf("************************************************************************\n");
PolyC = PolyMutiply(PolyA , PolyB);
printf("PolyA*PolyB: ");
DisplayPolyList(PolyC);
DestroyPol
yList(PolyC);
//PolyA/PolyB
printf("************************************************************************\n");
PolyC = PolyDivide(PolyA , PolyB);
printf("PolyA/PolyB: ");
DisplayPolyList(PolyC);
DestroyPolyList(PolyC);
printf("************************************************************************\n ");
}