设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct elem
{
int coef;
int expo;
struct elem* next;
}list;
list* Create_List_Readin();//从输入读取并建立链表;
list* Create_List_Empty();//建立空表,返回头结点;
list* Plus_List_Former(list* L1, list* L2);//多项式加法,存到L1,销毁L2;
list* Plus_List_New(list* L1, list* L2);//多项式加法,结果存在新的链表里;
list* Insert_List_LNode(list* LNode, int xishu, int zhishu);//在LNode这个结点后边加一个结点,并存储数据;
list* Multiply_List(list* L1, list* L2);//多项式乘法,结果存在新的链表里;
void Print_List(list* L);//打印;
int main()
{
list* L1 = Create_List_Readin(), * L2 = Create_List_Readin();
list* L3 = Multiply_List(L1, L2);
Print_List(L3);
L3 = Plus_List_Former(L1, L2);
Print_List(L3);
return 0;
}
list* Create_List_Empty()
{
list* head = (list*)malloc(sizeof(list));
head->next = NULL;//这步不能少;
return head;
}
list* Create_List_Readin()
{
list* head = (list*)malloc(sizeof(list)), * node, * end = head;
int n; scanf("%d", &n);
while (n-- > 0) {
node = (list*)malloc(sizeof(list));
scanf("%d %d", &node->coef, &node->expo);
if (node->coef == 0) {//零多项式直接跳过,不存储;
free(node);
continue;
}
end->next = node;
end = node;
}
end->next = NULL;
return head;
}
list* Plus_List_New(list* L1, list* L2)//多项式加法,保持按指数递降,结果存在新的链表里;
{
list* p1 = L1->next, * p2 = L2->next;
list* head = (list*)malloc(sizeof(list)), * node, * end = head;
while (p1 && p2) {
node = (list*)malloc(sizeof(list));
if (p1->expo < p2->expo) {
node->coef = p2->coef;
node->expo = p2->expo;
p2 = p2->next;
}
else if (p1->expo > p2->expo) {
node->coef = p1->coef;
node->expo = p1->expo;
p1 = p1->next;
}//新的结点里保存较大的数据,同时较大的那一项的指针后移指向下一项,但是较小的那个结点
//的指针不动,因为当前这一项还有比较的价值
else {
node->coef = p1->coef + p2->coef;
node->expo = p1->expo;
p1 = p1->next;
p2 = p2->next;//两个表的指针都要后移,
if (!node->coef) {//零多项式舍弃;
free(node);
continue;
}
}
end->next = node;
end = node;
}
end->next = (p1) ? p1 : p2;//因为大概率两个式子是不一样长的,但是一个式子遍历到尾了就会退
//出,而另一个式子很可能还有内容,这个时候就把它直接挂到新表尾
return head;
}
list* Plus_List_Former(list* L1, list* L2) // 穿针引线法(瞎起的名)多项式求和,保持按指数递降,存到L1,销毁L2;
{
list* p1 = L1->next, * p2 = L2->next, * p = L1;//p就是那根针,在p1,p2检索的时候,p始终寻找指数较大的那一个
if (!p1 || !p2)
return (p1) ? L1 : L2;//如果有一个是空表,就直接把另一个表返回,无所谓另一个表是否为空;
while (p1 && p2) {
if (p1->expo > p2->expo) {
p->next = p1;
p = p1;
p1 = p1->next;
}
else if (p2->expo > p1->expo) {
p->next = p2;
p = p2;
p2 = p2->next;
}//首先把p所在的结点连接到指数较大的那一项,然后p自己再指过去
else {
list* temp;
if (p1->coef += p2->coef) {
p->next = p1;
p = p1;
p1 = p1->next;
}
else {
p->next = p1->next;
free(p1);
p1 = p->next;
}//两项指数相等,把系数和存在p1结点里,删除p2结点
//若系数和为0,把两个结点都删除
temp = p2;
p2 = p2->next;
free(temp);
}
}
p->next = (p1) ? p1 : p2;//把还有内容的表直接挂到p
free(L2);
L2 = NULL;
return L1;
}
list* Insert_List_LNode(list* LNode, int xishu, int zhishu)//在LNode这个结点后边加一个结点,并存储数据;
{
list* node = (list*)malloc(sizeof(list));
LNode->next = node;
node->coef = xishu;
node->expo = zhishu;
node->next = NULL;
return node;//返回新的结点;
}
list* Multiply_List(list* L1, list* L2)
{
list* p1 = L1->next, * p2 = L2->next, * ptr;
if (!p1 || !p2)
return (!p1) ? L1 : L2;//返回空表;
list* L3 = Create_List_Empty();//L3就是数据存储的新表;
ptr = L3;
for (; p2; p2 = p2->next)
ptr = Insert_List_LNode(ptr, p1->coef * p2->coef, p1->expo + p2->expo);//先把L1的第一项同L2都乘一遍,结果挨个插在L3尾部;
for (p1 = p1->next; p1; p1 = p1->next) {
list* L4 = Create_List_Empty();//L4是临时的链表,用来存储L1自第二项开始每一项与L2相乘的结果
ptr = L4;
for (p2 = L2->next; p2; p2 = p2->next) {
ptr = Insert_List_LNode(ptr, p1->coef * p2->coef, p1->expo + p2->expo);//结果挨个插在L4尾部;
}
Plus_List_Former(L3, L4);//Plus_List_Former()会把传过去的第二个链表销毁,把求和的数据存在第一个链表;
//L4表这时已经不存在了,在下一项开始计算前会再创建;
}
return L3;
}
void Print_List(list* L)
{
list* ptr = L->next;
if (!ptr)
printf("0 0\n");//如果空表,直接打印0 0,换行
else {
for (; ptr->next; ptr = ptr->next)
printf("%d %d ", ptr->coef, ptr->expo);
printf("%d %d\n", ptr->coef, ptr->expo);//结尾没有多余空格
}
}