baidu_28495261 于 2015.05.26 12:45 提问

c++一元多项式求和问题求大神解答

struct elem
{
int data;
int exp;
elem* next;
};
{
public:
void PrintList();
private:
elem * first;
};
#endif

#include
using namespace std;
{
first=new elem;
first->next=NULL;
}
{
first=new elem;
elem*r=first;
for(int i=0;i {
elem*s=new elem;
s->data=a[i][0];
s->exp=a[i][1];
r->next=s;
r=s;
}
r->next=NULL;
}
{
while(first!=NULL)
{
elem *q=first;

first=first->next;

delete q;
}
}
{
elem*p=first->next;
while(p!=NULL)
{
cout<data<<"*X^"<exp<<"+";
p=p->next;
}
}

#include
using namespace std;
void main()
{
int a[][2]={{2,4},{3,5},{4,8}};
int b[][2]={{8,1},{7,2},{3,4},{5,8}};
cout<<"多项式A为："< A.PrintList();
cout B.PrintList();
cout cout try{
}
catch(char *wrong)
{
cout }
}
{
elem*pre=A.first;elem*p=pre->next;
elem*qre=B.first;elem*q=qre->next;
while(p!=NULL&&q!=NULL)
{
if(p->expexp) //p的指数小于q的指数
{
pre=p;
p=p->next;
}
else if(p->exp>q->exp)//p的指数大于q的指数
{
elem*v=q->next;
pre->next=q;
q->next=p;
q=v;
}
else //指数相等
{
p->data=p->data+q->data;
if(p->data==0) //如果相加后p的系数为0
{
pre->next=p->next;
delete p;
p=p->next;
}
else
{
pre=p;
p=pre->next;
}
qre->next=q->next;
delete q;
q=qre->next;
}
}
if(q!=NULL)
pre->next;
delete B.first; //释放第二个单链表头结点所占的内存；
A.PrintList();
}

3个回答

wsh1992o   2015.05.26 14:11

monaso   2015.05.26 12:59

tianyang2008   2015.05.26 13:27