设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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<stdio.h>
#include"stdlib.h"
using namespace std;
struct node{
int pre;
int pow;
node *next;
};
struct Fomula{
int n;
node *head;
};
void CreateFomula(Fomula* f){
f->head = (node*)malloc(sizeof(node));
node* p = f->head;
p->next = NULL;
for(int i=0; i<f->n; i++){
p->next = (node*)malloc(sizeof(node));
p = p->next;
p->next = NULL;
cin >> p->pre;
cin >> p->pow;
}
}
void listNodes(Fomula* fomu){
if(fomu->n == 0){
cout <<0<<" "<< 0;
}else{
node* p = fomu->head;
for(int i=0; i<fomu->n-1; i++){
p = p->next;
cout<<p->pre <<" "<<p->pow<<" ";
}
p = p->next;
cout<<p->pre <<" "<<p->pow;
}
}
//Get the result of sum up
void addUpFomu(Fomula *addup, Fomula *a, Fomula *b){
node* ap = a->head;
node* bp = b->head;
addup->head = (node*)malloc(sizeof(node));
node *p = addup->head;
int n=0;
while(ap->next!=NULL || bp->next!=NULL){
// every node have its memory
p ->next = (node*)malloc(sizeof(node));
p = p->next;
p->next = NULL;
n+=1;
// two list have its own node
if(ap->next!=NULL && bp->next!=NULL){
if(ap->next->pow == bp->next->pow){
int aa = ap->next->pre;
int bb = bp->next->pre;
int all = aa + bb;
p->pow = ap->next->pow;
p->pre = all;
ap = ap->next;
bp = bp->next;
}else if(ap->next->pow > bp->next->pow){
p->pow = ap->next->pow;
p->pre = ap->next->pre;
ap = ap->next;
}else if(ap->next->pow < bp->next->pow){
p->pow = bp->next->pow;
p->pre = bp->next->pre;
bp = bp->next;
}
}else if(ap->next!=NULL){
p->pow = ap->next->pow;
p->pre = ap->next->pre;
ap = ap->next;
}else if(bp->next!=NULL){
p->pow = bp->next->pow;
p->pre = bp->next->pre;
bp = bp->next;
}
}
addup->n = n;
}
//Get the result of multiply
void multiply(Fomula *multi, Fomula *a, Fomula *b){
node* ap = a->head;
node* bp = b->head;
multi->head = (node*)malloc(sizeof(node));
node *p = multi->head;
p->next = NULL;
for(int i=0;i<a->n;i++){
ap = ap->next;
bp = b->head;
for(int j=0;j<b->n;j++){
bp = bp->next;
int pow = ap->pow + bp->pow;
int pre = ap->pre * bp->pre;
//cout << pow << " " << pre << endl;
while(p->next != NULL){
if(p->next->pow == pow){
p->next->pre += pre;
break;
}else if(p->next->pow > pow){
p = p->next;
}else if(p->next->pow < pow){
node* p2 = p->next;
node *nn = (node*)malloc(sizeof(node));
nn->next = p2;
p->next = nn;
nn->pow = pow;
nn->pre = pre;
break;
}
}
if(p->next == NULL){
node *n = (node*)malloc(sizeof(node));
n->pow = pow;
n->pre = pre;
p->next = n;
n->next = NULL;
}
p = multi->head;
}
}
int n=0;
while(p->next!=NULL){
p = p->next;
n++;
}
multi->n = n;
}
void clearList(Fomula* f){
int n = f->n;
node *p = f->head;
while(p->next != NULL){
if(p->next->pre == 0){
node *mid = p->next;
p->next = mid->next;
free(mid);
f->n -= 1;
}else{
p = p->next;
}
}
}
int main()
{
Fomula *a = (Fomula*)malloc(sizeof(Fomula));
Fomula *b = (Fomula*)malloc(sizeof(Fomula));
cin >> a->n;
CreateFomula(a);
cin >> b->n;
CreateFomula(b);
Fomula *addup = (Fomula*)malloc(sizeof(Fomula));
Fomula *mixup = (Fomula*)malloc(sizeof(Fomula));
addUpFomu(addup, a, b);
multiply(mixup, a, b);
clearList(addup);
clearList(mixup);
listNodes(mixup);
cout << endl;
listNodes(addup);
return 0;
}