设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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>
using namespace std;
struct Polynode
{
int coef;
int exp;
Polynode *next;
};
typedef struct Polynode* Polynomial;
void attach(int c, int e, Polynomial *real)//real为指针Polynomial的指针,用指针作为形参可以双向传递
{
if (c) {
Polynomial p;
p = new Polynode();
p->coef = c;
p->exp = e;
p->next = NULL;//每次把新的一项赋给新建立的p,然后把下一项为空
(*real)->next = p;
*real = p;
}
}
Polynomial Readpoly()
{
Polynomial head,r,temp;
int c,e,N;
cin >> N;
head = new Polynode(); //head为一个空节点
head->next = NULL;
r = head;//尾节点
//read
while (N--)
{
cin >> c >> e;
attach(c, e, &r);
}
temp = head; head = head->next;//最后将空节点删除
delete temp;
return head;//返回的是第一个节点 这样后面比较的时候可以直接开始比,然后逐项next
}
Polynomial Add(Polynomial P1, Polynomial P2)
{
Polynomial head, real, temp, t1, t2;
t1 = P1, t2 = P2;
head = new Polynode();//head为空的,里面没有值
head->next = NULL;
real = head;
while (t1 && t2)
{
if (t1->exp == t2->exp)
{
int sum = t1->coef + t2->coef;
if (sum)
attach(sum, t1->exp, &real);
t1 = t1->next;
t2 = t2->next;
}
else if (t1->exp > t2->exp)
{
attach(t1->coef, t1->exp, &real);
t1 = t1->next;
}
else
{
attach(t2->coef, t2->exp, &real);
t2 = t2->next;
}
}
for (; t1; t1 = t1->next) attach(t1->coef, t1->exp, &real);
for (; t2; t2 = t2->next) attach(t2->coef, t2->exp, &real);
real->next = NULL;//real是指向结果的最后一项,加完了
temp = head; head = head->next; delete temp;//释放空节点
return head;
}
Polynomial Mult(Polynomial P1, Polynomial P2)
{
Polynomial head, t1, t2, temp, real;
int e, c;
if (!P1 || !P2) return NULL;
head = new Polynode(); // 后面要删掉这个空节点
head->next = NULL;
real = head;
t1 = P1; t2 = P2;
while (t2) //P1第一项 逐项乘以 P2每一项 得出一个序列
{
attach(t1->coef * t2->coef, t1->exp + t2->exp, &real);
t2 = t2->next;
}
t1 = t1->next;//从t1的第二个节点开始
while (t1)//t1的每一项乘以t2的每一项 这里每次的结果要插入到上面得出的递减序列中去
{
t2 = P2; real = head;//重新初始化
while (t2)
{
c = t1->coef * t2->coef; e = t1->exp + t2->exp;
while (real->next != NULL && real->next->exp > e)
{
real = real->next;
}
if (real->next != NULL && real->next->exp == e)//合并
{
if (real->next->coef + c)//系数相加不等于0
real->next->coef += c;
else//等于0 删掉这个节点 也就是real->next 检测点2 同类项合并时有抵消
{
temp = real->next; real->next = temp->next; delete temp;
}
}
else //插入操作,需要申请新节点
{
temp = new Polynode();
temp->coef = c; temp->exp = e;
temp->next = real->next;
real->next = temp;
real = real->next;
}
t2 = t2->next;//继续算t2中下一项
}
t1 = t1->next;
}
temp = head; head = head->next; delete temp;
return head;
}
void Printpoly(Polynomial P)//输出 遍历链表
{
int flag = 0;//最后一项不输出空格 将输出格式视为 空格 系数 指数
if (!P)
{
cout << "0 0" << endl; return;
}
while (P)//第一项不输出空格 设计flag判断是否为第一项
{
if (!flag)
flag = 1;
else cout << " ";
cout << P->coef << " " << P->exp;
P = P->next;
}
cout << endl;
}
int main()
{
Polynomial P1, P2, p_add, p_mult;
P1 = Readpoly();
P2 = Readpoly();
p_mult = Mult(P1, P2);
Printpoly(p_mult);
p_add = Add(P1, P2);
Printpoly(p_add);
return 0;
}