#include <iostream>
using namespace std;
typedef struct node {
int x;
int y;
node *next;
}dx;
dx *readPoly();
dx *mult(dx *p1, dx *p2);
void printAdd(dx *p1, dx *p2);
int main(int argc, char *argv[])
{
dx *p1 = readPoly();
dx *p2 = readPoly();
dx *mul = mult(p1, p2);
while (mul != NULL)
{
cout << mul->x << " " << mul->y;
if (mul->next != NULL) cout << " ";
mul = mul->next;
}
cout << endl;
printAdd(p1, p2);
return 0;
}
dx *readPoly()
{
int n;
cin >> n;
dx *p = new dx;
p->next = NULL;
dx *l = p;
for (int i = 0; i < n; i++)
{
l->next = new dx;
l = l->next;
cin >> l->x;
cin >> l->y;
l->next = NULL;
}
dx *p1 = p->next;
delete p;
return p1;
}
dx *mult(dx *p1, dx *p2)
{
dx *ret = new dx;
dx *rear = ret;
rear->y = 1000;
dx *tmp = ret;
while (p1 != NULL)
{
dx *p22 = p2;
while (p22 != NULL)
{
dx *pt = new dx;
pt->x = p1->x*p22->x;
pt->y = p1->y + p22->y;
pt->next = NULL;
rear->next = pt;
if (pt->y >= rear->y)
{
while (tmp->next->y > pt->y)
{
tmp = tmp->next;
}
if (tmp->next->y == pt->y)
{
tmp->next->x += pt->x;
rear->next = NULL;
delete pt;
tmp = ret;
p22 = p22->next;
continue;
}
pt->next = tmp->next;
tmp->next = pt;
tmp = ret;
rear->next = NULL;
p22 = p22->next;
continue;
}
rear = rear->next;
p22 = p22->next;
}
p1 = p1->next;
}
dx *ret1 = ret->next;
delete ret;
return ret1;
}
void printAdd(dx *p1, dx *p2)
{
while (p1 != NULL && p2 != NULL)
{
if (p1->y > p2->y)
{
cout << p1->x << " " << p1->y << " ";
p1 = p1->next;
}
else if (p1->y < p2->y)
{
cout << p2->x << " " << p2->y << " ";
p2 = p2->next;
}
else
{
cout << p1->x + p2->x << " " << p1->y << " ";
p1 = p1->next;
p2 = p2->next;
}
}
while (p1 != NULL)
{
cout << p1->x << " " << p1->y;
if (p1->next != NULL) cout << " ";
p1 = p1->next;
}
while (p2 != NULL)
{
cout << p2->x << " " << p2->y;
if (p2->next != NULL) cout << " ";
p2 = p2->next;
}
}
两个一元多项式的乘积与和
最新推荐文章于 2023-01-09 14:31:49 发布