首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

哪出的有关问题呢

2013-07-08 
哪出的问题呢?题目: 用链表表示多项式,并且实现求两多项式的和; 例如:3x^4-6x^2 +5x - 10//多项式1+2x^3 +

哪出的问题呢?

题目: 用链表表示多项式,并且实现求两多项式的和; 例如:
      3x^4     -6x^2 +5x - 10         //多项式1
+        2x^3 +6x^2 -x +1            // 多项式2
=3x^4 +2x^3   -4x +1               //多项式3

程序中:  输入 4 3 2 -6 1 5 0 -10  //代表多项式1
             3 2 2 6 1 -1 0 1    //代表多项式2
   
      输出  4 3 3 2 1 4 0 -9     //代表多项式3

下面是我花一天憋出来的.. 本渣渣刚上完大一,实在是水,程序就是运行出错,求大神解释纠正!





#include<iostream>
using namespace std;

struct DXS{
int ZS;
int XS;
int BZF;       //标志位
DXS *next;
};
int main(void)
{  DXS *Flist, *Slist, *Fhead,
       *Shead, *Ftail, *Stail, *a, *b,*c,*d,
   *Thead, *Tlist;
   Fhead=Ftail=NULL;
   while(1)
   {
   Flist=new DXS;
   cin>>Flist->ZS>>Flist->XS;
   
   Flist->next=NULL;
   if(Ftail==NULL)
   Fhead=Ftail=Flist;
   else {Ftail->next=Flist;
         Ftail=Flist; }    
   if(Flist->ZS==0)
   { break;}     //结束语句
   }                                      //建立第一个链表
   Stail=Shead=NULL;
   while(1)
   {
   Slist=new DXS;

   cin>>Slist->ZS>>Slist->XS;
   
   Slist->next=NULL;

   if(Stail==NULL)
   Shead=Slist=Stail;
   else { Stail->next=Slist;
          Stail=Slist; }   
   if(Slist->ZS==0)


   { break; }      //结束语句
   }                          //建立第二个链表
                               
   for(a=Fhead;a!=NULL;a=a->next)                //把相等指数的项相加到第二个链表
   {  a->BZF=0;
      for(b=Shead;b!=NULL;b=b->next)
  {  if(a->ZS=b->ZS)
     {  b->XS+=a->XS;
        a->BZF=1;             //有相等指数时,标志一下
     }
  }
   }
   for(a=Fhead;a!=NULL;a=a->next)    //将指数不相等的项插入第二个链表后
   {  if(a->BZF==0)
      {  Stail->next=a;
          Stail=a;
  Stail->next=NULL;
      }
   }
   Tlist=NULL;
   Thead=new DXS;
   Thead->next=Shead;
   while(Thead->next!=Tlist)     //冒泡排序
   {  b=Thead;
      c=b->next;
  while(c!=Tlist)
  {  if(c->next==NULL) break;
     else if(b->next->ZS<c->next->ZS)     //交换两结点
 {      d=c->next;
        b->next=c->next;
c->next=c->next->next;
b->next->next=c;
c=d;                   //这个地方不是很确定
 }
 b=b->next;
 c=c->next;
  }
  Tlist=b;
  }
   cout<<'\n';
   for(Thead=Tlist;Thead!=NULL;Thead=Thead->next)
   {  if(Thead->XS!=0)
      cout<<Thead->ZS<<" "<<Thead->XS;
   }
   return 0;[code=c]


}[/code]

程序可以输入,但提示出错

C++ 链表 难题
[解决办法]


#include<iostream>
using namespace std;

struct DXS{
    int ZS;
    int XS;
    int BZF;       //标志位
    DXS *next;
};
int main(void)
{  DXS *Flist, *Slist, *Fhead,
    *Shead, *Ftail, *Stail, *a, *b,*c,*d,
    *Thead, *Tlist;
    Fhead=Ftail=NULL;
    while(1)
    {
        Flist=new DXS;
        cin>>Flist->ZS>>Flist->XS;
        Flist->next=NULL;
        if(Fhead==NULL) //应该是判断头节点
            Fhead=Ftail=Flist;
        else {Ftail->next=Flist;
            Ftail=Flist; }    
        if(Flist->ZS==0)
        { break;}     //结束语句
    }                                      //建立第一个链表
    Stail=Shead=NULL;
    while(1)
    {
        Slist=new DXS;
        cin>>Slist->ZS>>Slist->XS;
        Slist->next=NULL;
        if(Shead==NULL)  {//这里也是判断头节点
            Shead=Stail=Slist; //赋值顺序错误!
        }
        else { Stail->next=Slist;
            Stail=Slist; }   


        if(Slist->ZS==0)
        { break; }      //结束语句
    }                          //建立第二个链表
    for(a=Fhead;a!=NULL;a=a->next)                //把相等指数的项相加到第二个链表
    {  a->BZF=0;
        for(b=Shead;b!=NULL;b=b->next)
        {  if(a->ZS==b->ZS)  //判断语句不是=赋值
            {  b->XS+=a->XS;
                a->BZF=1;             //有相等指数时,标志一下
            }
        }
    }
    for(a=Fhead;a!=NULL;a=a->next)    //将指数不相等的项插入第二个链表后
    {  if(a->BZF==0)
        {  Stail->next=a;
            Stail=a;
            Stail->next=NULL;
        }
    }
    Tlist=NULL;
    Thead=new DXS;
    Thead->next=Shead;
    while(Thead->next!=Tlist)     //冒泡排序
    {  b=Thead;
        c=b->next;
        while(c!=Tlist)
        {  if(c->next==NULL) break;
            else if(b->next->ZS<c->next->ZS)     //交换两结点
            {      d=c->next;
                b->next=c->next;
                c->next=c->next->next;
                b->next->next=c;
                c=d;                   //这个地方不是很确定


                 continue;
            }
            b=b->next;
            c=c->next;
        }
        if(c==Tlist)
            Tlist=c;
        else
            Tlist=b;
    }
    cout<<'\n';
    for(Thead=Tlist;Thead!=NULL;Thead=Thead->next){
        if(Thead->XS!=0)
        cout<<Thead->ZS<<" "<<Thead->XS<<' '; 
    }
    return 0;
}

热点排行