问题描述
- 问各位一下,VC6.0会因为代码执行的时间过长,而终止运行
-
如下程序:
#include
#include
struct PolyNode
{
float coef;
int exp;
PolyNode * next;
};
//template
class PolyLinkList
{
public:
PolyLinkList(float a[],int n=0);
~PolyLinkList();
void ShowList();
void PolyAdd(PolyLinkList & LB);
private:
PolyNode * head;
};
//---------------------------------------------------------------------------
//template
PolyLinkList::PolyLinkList(float a[],int n)
{
int i=0;
PolyNode * q=head;
for(i=0;i
{
PolyNode * s=new PolyNode;
s->coef=a[i];
s->exp=i;
q->next=s;
q=q->next;
}
}
//template
PolyLinkList::~PolyLinkList()
{
PolyNode * p=head;
while(p->next)
{
PolyNode * q=p;
p=p->next;
delete q;
}
}
//template
void PolyLinkList::ShowList()
{
PolyNode * p=head;
while(p->next!=NULL)
{
cout<next->coef<<"*E"<next->exp;
if(p->next->next!=NULL)
cout<<"+";
//cout<
p=p->next;
}
cout<
}
//template
void PolyLinkList::PolyAdd(PolyLinkList & LB)
{
float sum;
PolyNode * pa,* pb,* qa,* qb;
pa=head;
qa=pa->next;
pb=LB.head;
qb=pb->next;
while(qa!=NULL&&qb!=NULL)
{
if(qa->expexp)
{
pa=qa;
qa=qa->next;
}
else if(qa->exp>qb->exp)
{
pb->next=qb->next;
qb->next=qa;
pa->next=qb;
pa=qb;
qb=pb->next;
}
else
{
sum=qa->coef+qb->coef;
if(sum==0)
{
pa->next=qa->next;
delete qa;
pb->next=qb->next;
delete qb;
qb=pb->next;
}
else
{
qa->coef=sum;
pa=qa;
qa=qa->next;
pb->next=qb->next;
delete qb;
qb=pb->next;
}
}
}
if(qb!=NULL)
qa->next=qb;
}
/*void PolyLinkList::ShowList()
{
PolyNode * p=head;
while(p->next)
{
cout<next->coef<<"*E"<next->exp;
if(!(p->next->next))
cout<<"+";
}
}*/
//-------------------------------------------------------------------------------------
void main()
{
float a[5]={1.0,2.0,3.0,4.0,5.0};
PolyLinkList str(a,5);
str.ShowList();
float b[5]={6.0,7.0,8.0,9.0,0};
PolyLinkList str1(b,5);
str.PolyAdd(str1);
str.ShowList();
system("pause");
}
解决方案
这个代码可能有问题的,你得调试一下
时间: 2024-09-20 14:40:09