如何在C++中用链表实现多项式的加减乘法

虽然问题很初级,但请各位大虾把完整的代码写出来!急用!... 虽然问题很初级,但请各位大虾把完整的代码写出来!急用! 展开
 我来答
允梦竹0C4
推荐于2016-07-02 · TA获得超过333个赞
知道答主
回答量:181
采纳率:0%
帮助的人:0
展开全部
刚好做过
#include<iostream>
using namespace std;
#include<stdlib.h>
typedef int ElemType;

struct Pnomial //Pnomial=Polynomial(多项式)
{
ElemType co,de1,de2,de3;
//co=coefficient(系数), de=degree(次数)

Pnomial* next;
};

Pnomial *ADD(Pnomial *ph);

void mul(Pnomial *ph,Pnomial *qh);

void main()
{
Pnomial *ph,*qh,*p,*s,*q;
//ph表头指针,p移动指针,q临时储存结点

ph=p=new Pnomial;
qh=q=new Pnomial;

cout<<"\nthe 1st Polynomial:"<<endl;
cout<<"give coefficient value:"<<endl;

while(p->co!=0)
{
s=new Pnomial;

cin>>s->co;
if(s->co!=0)
{
cout<<"x^";
cin>>s->de1;
cout<<"y^";
cin>>s->de2;
cout<<"z^";
cin>>s->de3;
cout<<"+";
}

p->next=s;
p=s;
}

cout<<"\nthe 1st Polynomial end."<<endl;
cout<<"\nthe 2nd Polynomial:"<<endl;
cout<<"give coefficient value:"<<endl;

do
{
s=new Pnomial;

cin>>s->co;
if(s->co!=0)
{
cout<<"x^";
cin>>s->de1;
cout<<"y^";
cin>>s->de2;
cout<<"z^";
cin>>s->de3;
cout<<"+";
}

q->next=s;
q=s;
} while(q->co!=0);

cout<<"the 2nd Polynomial end."<<endl;

p->next=NULL;
p=ph->next;
q->next=NULL;
q=qh->next;

cout<<'\n'<<endl;
cout<<"the 1st Polynomial:"<<endl;

while(p->next!=NULL)
{

if(p!=ph->next)
cout<<" + ";

if(p->co!=1)
cout<<p->co;

if(p->de1!=0)
{
cout<<"x";
if(p->de1!=1)
cout<<"^"<<p->de1;
}
if(p->de2!=0)
{
cout<<"y";
if(p->de2!=1)
cout<<"^"<<p->de2;
}
if(p->de3!=0)
{
cout<<"z";
if(p->de3!=1)
cout<<"^"<<p->de3;
}
p=p->next;
}
cout<<"\nthe 1st Polynomial end."<<endl;
cout<<"\nthe 2nd Polynomial:"<<endl;
while(q->next!=NULL)
{

if(q!=qh->next)
cout<<" + ";

if(q->co!=1)
cout<<q->co;

if(q->de1!=0)
{
cout<<"x";
if(q->de1!=1)
cout<<"^"<<q->de1;
}
if(q->de2!=0)
{
cout<<"y";
if(q->de2!=1)
cout<<"^"<<q->de2;
}
if(q->de3!=0)
{
cout<<"z";
if(q->de3!=1)
cout<<"^"<<q->de3;
}
q=q->next;
}
cout<<"\nthe 2nd Polynomial end."<<endl;
cout<<'\n'<<endl;
mul(ph,qh);
}

void mul(Pnomial *ph,Pnomial *qh)
{

Pnomial *p,*q;

Pnomial *re,*di,*temp; //新建一个链表储存结果
//re=result(结果), di=displace(移动指针)

int counter=0;
//计数变量,记录p赋值给temp的起始结点

re=di=new Pnomial;
for(p=ph->next;p->next!=NULL;p=p->next)
{
for(q=qh->next;q->next!=NULL;q=q->next)
{
temp=new Pnomial;
temp->co=p->co*q->co;
temp->de1=p->de1+q->de1;
temp->de2=p->de2+q->de2;
temp->de3=p->de3+q->de3;
di->next=temp;
di=temp;
if(p==ph->next&&q==qh->next)
re=di;
}
}
di->next=NULL;
cout<<"the result Polynomial:"<<endl;
di=ADD(re);
//di回到表头结点,准备打印结果多项式
// di=re->next;//di回到表头结点,准备整理多项式
re=di;

while(di!=NULL)
{
if(di!=re)
cout<<" + ";

if(di->co!=1)
cout<<di->co;

if(di->de1!=0)
{
cout<<"x";
if(di->de1!=1)
cout<<"^"<<di->de1;
}
if(di->de2!=0)
{
cout<<"y";
if(di->de2!=1)
cout<<"^"<<di->de2;
}
if(di->de3!=0)
{
cout<<"z";
if(di->de3!=1)
cout<<"^"<<di->de3;
}
di=di->next;

}
}

Pnomial *ADD(Pnomial *ph)
{

Pnomial *re,*p,*q;
p=ph;
q=p->next;
Pnomial *di,*temp; //新建一个链表储存结果
//re=result(结果), di=displace(移动指针)

re=di=new Pnomial;

while(p!=NULL)
{
temp=new Pnomial;
temp->co=p->co;
temp->de1=p->de1;
temp->de2=p->de2;
temp->de3=p->de3;

for(;q!=NULL;)
{
if(p->de1==q->de1&&p->de2==q->de2&&p->de3==q->de3)
{
temp->co+=q->co;
p->next=q->next;
delete q;
q=p->next;
}
else
q=q->next;
}

di->next=temp;
di=temp;
if(p==ph)
re=di;
p=p->next;
if(p!=NULL)
q=p->next;
}
di->next=NULL;
return re;

}

