7-2 一元多项式的乘法与加法运算 (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 <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ListNode
{
int value;
int key;
struct ListNode *next;
};
typedef struct ListNode *List;
List CreatList(int n);
List AddList(List list1, List list2);
List MulList(List list1, List list2);
List order(List list);
List toghter(List list);
int main()
{
int n = 0, m = 0;
scanf("%d", &n);
List list1 = CreatList(n);//第一个表达式
scanf("%d", &m);
List list2 = CreatList(m);//第二个表达式
List mulList = MulList(list1->next, list2->next);//两个表达式相乘
mulList = mulList->next;
if (!mulList) printf("0 0\n");
else {
if (mulList->value != 0) printf("%d %d", mulList->value, mulList->key);
while (mulList->next) {
if (mulList->next->value != 0) printf(" %d %d", mulList->next->value, mulList->next->key);
mulList = mulList->next;
}
printf("\n");
}
List addList = AddList(list1->next, list2->next); //两个表达式相加
addList = addList->next;
if (!addList) printf("0 0\n");
else {
if (addList->value != 0) printf("%d %d", addList->value, addList->key);
while (addList->next) {
if (addList->next->value != 0) printf(" %d %d", addList->next->value, addList->next->key);
addList = addList->next;
}
}
return 0;
}
List CreatList(int n)//表达式的存储
{
List head = (List) malloc (sizeof (struct ListNode)), tail = head;
head->next = NULL;
int i = 0, value = 0, key = 0;
for (i = 0; i < n; i++)
{
scanf("%d %d", &value, &key);
List node = (List) malloc (sizeof (struct ListNode));
node->value = value;
node->key = key;
node->next = tail->next;
tail->next = node;
tail = node;
}
return head;
}
List AddList(List list1, List list2)//表达式相加
{
List head = (List) malloc (sizeof (struct ListNode)), tail = head;
head->next = NULL;
while (list1 && list2) {
int value = 0, key = 0;
if (list1->key == list2->key) {
value = list1->value + list2->value;
key = list1->key;
list1 = list1->next;
list2 = list2->next;
}
else if (list1->key < list2->key) {
value = list2->value;
key = list2->key;
list2 = list2->next;
}
else if (list1->key > list2->key) {
value = list1->value;
key = list1->key;
list1 = list1->next;
}
if (value != 0) {
List node = (List) malloc (sizeof (struct ListNode));
node->key = key;
node->value = value;
node->next = tail->next;
tail->next = node;
tail = node;
}
}
if (list1) tail->next = list1;
if (list2) tail->next = list2;
return head;
}
List MulList(List list1, List list2)//表达式相乘
{
List head = (List) malloc (sizeof (struct ListNode)), tail = head;
head->next = NULL;
while (list1) {
int value = 0, key = 0;
List l2 = list2;
while (l2) {
value = list1->value * l2->value;
key = list1->key + l2->key;
if (value != 0) {
List node = (List) malloc (sizeof (struct ListNode));
node->value = value;
node->key = key;
node->next = tail->next;
tail->next = node;
tail = node;
}
l2 = l2->next;
}
list1 = list1->next;
}
if (head->next) head = order(toghter(head));
return head;
}
List toghter(List list)
{
List l1 = list, l2 = list->next;
while (l1->next) {
l2 = l1->next;
while (l2->next) {
if (l1->next->key == l2->next->key) {
l1->next->value += l2->next->value;
l2->next = l2->next->next;
}
else l2 = l2->next;
}
l1 = l1->next;
}
return list;
}
List order(List list)
{
List l1 = list->next, l2 = l1->next;
while (l1) {
l2 = l1->next;
while (l2) {
if (l1->key < l2->key) {
int value = 0, key = 0;
value = l2->value;
l2->value = l1->value;
l1->value = value;
key = l2->key;
l2->key = l1->key;
l1->key = key;
}
l2 = l2->next;
}
l1 = l1->next;
}
return list;
}