c语言实现多项式的求和与乘积
由于一些时间原因和个人英语水平有限,在这个代码里面并没有做申请空间是否成功的判断和启明有些LOW,不要介意,而且输入的多项式只能是指数非递增的,嘻嘻,直接进入正题:
本程序用的是C语言链表实现的多项式的求和与乘积计算。
结构体:
typedef struct Node
{
int zs;
int xs;
struct Node* next;
}poly;
typedef poly* poly_node;
typedef poly* polynomial;
创建并赋值多项式链表函数:
polynomial GetPoly()
{
int n;
scanf("%d",&n);
poly_node head,end,temp;
head = (poly*)malloc(sizeof(poly)); //申请头节点
end = head;
for(;n>0;n--)
{
end->next = (poly*)malloc(sizeof(poly)); //申请节点,并且是一条空头的链表
end = end->next;
scanf("%d%d",&end->xs,&end->zs);
}
end->next = NULL; //封尾,方便后序的遍历
temp = head; //中间变量
head = head->next; //头为空,不影响数据
free(temp); //释放空头节点空间
return head;
}
多项式求和函数:
polynomial polySum(polynomial p1,polynomial p2)
{
poly_node head,end,temp;
head = (poly*)malloc(sizeof(poly));
end = head;
while( p1 && p2 )
{ //此处遵循多项式求和规律
if(p1->zs > p2->zs)
{
polyNodeAdd(p1->zs , p1->xs , &end); //调用追加函数,添加多项式结果
p1 = p1->next;
}
else if(p1->zs < p2->zs)
{
polyNodeAdd(p2->zs , p2->xs , &end); //调用追加函数,添加多项式结果
p2 = p2->next;
}
else
{
if((p1->xs + p2->xs) != 0) //如果指数相等,系数不为零执行此步
polyNodeAdd(p1->zs , (p1->xs) + (p2->xs) , &end);
p1 = p1->next;
p2 = p2->next;
}
}
for( ; p1; p1 = p1->next) //如果链表在上面的while没有遍历完,则在此处完成遍历添加
polyNodeAdd(p1->zs , p1->xs , &end);
for( ; p2; p2 = p2->next)
{
polyNodeAdd(p2->zs , p2->xs , &end);
}
end->next = NULL;
temp = head;
head = head->next;
free(temp);
return head;
}
多项式乘法函数:
polynomial polyProduct(polynomial p1,polynomial p2)
{
poly_node head,end,temp,head2;
head2 = (poly*)malloc(sizeof(poly));
head2 = NULL;
for(; p1 ;p1 = p1->next) //调用多项式求和函数,将第一个多项式的第n项与第二条多项式的乘积加上第一个多项式的第n+1项与第二条多项式的乘积的和。
{
head = (poly*)malloc(sizeof(poly));
end = head;
for(temp = p2;temp;temp = temp->next)
polyNodeAdd( (p1->zs)+(temp->zs),(p1->xs)*(temp->xs) , &end);
head2 = polySum(head2,head->next);
free_poly(head->next); //释放掉上一条多项式链表
}
return head2;
}
多项式链表节点追加函数:
void polyNodeAdd(int zs,int xs,poly_node* end) //二级指针
{
poly_node temp;
temp = (poly*)malloc(sizeof(poly));
temp->zs = zs;
temp->xs = xs;
(*end)->next = temp;
(*end) = (*end)->next;
(*end)->next=NULL; //封口
}
多项式链表打印函数:
void polyOutput(polynomial temp)
{
printf("\n");
if(!temp)printf("0 0"); //如果得出的多项式为空时输出0 0.
while(temp)
{
printf("%d %d ",temp->xs,temp->zs);
temp = temp->next;
}
}
完整程序:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int zs;
int xs;
struct Node* next;
}poly;
typedef poly* poly_node;
typedef poly* polynomial;
polynomial GetPoly();
void free_poly(polynomial);
void polyNodeAdd(int,int,poly_node*);
polynomial polySum(polynomial,polynomial);
polynomial polyProduct(polynomial,polynomial);
void polyOutput(polynomial);
int main()
{
polynomial p1,p2,temp;
p1=GetPoly();
p2=GetPoly();
temp = polyProduct(p1,p2);
polyOutput(temp);
free_poly(temp);
temp = polySum(p1,p2);
polyOutput(temp);
free_poly(temp);
return 0;
}
polynomial GetPoly()
{
int n;
scanf("%d",&n);
poly_node head,end,temp;
head = (poly*)malloc(sizeof(poly));
end = head;
for(;n>0;n--)
{
end->next = (poly*)malloc(sizeof(poly));
end = end->next;
scanf("%d%d",&end->xs,&end->zs);
}
end->next = NULL;
temp = head;
head = head->next;
free(temp);
return head;
}
polynomial polySum(polynomial p1,polynomial p2)
{
poly_node head,end,temp;
head = (poly*)malloc(sizeof(poly));
end = head;
while( p1 && p2 )
{
if(p1->zs > p2->zs)
{
polyNodeAdd(p1->zs , p1->xs , &end);
p1 = p1->next;
}
else if(p1->zs < p2->zs)
{
polyNodeAdd(p2->zs , p2->xs , &end);
p2 = p2->next;
}
else
{
if((p1->xs + p2->xs) != 0)
polyNodeAdd(p1->zs , (p1->xs) + (p2->xs) , &end);
p1 = p1->next;
p2 = p2->next;
}
}
for( ; p1; p1 = p1->next)
polyNodeAdd(p1->zs , p1->xs , &end);
for( ; p2; p2 = p2->next)
{
polyNodeAdd(p2->zs , p2->xs , &end);
}
end->next = NULL;
temp = head;
head = head->next;
free(temp);
return head;
}
void polyNodeAdd(int zs,int xs,poly_node* end)
{
poly_node temp;
temp = (poly*)malloc(sizeof(poly));
temp->zs = zs;
temp->xs = xs;
(*end)->next = temp;
(*end) = (*end)->next;
(*end)->next=NULL;
}
void polyOutput(polynomial temp)
{
printf("\n");
if(!temp)printf("0 0");
while(temp)
{
printf("%d %d ",temp->xs,temp->zs);
temp = temp->next;
}
}
void free_poly(polynomial p)
{
poly_node temp;
while(p)
{
temp = p;
p=p->next;
free(temp);
}
}
polynomial polyProduct(polynomial p1,polynomial p2)
{
poly_node head,end,temp,head2;
head2 = (poly*)malloc(sizeof(poly));
head2 = NULL;
for(; p1 ;p1 = p1->next)
{
head = (poly*)malloc(sizeof(poly));
end = head;
for(temp = p2;temp;temp = temp->next)
polyNodeAdd( (p1->zs)+(temp->zs),(p1->xs)*(temp->xs) , &end);
head2 = polySum(head2,head->next);
free_poly(head->next);
}
return head2;
}
测试用例:
项数 系数 指数 系数 指数……
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
(一般情况)
2 1 2 1 0
2 1 2 -1 0
(同类项合并时有抵消)
2 -1000 1000 1000 0
2 1000 1000 -1000 0
(系数和指数取上限,结果有零多项式)
0
1 999 1000
(输入有零多项式和常数多项式)
输出结果:
还有最最重要的一点,记得加空间申请是否成功的判断哟。
————————————本文结束————————————
评论区