###### 多项式乘法与加法运算（链表实现）
#include<iostream>
using namespace std;
typedef struct PolyNode *Polynomial;
struct PolyNode
{
int coef;
int expon;
};

void Attach(int c, int e, Polynomial *pRear){
Polynomial P;//这里有点小问题，按理说应该动态申请一块空间，可是加上P=new PolyNode后编译错误...也没太弄懂
P->coef = c;
P->expon = e;
(*pRear) = P;
}

int N,c,e;
Polynomial Rear, P,t;
cin >> N;
P = new PolyNode;
Rear = P;
while (N--){
cin >> c >> e;
Attach(c, e, &Rear);
}
t = P; P = P->link; delete t;//删除临时生成的头结点
return P;
}

Polynomial t1, t2, Rear, P;
t1 = P1; t2 = P2;
P = new PolyNode;
Rear = P;
while (t1&&t2){
if (t1->expon > t2->expon){
Attach(t1->coef, t1->expon, &Rear);
}
if (t1->expon < t2->expon){
Attach(t1->coef, t2->expon, &Rear);
}
if (t1->expon == t2->expon){
if (t1->expon + t2->expon){
Attach(t1->coef + t2->coef, t1->expon, &Rear);
}
}
}
return P;
}
Polynomial Mult(Polynomial P1, Polynomial P2){
Polynomial t1, t2, Rear, P,t;
int c, e;
if (!P1 || !P2) return NULL;
t1 = P1; t2 = P2;
P = new PolyNode;
Rear = P;
while (t2){
Attach(t1->coef*t2->coef, t1->expon + t2->expon, &Rear);
}
while (t1){
t2 = P2; Rear = P;
while (t2){
e = t1->expon + t2->expon;
c = t1->coef*t2->coef;
else{
}
}
else{
t = new PolyNode;
t->coef = c; t->expon = e;
}
}
}
return P;
}

void PrintPoly(Polynomial P){
int flag = 0;
if (!P) cout << 0 << 0 << endl;
while (P){
if (!flag) flag = 1;
else
cout << " ";
cout << P->coef << P->expon;
}
}
int main(){
Polynomial P1, P2, PP, PS;
PrintPoly(PP);
PS = Mult(P1, P2);
PrintPoly(PS);

return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef struct Term{
int coe;//系数
int exp;//指数
}Term;

bool compare(Term a,Term b)
{
return a.exp>b.exp;
}

/*
Term Mul(Term a,Term b)
{
a.coe = a.coe*b.coe;
a.exp = a.exp+b.exp;
return a;
}
*/

/*
{
a.coe +=b.coe;
return a;
}*/
void print(Term a)
{
cout<<a.coe<<" "<<a.exp<<" ";
}
int main()
{

vector<Term>    vec1;
vector<Term>    vec2;
vector<Term>    vec;
vector<Term>::iterator  it1,it2;

int N1,N2;
int coe,exp;
bool  flag = false;
Term t;
cin>>N1;
while(N1--)
{
cin>>coe>>exp;
t.coe = coe;
t.exp = exp;
vec1.push_back(t);
}
//for_each(vec1.begin(),vec1.end(),print);
//system("pause");

cin>>N2;
while(N2--)
{
cin>>coe>>exp;
t.coe = coe;
t.exp = exp;
vec2.push_back(t);
}

//for_each(vec2.begin(),vec2.end(),print);
//system("pause");

for (it1=vec1.begin();it1!=vec1.end();it1++)
for (it2=vec2.begin();it2!=vec2.end();it2++)
{
t.coe = it1->coe * it2->coe;
t.exp = it1->exp + it2->exp;
if (t.coe!=0)
vec.push_back(t);

}

sort(vec.begin(),vec.end(),compare);

for (it1 = vec.begin();it1!=vec.end();it1=it2)
{
for (it2 = it1+1;it2!=vec.end() &&it1->exp==it2->exp;it2++)
it1->coe += it2->coe;
if (it1->coe!=0)
{
if (flag)
cout<<" ";
else
flag = true;
cout<<it1->coe<<" "<<it1->exp;
}
}
if (vec1.size()==0)
{
cout<<"0 0";
}
cout<<endl;
flag = false;

for (it1=vec1.begin(),it2=vec2.begin();it1!=vec1.end()&&it2!=vec2.end();)
{
if (it1->exp >it2->exp)
{
if (flag)
cout<<" ";
else
flag = true;
cout<<it1->coe<<" "<<it1->exp;
it1++;
}
else if (it1->exp < it2->exp)
{
if (flag)
cout<<" ";
else
flag = true;
cout<<it2->coe<<" "<<it2->exp;
it2++;
}
else
{

if ((it1->coe+it2->coe))
{
if (flag)
cout<<" ";
else
flag = true;
cout<<it1->coe+it2->coe<<" "<<it1->exp;
}

it1++;
it2++;
}
}

if (it1==vec1.end())
{
while(it2!=vec2.end())
{
if (flag)
cout<<" ";
else
flag = true;
cout<<it2->coe<<" "<<it2->exp;
it2++;
}
}

if (it2==vec2.end())
{
while(it1!=vec1.end())
{
if (flag)
cout<<" ";
else
flag = true;
cout<<it1->coe<<" "<<it1->exp;
it1++;
}
}

if (flag==false)
cout<<"0 0";
cout<<endl;

//for_each(vec.begin(),vec.end(),print);

//system("pause");
return 0;
}

#### 一元多项式的加法、减法、乘法

2014年06月17日 6KB 下载

#### 用链表实现一元多项式的加法运算

2016-02-26 19:13:11

#### PAT 一元多项式的乘法与加法运算(链表 c++版)

2015-09-09 22:45:30

#### 链表的应用：单元多项式的加法、减法、乘法

2014-06-17 10:42:11

#### PTA 一元多项式的乘法与加法运算

2015-11-08 21:49:32

#### 【数据结构】用链表实现多项式运算

2016-08-08 12:31:06

#### 用链表实现一元多项式的加、减、乘、求导运算

2015-09-09 15:27:54

#### 一元多项式加法与乘法运算链表实现

2017-01-29 01:56:36

#### 一元多项式的乘法与加法运算【Java实现--通过单项链表实现队列】

2015-10-06 17:17:59

#### c语言单链表实现多项式计算

2016-08-09 20:21:52