题目要求
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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 <iostream>
#include <cstdlib>
struct listNode;
typedef listNode* polynomial;
struct listNode
{
int coef;
int expon;
polynomial next;
};
/* 将新的项结点加入到现有项的末尾 */
void attach(int coef, int expon, polynomial& rear)
{
polynomial newNode = (polynomial)malloc(sizeof(listNode));
newNode->coef = coef;
newNode->expon = expon;
newNode->next = NULL;
rear->next = newNode;
rear = newNode;
}
/* 读取输出的多项式 */
polynomial readPoly()
{
int n, c, e;
polynomial head, rear, temp;
std::cin >> n; //输入多项式项数
head = (polynomial)malloc(sizeof(listNode));//分配一个空的头节点
head->next = NULL;
rear = head;
while (n--)
{
std::cin >> c >> e;
attach(c, e, rear);
}
temp = head;
head = head->next;
free(temp);
return head;
}
/* 多项式相加 */
polynomial add(polynomial P1, polynomial P2)
{
polynomial t1, t2, head, rear, temp;
t1 = P1;
t2 = P2;
head = (polynomial)malloc(sizeof(listNode));
head->next = NULL;
rear = head;
while (t1 && t2)
{
if (t1->expon > t2->expon)
{
attach(t1->coef, t1->expon, rear);
t1 = t1->next;
}
else if (t1->expon < t2->expon)
{
attach(t2->coef, t2->expon, rear);
t2 = t2->next;
}
else
{
int sum = t1->coef + t2->coef;
if (sum != 0) //处理同类项合并
{
attach(sum, t1->expon, rear);
}
t1 = t1->next;
t2 = t2->next;
}
}
/* 将剩余项数全部添加到多项式尾部 */
if (t1 == NULL)
{
while (t2)
{
attach(t2->coef, t2->expon, rear);
t2 = t2->next;
}
}
if (t2 == NULL)
{
while (t1)
{
attach(t1->coef, t1->expon, rear);
t1 = t1->next;
}
}
/* 删除一开始建立的空头结点 */
temp = head;
head = head->next;
free(temp);
return head;
}
polynomial mult(polynomial P1, polynomial P2)
{
polynomial t1, t2, head, rear, temp;
t1 = P1;
t2 = P2;
if (!t1 || !t2) return NULL;
head = (polynomial)malloc(sizeof(listNode));
head->next = NULL;
rear = head;
/* 先用t1的第一项乘以t2,建立一个多项式的基础*/
while (t2)
{
attach(t1->coef * t2->coef, t1->expon + t2->expon, rear);
t2 = t2->next;
}
t1 = t1->next;
/* 在上方基础上,将t1除第一项以外的项和t2的所有项分别相乘 */
while (t1)
{
t2 = P2;
rear = head;
while (t2)
{
int c = t1->coef * t2->coef;
int e = t1->expon + t2->expon;
//核心步骤:找准相乘后的项要插入的正确位置
while (rear->next && rear->next->expon > e) rear = rear->next;
if (rear->next && rear->next->expon == e)
{
if (rear->next->coef + c)
{
rear->next->coef += c;
}
else
{
temp = rear->next;
rear->next = temp->next;
free(temp);
}
}
else
{
temp = (polynomial)malloc(sizeof(listNode));
temp->coef = c;
temp->expon = e;
temp->next = rear->next;
rear->next = temp;
rear = rear->next;
}
t2 = t2->next;
}
t1 = t1->next;
}
t2 = head;
head = head->next;
free(t2);
return head;
}
void print(polynomial p)
{
int flag = 1;
if (!p) printf("0 0");
while (p)
{
if (!flag)
{
std::cout << ' ';
}
else flag = 0;
printf("%d %d", p->coef, p->expon);
p = p->next;
}
printf("\n");
}
int main()
{
polynomial P1, P2, PA, PM;
P1 = readPoly();
P2 = readPoly();
PM = mult(P1, P2);
print(PM);
PA = add(P1, P2);
print(PA);
return 0;
}