7-1 一元多项式的乘法与加法运算 (20 分)
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0
。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
链表法解题
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct PolyNode *Polynomial;//链表结点的构成
struct PolyNode
{
int coef;//系数
int expon;//质数
Polynomial link;//指针域
};
Polynomial ReadPoly();
Polynomial Add(Polynomial P1,Polynomial P2);
Polynomial Mult(Polynomial P1,Polynomial P2);
void PrintPoly(Polynomial P);
void Attach(int c,int e,Polynomial *pRear);
void Attach(int c,int e, Polynomial *pRear)
{
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));//采用先申请空结点的方法
P->coef = c;//对新结点赋值
P->expon = e;
P->link = NULL;
(*pRear)->link = P;//把新申请的结点P插到pRear后面,指针域移动
*pRear = P;//使pRear指向最后
}
Polynomial ReadPoly()
{
Polynomial P,Rear,t;//Rear表示当前多项式尾部指针,临时变量t是用来储存空结点,之后会被删除
int n;
int c,e;
scanf("%d",&n);
P = (Polynomial)malloc(sizeof(struct PolyNode));//申请链表头空结点
P->link = NULL;
Rear = P;//让Rear指向空结点
while(n--)
{
scanf("%d %d",&c,&e);
Attach(c,e,&Rear);//将当前项插入多项式尾部
}
t = P;P = P->link;free(t);//删除空(头)结点语句,把空结点用t储存,link指针指向P,继续往下走,然后删除空结点t
return P;
}
Polynomial Add(Polynomial P1,Polynomial P2)
{
Polynomial P,Rear,t1,t2,t;
int sum;
t1 = P1;
t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;//让Rear指向空结点
while(1)
{
if(t1 != NULL&&t2 != NULL)
{
if(t1->expon == t2->expon)
{
sum = t1->coef + t2->coef;
if(sum != 0)
Attach(sum,t1->expon,&Rear);
t1 = t1->link;
t2 = t2->link;
}
else if(t1->expon > t2->expon)
{
Attach(t1->coef,t1->expon,&Rear);
t1 = t1->link;
}
else
{
Attach(t2->coef,t2->expon,&Rear);
t2 = t2->link;
}
}
else if(t1 != NULL)
{
Attach(t1->coef,t1->expon,&Rear);
t1 = t1->link;
}
else if(t2 != NULL)
{
Attach(t2->coef,t2->expon,&Rear);
t2 = t2->link;
}
else
break;
}
/*
while(t1&&t2)//当t1,t2都不空
{
if(t1->expon==t2->expon)
{
sum = t1->coef+t2->coef;
if(sum==0)
{
t1=t1->link;
t2=t2->link;
}
else
{
Attach(sum,t1->expon,&Rear);
t1=t1->link;
t2=t2->link;
}
}
if(t1->expon>t2->expon)
{
Attach(t1->coef,t1->expon,&Rear);
t1=t1->link;
}
if(t1->expon<t2->expon)
{
Attach(t2->coef,t2->expon,&Rear);
t2=t2->link;
}
}
while(t1)
{
Attach(t1->coef,t1->expon,&Rear);
t1=t1->link;
}
while(t2)
{
Attach(t2->coef,t2->expon,&Rear);
t2=t2->link;
}
*/
Rear->link = NULL;
t=P;P=P->link;free(t);//删除空结点
return P;
}
Polynomial Mult(Polynomial P1,Polynomial P2)
{
Polynomial P,Rear,t1,t2,t;
int c,e;
if(!P1||!P2)
return NULL;
t1 = P1;t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;
while(t2)//先用P1的第一项乘以P2
{
Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
t2 = t2->link;
}
t1 = t1->link;
while(t1)
{
t2 = P2;
Rear = P;
while(t2)
{
c = t1->coef*t2->coef;
e = t1->expon+t2->expon;
while(Rear->link&&Rear->link->expon>e)//要插入的与链表中的比较,如果比链表中的小,就将Rear后移
Rear = Rear->link;
if(Rear->link&&Rear->link->expon == e)//如果相等
{
if(Rear->link->coef + c)//加起来不等于0的话就直接系数累加
Rear->link->coef += c;
else//等于0的话直接删掉即可
{
t = Rear->link;
Rear->link = t->link;
free(t);
}
}
else//要插入的与链表中的比较,如果比链表中的大,就要构造空结点
{
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->coef = c;t->expon = e;
t->link = Rear->link;//插入操作
Rear->link = t;Rear = Rear->link;
}
t2 = t2->link;
}
t1 = t1->link;
}
t2 = P;P = P->link;free(t2);//删除刚开始构建的空结点
return P;
}
void PrintPoly(Polynomial P)
{
int flag = 0;
if(!P)
{
printf("0 0\n");
return ;
}
while(P)
{
if(!flag)
flag = 1;
else
printf(" ");
printf("%d %d",P->coef,P->expon);
P = P->link;
}
printf("\n");
}
int main()
{
Polynomial P1,P2,PP,PS;
P1 = ReadPoly();
P2 = ReadPoly();
PP = Mult(P1,P2);//乘法
PrintPoly(PP);
PS = Add(P1,P2);//加法
PrintPoly(PS);
return 0;
}