Board logo

标题: 我写了一段源代码,是关于一元多项式相加的,希望大家能帮我看看 [打印本页]

作者: sophie8910    时间: 2009-3-29 21:04     标题: 我写了一段源代码,是关于一元多项式相加的,希望大家能帮我看看

#include< iostream.h >
struct TLinkNode
{
int info;
TLinkNode *next;
};
class TLinearListLink
{
protected:
   TLinkNode *head;

public:
           TLinearListLink(void);
        void output(TLinkNode *pNode )const;


};
void TLinearListLink ::output(TLinkNode *pNode) const
{
while (pNode != NULL)
{
cout<<pNode->info <<" ";
pNode = pNode->next;
}
cout<<"/n";
}
long TPolynomialAdd(TLinearListLink<TPolynomialItem>&a,TLinearListLink<TPolynomialItem>&b)
{

  TLinkNode <TPolynomialItem> *p,*q,*p0,*q0;
  int k=0;
  float x;
  p = a.head;
  q = b.head;
  while( P!=NULL&&q!=NULL )
  {
   if(p->exp>q->exp)
   {
    q0 = q;
        q = q->next;
        q0->next = p;
        k++;
   }
   else
           if( p->exp < q->exp)
           {
           p = p->next;
           k++;
           }
           else
           {
           p->coef = p->coef + q->coef;
           if(p->coef == 0)
           {
           p0 = p;
           p = p->next;
           delete p0;
           }
           else
           {
            p = p->next;
           }
           q0=q; q = q->next;
           delete q0;
           }
           if(q!=NULL)
       
           p->next = q;
           b.head = NULL;
           cout<<" At last k is :"<<k<<endl;
           a.output();
          

  }
}
struct TPolynomialItem
{
float coef;
int exp;

};

/*intream& operator >>( ostream &o,TPolynomialItem &a)
{
o>>a.coef >>",">>a.exp ;
return o;
}
*/
void main()
{
  TLinearListLink <TPolynomialItem>&c,&d;
  cout<<" Please input a polynomial c :"<<endl;
  cin>>c;
  cout<<"Please input a polynomial d :"<<endl;
  cin>>d;

  cout<<" After adding c become to :"<< TPolynomialAdd(c,d)<<endl;



}
我改过来改过去总有点错误,我调试不出来,希望大家能帮帮我~~

真的很急!!!!1




欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/) Powered by Discuz! 6.0.0