设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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 <stdlib.h>
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType coef;
ElementType pow;
PtrToNode Next;
};
typedef PtrToNode List;
List initNode()
{
List L = (List)malloc(sizeof(struct Node));
L->Next = NULL;
return L;
}
void Print( List L )
{
if (L->Next == NULL)
{
printf("0 0");
}
else
{
L = L->Next;
while(L !=NULL)
{
printf("%d %d", L->coef, L->pow);
if (L->Next != NULL)
printf(" ");
L = L->Next;
}
}
}
List readInputLine()
{
List L = initNode();
List Header = L;
char c;
int num;
scanf("%d%c", &num, &c);
if (num > 0)
{
while(1)
{
List temp = initNode();
scanf("%d %d%c", &(temp->coef), &(temp->pow), &c);
L -> Next = temp;
L = L -> Next;
if (c == '\n')
break;
}
}
else
{
List node = initNode();
node->coef = 0;
node->pow = 0;
L->Next = node;
}
return Header;
}
void removeZero(List l)
{
List lPre = l;
l = l->Next;
while(l != NULL)
{
if (l->coef == 0)
{
List temp = l;
lPre->Next = l->Next;
l=lPre;
free(temp);
}
else
{
lPre = l;
}
l = l->Next;
}
}
List sum(List l1, List l2)
{
l1 = l1->Next;
l2 = l2->Next;
List l = initNode();
List Header = l;
while(l1 != NULL && l2 != NULL)
{
List temp = initNode();
l->Next = temp;
l = temp;
if (l1->pow == l2->pow)
{
l->pow = l1->pow;
l->coef = l1->coef + l2->coef;
l1 = l1->Next;
l2 = l2->Next;
}
else if (l1->pow > l2->pow)
{
l->pow = l1->pow;
l->coef = l1->coef;
l1 = l1->Next;
}
else
{
l->pow = l2->pow;
l->coef = l2->coef;
l2 = l2->Next;
}
}
if (l1 != NULL)
{
List temp = initNode();
l->Next = temp;
l = temp;
l->pow = l1->pow;
l->coef = l1->coef;
l1 = l1->Next;
}
if (l2 != NULL)
{
List temp = initNode();
l->Next = temp;
l = temp;
l->pow = l2->pow;
l->coef = l2->coef;
l2 = l2->Next;
}
l->Next = NULL;
removeZero(Header);
return Header;
}
List find(List l, ElementType x)
{
l = l->Next;
while(l != NULL)
{
if (l->pow == x)
return l;
l = l->Next;
}
return NULL;
}
void insert(List l, List node)
{
List lPre = l;
while(l->Next != NULL)
{
if (l->Next->pow < node->pow)
{
node->Next = l->Next;
l->Next = node;
return;
}
l = l->Next;
}
if (l->Next == NULL)
{
l->Next = node;
}
}
List multiply(List l1, List l2)
{
l1 = l1->Next;
List l2Temp = l2->Next;
List Header = initNode();
while(l1 != NULL)
{
l2 = l2Temp;
while(l2 != NULL)
{
int pow = l1->pow + l2->pow;
int coef = l1->coef * l2->coef;
List temp = find(Header, pow);
if (temp)
{
temp->coef += coef;
}
else
{
List newNode = initNode();
newNode->pow = pow;
newNode->coef = coef;
insert(Header, newNode);
}
l2 = l2->Next;
}
l1 = l1->Next;
}
removeZero(Header);
return Header;
}
int main(void)
{
List L1, L2;
L1 = readInputLine();
L2 = readInputLine();
List M = multiply(L1, L2);
Print(M);
printf("\n");
List S = sum (L1, L2);
Print(S);
return 0;
}