| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1153 人关注过本帖
标题:数据结构一元多项式相加问题
只看楼主 加入收藏
yan761649432
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-11-15
收藏
 问题点数:0 回复次数:1 
数据结构一元多项式相加问题
#include <iostream>
using namespace std;
typedef struct node
{
    float coef;//系数域
    int exp;//指数域
    struct node *next;

}polynode;
//创建
polynode *init_polynode()
{
    int c=0,e=0;
    polynode *p=NULL,*q=NULL,*r=NULL;
    p=new polynode;
    r=p;
    cout<<"请按指数的升降次序输入系数和指数:"<<endl;
    cin>>c>>e;
    while(c!=0)
    {
       q=new polynode;
       q->coef=c;
       q->exp=e;
       r->next=q;
       r=q;
       cin>>c>>e;
    }
    if(r!=NULL)
       r->next=NULL;
    return p;
   
}
void sort(polynode *L,int n){       //冒泡排序
 polynode *p,*q;
 p=L->next;
 q=p->next;
 int i,j,t,k;
 for(j=0;i<n-1;j++)
 {
  for(i=0;i<n-j-1;i++)
  {
   if(p->exp>q->exp)
   {
    t=p->exp;
    p->exp=q->exp;
    q->exp=t;
    k=p->coef;
    p->coef=q->coef;
    q->coef=k;
   }//if
      p=p->next;
   q=q->next;
  }//for
 }//for
}//PaixuF

//相加
polynode *add_polynode(polynode *pa, polynode *pb)
{
    polynode *head, *t, *s,*p1,*p2;
    int v;
    p1=pa->next;
    p2=pb->next;
    t=pa;
    head=t;
    while(p1&&p2)
    {
       if(p1->exp==p2->exp)
       {
          v=p1->coef+p2->coef;
          if(v!=0)
          {
             p1->coef=v;
             t->next=p1;
             t=t->next;
             p1=p1->next;
             s=p2;
             p2=p2->next;
             delete s;
          }
          else
          {
              s=p1->next;delete p1;p1=s;
              s=p2->next;delete p2;p2=s;
          }
       }
       else if(p1->exp<p2->exp)
       {
           t->next=p1;
           t=t->next;
           p1=p1->next;
       }
       else
       {
           t->next=p2;
           t=t->next;
           p2=p2->next;
       }
    }
     if(p1)
         t->next=p1;
     else
         t->next=p2;

     return head;
}
void print_polynode(polynode *head)
{
    polynode *p=NULL;
    p=head->next;
    if(p==NULL)
        cout<<"多项式为NULL;"<<endl;
    else
    {
       while(p!=NULL)
       {
           if(p->coef>=0)
             cout<<"+"<<p->coef<<"x^"<<p->exp;
           else
              cout<<p->coef<<"x^"<<p->exp;
           p=p->next;

       }
       cout<<endl;
    }

   

}

int main()
{
 polynode *heada=NULL,*headb=NULL,*headsum=NULL;
 int m,n;
 cout<<"建立第一个函数:"<<endl;
 cout<<"请输入多项式项数:";
 cin>>m;
 heada=init_polynode();
 cout<<"第一个函数:"<<endl;
 sort(heada,m);
 print_polynode(heada);
 cout<<"建立第二个函数:"<<endl;
 cout<<"请输入多项式项数:";
 cin>>n;
 headb=init_polynode();
 cout<<"第二个函数:"<<endl;
 sort(headb,n);
 print_polynode(headb);
 headsum=add_polynode(heada,headb);
 cout<<"函数和为:"<<endl;
 print_polynode(headsum);
 return 0;
}



