先合并同类项,然后求值;

2025-05-24 07:57:00
推荐回答(2个)
回答(1):

#include
using namespace std;
#include
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:"< cout<<"give coefficient value:"<
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."< cout<<"\nthe 2nd Polynomial:"< cout<<"give coefficient value:"<
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."<
p->next=NULL;
p=ph->next;
q->next=NULL;
q=qh->next;

cout<<'\n'< cout<<"the 1st Polynomial:"<
while(p->next!=NULL)
{

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

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

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

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

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

if(q->de1!=0)
{
cout<<"x";
if(q->de1!=1)
cout<<"^"<de1;
}
if(q->de2!=0)
{
cout<<"y";
if(q->de2!=1)
cout<<"^"<de2;
}
if(q->de3!=0)
{
cout<<"z";
if(q->de3!=1)
cout<<"^"<de3;
}
q=q->next;
}
cout<<"\nthe 2nd Polynomial end."< cout<<'\n'< 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:"< di=ADD(re);
//di回到表头结点,准备打印结果多项式
// di=re->next;//di回到表头结点,准备整理多项式
re=di;

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

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

if(di->de1!=0)
{
cout<<"x";
if(di->de1!=1)
cout<<"^"<de1;
}
if(di->de2!=0)
{
cout<<"y";
if(di->de2!=1)
cout<<"^"<de2;
}
if(di->de3!=0)
{
cout<<"z";
if(di->de3!=1)
cout<<"^"<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;

}

多项式乘法,里面有你想要的,自己看看~

回答(2):

不好意思!
不过我看不到有啥内情!!
既然你这样说我!
我就帮你解解吧!
X的2平方 应该是 X的2次方吧!则

(1)7x^2 - 2x - 4x^2 + 3x - x + x^2
= 7x^2 - 4x^2 + x^2 + 3x - 2x - x
= 4x^2 + 0
∵x=(-1/2)
∴4x^2 = 4 * (-1/2)^2 = 1

(1)x^2 - 2xy + y^2 + 3xy - x^2 + y^2
= x^2 - x^2 + y^2 + y^2 + 3xy - 2xy
= 2y^2 + xy
我不明白你的问题!
啥是-AB的系数啊!若按标准二元一次式
ax^2 + bx + c = 0 来说!
那么关于Y的方程 2y^2 + xy (其实不能说是方程!没有等号!)的-AB就是 -2x
次数是2 ,二次项系数为2!

打这些数真烦!