减法的自己改改,很简单
陶舟牵振博
2019-07-08 · TA获得超过3717个赞
知道大有可为答主
回答量:3080
采纳率:31%
帮助的人:203万
展开全部
刚好做过
#include<iostream>
using
namespace
std;
#include<stdlib.h>
typedef
int
ElemType;
struct
Pnomial
//Pnomial=Polynomial(多项式)
{
ElemType
co,de1,de2,de3;
//co=coefficient(系数),
de=degree(次数)
Pnomial*
next;
};
Pnomial
*ADD(Pnomial
*ph);
void
mul(Pnomial
*ph,Pnomial
*qh);
void
main()
{
Pnomial
*ph,*qh,*p,*s,*q;
//ph表头指针,p移动指针,q临时储存结点
ph=p=new
Pnomial;
qh=q=new
Pnomial;
cout<<"\nthe
1st
Polynomial:"<<endl;
cout<<"give
coefficient
value:"<<endl;
while(p->co!=0)
{
s=new
Pnomial;
cin>>s->co;
if(s->co!=0)
{
cout<<"x^";
cin>>s->de1;
cout<<"y^";
cin>>s->de2;
cout<<"z^";
cin>>s->de3;
cout<<"+";
}
p->next=s;
p=s;
}
cout<<"\nthe
1st
Polynomial
end."<<endl;
cout<<"\nthe
2nd
Polynomial:"<<endl;
cout<<"give
coefficient
value:"<<endl;
do
{
s=new
Pnomial;
cin>>s->co;
if(s->co!=0)
{
cout<<"x^";
cin>>s->de1;
cout<<"y^";
cin>>s->de2;
cout<<"z^";
cin>>s->de3;
cout<<"+";
}
q->next=s;
q=s;
}
while(q->co!=0);
cout<<"the
2nd
Polynomial
end."<<endl;
p->next=NULL;
p=ph->next;
q->next=NULL;
q=qh->next;
cout<<'\n'<<endl;
cout<<"the
1st
Polynomial:"<<endl;
while(p->next!=NULL)
{
if(p!=ph->next)
cout<<"
+
";
if(p->co!=1)
cout<<p->co;
if(p->de1!=0)
{
cout<<"x";
if(p->de1!=1)
cout<<"^"<<p->de1;
}
if(p->de2!=0)
{
cout<<"y";
if(p->de2!=1)
cout<<"^"<<p->de2;
}
if(p->de3!=0)
{
cout<<"z";
if(p->de3!=1)
cout<<"^"<<p->de3;
}
p=p->next;
}
cout<<"\nthe
1st
Polynomial
end."<<endl;
cout<<"\nthe
2nd
Polynomial:"<<endl;
while(q->next!=NULL)
{
if(q!=qh->next)
cout<<"
+
";
if(q->co!=1)
cout<<q->co;
if(q->de1!=0)
{
cout<<"x";
if(q->de1!=1)
cout<<"^"<<q->de1;
}
if(q->de2!=0)
{
cout<<"y";
if(q->de2!=1)
cout<<"^"<<q->de2;
}
if(q->de3!=0)
{
cout<<"z";
if(q->de3!=1)
cout<<"^"<<q->de3;
}
q=q->next;
}
cout<<"\nthe
2nd
Polynomial
end."<<endl;
cout<<'\n'<<endl;
mul(ph,qh);
}
void
mul(Pnomial
*ph,Pnomial
*qh)
{
Pnomial
*p,*q;
Pnomial
*re,*di,*temp;
//新建一个链表储存结果
//re=result(结果),
di=displace(移动指针)
int
counter=0;
//计数变量,记录p赋值给temp的起始结点
re=di=new
Pnomial;
for(p=ph->next;p->next!=NULL;p=p->next)
{
for(q=qh->next;q->next!=NULL;q=q->next)
{
temp=new
Pnomial;
temp->co=p->co*q->co;
temp->de1=p->de1+q->de1;
temp->de2=p->de2+q->de2;
temp->de3=p->de3+q->de3;
di->next=temp;
di=temp;
if(p==ph->next&&q==qh->next)
re=di;
}
}
di->next=NULL;
cout<<"the
result
Polynomial:"<<endl;
di=ADD(re);
//di回到表头结点,准备打印结果多项式
//
di=re->next;//di回到表头结点,准备整理多项式
re=di;
while(di!=NULL)
{
if(di!=re)
cout<<"
+
";
if(di->co!=1)
cout<<di->co;
if(di->de1!=0)
{
cout<<"x";
if(di->de1!=1)
cout<<"^"<<di->de1;
}
if(di->de2!=0)
{
cout<<"y";
if(di->de2!=1)
cout<<"^"<<di->de2;
}
if(di->de3!=0)
{
cout<<"z";
if(di->de3!=1)
cout<<"^"<<di->de3;
}
di=di->next;
}
}
Pnomial
*ADD(Pnomial
*ph)
{
Pnomial
*re,*p,*q;
p=ph;
q=p->next;
Pnomial
*di,*temp;
//新建一个链表储存结果
//re=result(结果),
di=displace(移动指针)
re=di=new
Pnomial;
while(p!=NULL)
{
temp=new
Pnomial;
temp->co=p->co;
temp->de1=p->de1;
temp->de2=p->de2;
temp->de3=p->de3;
for(;q!=NULL;)
{
if(p->de1==q->de1&&p->de2==q->de2&&p->de3==q->de3)
{
temp->co+=q->co;
p->next=q->next;
delete
q;
q=p->next;
}
else
q=q->next;
}
di->next=temp;
di=temp;
if(p==ph)
re=di;
p=p->next;
if(p!=NULL)
q=p->next;
}
di->next=NULL;
return
re;
}
减法的自己改改,很简单
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式