我的这个程序当有一个多项式为0时不能输出结果,不知道该怎么改?
搜索更多相关主题的帖子: 多项式 数据结构 相加 
2010-11-23 08:17
chenhaiquanw
Rank: 2
等 级:论坛游民
帖 子:9
专家分:70
注 册:2010-11-28
收藏
得分:0 
#include <iostream>
using namespace std;
typedef struct node
{
    float coef;//系数域
    int exp;//指数域
    struct node *next;
}polynode;
//创建
polynode *init_polynode()
{
    int c = 0, e = 0;
    polynode *p = NULL, *q = NULL, *r = NULL;
    p = new polynode;
    r = p;
    r->next = NULL;                              //注意楼主没加(当只有一项时会出错)
    cout << "请按指数的升降次序输入系数和指数:" <<endl;
    cin >> c >> e;
    if(c == 0)
    {
        return NULL;
    }
    while (c != 0)
    {
        q = new polynode;
        q->coef = c;
        q->exp = e;
        q->next = NULL;
        r->next = q;
        r = q;
        cin >> c >> e;
    }
    if (r != NULL)
        r->next = NULL;
    return p;
}
void sort(polynode *L, int n)
{       //冒泡排序
    polynode *p, *q;
    if( L==NULL)               //当为空的时候,应该直接结束....
    {
        return;
    }
    p = L->next;
    q = p->next;
    int i, j, t, k;
    for (j = 0; i < n - 1; j++)
    {
        for (i = 0; i < n - j - 1; i++)
        {
            if (p->exp>q->exp)
            {
                t = p->exp;
                p->exp = q->exp;
                q->exp = t;
                k = p->coef;
                p->coef = q->coef;
                q->coef = k;
            }// if
            p = p->next;
            q = q->next;
        }// for
    }// for
}// PaixuF

//相加

///////////////////////////////////////////////////////////////////////
//
// 函数名       : *add_polynode
// 功能描述     : 多项式相加
// 参数         : polynode *pa
// 参数         : polynode *pb
// 返回值       : polynode
//
///////////////////////////////////////////////////////////////////////
polynode *add_polynode(polynode *pa, polynode *pb)
{
    polynode *head, *t, *s, *p1, *p2;
    int v;
   if( pa == NULL && pb == NULL)//  楼主应当加个判断,楼主多个地方忘了指针有空的时候。
       return NULL;
   else
       if(pa == NULL && pb != NULL)
           return pb;
       else
           if(pa != NULL && pb == NULL)
               return pa;

    p1 = pa->next;
    p2 = pb->next;
    t = pa;
    head = t;
    while (p1 && p2)
    {
        if (p1->exp == p2->exp)
        {
            v = p1->coef + p2->coef;
            if (v != 0)
            {
                p1->coef = v;
                t->next = p1;
                t = t->next;
                p1 = p1->next;
                s = p2;
                p2 = p2->next;
                delete s;
            }
            else
            {
                s = p1->next;
                delete p1;
                p1 = s;
                s = p2->next;
                delete p2;
                p2 = s;
            }
        }
        else if (p1->exp < p2->exp)
        {
            t->next = p1;
            t = t->next;
            p1 = p1->next;
        }
        else
        {
            t->next = p2;
            t = t->next;
            p2 = p2->next;
        }
    }
    if (p1)
        t->next = p1;
    else
        t->next = p2;
   
    return head;
}
void print_polynode(polynode *head)
{
    polynode *p = NULL;
    if( head == NULL)
        return ;
    p = head->next;
    if (p == NULL)
        cout << "多项式为NULL;"<<endl;
    else
    {
        while (p != NULL)
        {
            if (p->coef >= 0)
                cout <<p->coef << "X^ " <<p->exp << "+";
            else
                cout << p->coef << "X^" <<p->exp;
            p = p->next;
        }
        cout << endl;
    }
}

int main()
{
    polynode *heada = NULL, *headb = NULL, *headsum = NULL;
    int m, n;
    cout << "建立第一个函数:" <<endl;
    cout << "请输入多项式项数:";
    cin >> m;
    heada = init_polynode();
    cout << "第一个函数:" <<endl;
    sort(heada, m);
    print_polynode(heada);
    cout << "建立第二个函数:" <<endl;
    cout << "请输入多项式项数:";
    cin >> n;
    headb = init_polynode();
    cout << "第二个函数:" <<endl;
    sort(headb, n);
    print_polynode(headb);
    headsum = add_polynode(heada, headb);
    cout << "函数和为:" <<endl;
    print_polynode(headsum);
    return 0;
}
/*还有很多不完美的地方,比如明明已经输入了多项式几项却还要输入0 +其他任一整数,才能结束 。我没看全,楼主指的错误改了,0项也可以运行,望楼主其他地方应该加以改进,加油*/

2010-12-05 15:10
快速回复:数据结构一元多项式相加问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026689 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved