2 u010940902 u010940902 于 2013.06.10 21:43 提问

问各位一下,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");
}

1个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.15 23:53

这个代码可能有问题的,你得调试一下

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!