2个回答
展开全部
刚好做过
#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;
}
减法的自己改改,很简单
#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;
}
减法的自己改改,很简单
展开全部
刚好做过
#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;
}
减法的自己改改,很简单
#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;
}
减法的自己改改,很简单
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询