| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1748 人关注过本帖, 1 人收藏
标题:一个关于多项式链表的程序,但乘法不能运行,请大家帮忙看看
只看楼主 加入收藏
Invariably
Rank: 2
等 级:论坛游民
帖 子:54
专家分:46
注 册:2010-9-18
结帖率:90%
收藏(1)
已结贴  问题点数:20 回复次数:7 
一个关于多项式链表的程序,但乘法不能运行,请大家帮忙看看
这是本人写的一个关于多项式链表的加减乘运算的程序,但是乘法不能运行(编译和链接都没问题),请大家多多指教,小弟先谢谢了!!!


# include<iostream>
using namespace std;
struct node
{
    float coef;//系数域
    int exp;//指数域
    struct node * next;//指向下一个节点的指针
};
typedef struct node polynode;
polynode *add_polynode(polynode *pa,polynode *pb)//10
{
polynode *r1=NULL,*r2=NULL,*q1=NULL,*q2=NULL;
if(pb->next==NULL)    return pa;//每个多项式链表至少有一个结点,它的系数和指数都是零
else if(pa->next==NULL)return pb;
else if(pa->next!=NULL&&pb->next!=NULL)
    {
    polynode head1,head2;
    head1.next=pa;
    head2.next=pb;
    pa=&head1;//20
    pb=&head2;
    r1=pa,r2=pb;
    q1=r1->next ;
    q2=r2->next ;
       do {
        if(q1->exp >q2->exp )
            {
            r1=q1;
            q1=q1->next ;   
            }//30
        else if(q1->exp <q2->exp )
            {
            r1->next =q2;
            q2=q2->next;
            r1->next->next=q1;
            r1=r1->next;
            }
        else if(q1->next ==q2->next )
            {
            float v;//40
            v=q1->coef+q2->coef;
            if(v)
                {
                q1->coef=v;
                r1=q1;
                q1=q1->next;
                r2=q2;
                q2=q2->next;
                delete r2;
                }//50
            else{
                q1=q1->next;
                delete r1->next;
                r1->next=q1;
                r2=q2;
                q2=q2->next;
                delete r2;
                }
            }
        }while(q1->next&&q2->next);//60
       pa=head1.next;
    if(q2->next ==NULL)delete q2;
    else   {
            delete q1;
            r1->next =q2;
            }
    return pa;
    }
}//70
polynode *init_polynode()
{
    int e=0;
    float c=0;
    polynode *p=NULL,*q=NULL,*r=NULL;
    p=new polynode;//p指向的是链表的表头
    r=p;//r指向链表的最后一个结点
    r->next=NULL;
    cout<<"请输入系数(实数型)和指数(整数型),注意:首项系数不要为零,按降幂方式输入当系数和指数为零时输入结束"<<endl;
    //当输入的系数和指数为零时,表示输入过程结束,且输入按降幂方式输入//80
    cin>>c>>e;
    r->coef=c;
    r->exp=e;
    if(c==0) return p;
    while(c!=0)
    {
        q=new polynode;//q指向每一次新产生的结点
        cout<<"请输入次项系数coef(实数型)和指数exp(整数型)"<<endl;
        cin>>c>>e;
        q->coef=c;//90
        q->exp=e;
        q->next=NULL;
        r->next=q;
        r=q;
    }
    if(r->next!=NULL)r->next=NULL;
    return p;
}//建立一个多项式链表,该链表的最后一个结点中方的系数和指数都是零
void print_polynode(polynode *q)
{                               //100
    if(q->coef==0)cout<<"f(x)=0"<<endl;
    else
        {
        polynode *r=q;
        cout<<"f(x)=";
        while(r!=NULL&&r->coef !=0)
            {
            cout<<r->coef<<"X^"<<r->exp;
            r=r->next ;
            if(r->next !=NULL)//110
                {
                if(r->coef >=0)cout<<"+";
                }
            };
        cout<<endl;
        };
}//多项式的输出函数,吧多项式输出到屏幕上
polynode *posit_polynode(polynode*p)
{
    if(p->coef )    //120
    {
        polynode *r=p;
        while(r->next )
        {
        r->coef =-(r->coef) ;
        r=r->next ;
        }
    }
    return p;
}//求负的f(x)//130
polynode *minus_polynode(polynode*pa,polynode*pb)
{
    return    add_polynode(pa,posit_polynode(pb));
}//两多项式相减
polynode *nod_ml_nod(polynode *r1,polynode *r2)
{
    while(r1->next &&r2->next )
    {
        r1->coef *=r2->coef ;
        r1->exp +=r2->exp ;//140
        delete r2;
        return r1;
    }
    r1->coef =0;
    r1->exp =0;
    r1->next =NULL;
    return r1;
}//实现两个多项式两项相乘,即链表的两个节点相乘
polynode *multip_polynode(polynode *pa,polynode *pb)//150
{    polynode *g=NULL,termin;
    termin.coef =0;
    termin.exp =0;
    termin.next =NULL;
    g=&termin;
    if(pa->next &&pb->next )
    {
        polynode *ra=pa,*rb=NULL,*t=NULL,*s=NULL,*f=NULL,end;
        end.coef =0;
        end.exp =0;
        end.next =NULL;   
        while(ra->next)//表示ra没有指到最后一个结点
        {
            f=&end;                        
            rb=pb;
            while(rb->next )
            {
                t=rb->next ;
                s=nod_ml_nod(ra,rb);
                f=add_polynode(f,s);
                rb=t;
            }
            g=add_polynode(g,f);
        }
        return g;
    }
    else return g;
}//实现两多项式相乘
int main()
{
    polynode *p=NULL,*q=NULL;
    p=init_polynode();
    q=init_polynode();
    print_polynode(multip_polynode(p,q));
    return 0;
}

搜索更多相关主题的帖子: return 多项式 
2011-04-06 16:19
pang1567
Rank: 1
来 自:湖北武汉
等 级:新手上路
帖 子:5
专家分:5
注 册:2011-4-5
收藏
得分:5 
看的头疼  你把一些循环也注释下吧  要是需要一元多项式的加减乘的源码  可以回复我

坚持、细心是成功的必修课
2011-04-06 18:47
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:15 
程序代码:
#include<iostream>
using namespace std;

struct node
{
    float coef;//系数域
    int exp;//指数域
    struct node * next;//指向下一个节点的指针
};
typedef struct node polynode;

polynode * init_polynode ()
{
    int e = 0;
    float c = 0;
    polynode *p = NULL, *r = NULL;
    cout<<"请输入系数(实数型)和指数(整数型),注意:首项系数不要为零,按降幂方式输入当系数和指数为零时输入结束"<<endl;
    cin>>c>>e;

    while ( c != 0 )
    {
        if ( NULL != p )
        {
            r->next = new polynode;
            r = r->next;
            r->coef = c;
            r->exp = e;
            r->next = NULL;
        }
        else
        {
            p = r = new polynode;
            r->coef = c;
            r->exp = e;
            r->next = NULL;
        }
        cout<<"请输入次项系数coef(实数型)和指数exp(整数型)"<<endl;
        cin>>c>>e;

    }

    return p;
}


void print_polynode ( polynode *q )
{
    if ( NULL == q )
    {
        cout << "f(x) = 0" << endl;
        return;
    }
    else
    {
        polynode *r = q;
        cout << "f(x) = ";
        while ( NULL != r )
        {
            cout << r->coef << "X^" << r->exp;
            r = r->next;
            if ( NULL != r && r->coef > 0 )
            {
                cout << " + ";
            }
        }
    }

    cout << endl;

}

polynode * add_polynode ( polynode *pa, polynode *pb )
{
    polynode *p = NULL, *r = NULL, *temp;

    while ( NULL != pa && NULL != pb )
    {
        if ( pa->exp == pb->exp )
        {
            if ( pa->coef+pb->coef == 0 )
            {
                temp = pa;
                pa = pa->next;
                delete temp;

                temp = pb;
                pb = pb->next;
                delete temp;
            }
            else
            {
                temp = pb;
                pb = pb->next;
                pa->coef += temp->coef;
                delete temp;
                

                if ( NULL != r )
                {
                    r->next = pa;
                    r = r->next;
                }
                else
                {
                    p = r = pa;
                }

                pa = pa->next;
            }
        }
        else if ( pa->exp > pb->exp )
        {
            if ( NULL != r )
            {
                r->next = pa;
                r = r->next;
            }
            else
            {
                p = r = pa;
            }
            pa = pa->next;
        }
        else if ( pa->exp < pb->exp )
        {
            if ( NULL != r )
            {
                r->next = pb;
                r = r->next;
            }
            else
            {
                p = r = pb;
            }
            pb = pb->next;
        }
    }
   
    if ( NULL != pa )
    {
        if ( NULL == p )
        {
            p = new polynode;
            p = pa;
        }
        else
        {
            r->next = pa;
        }
    }

    else if ( NULL != pb )
    {
        if ( NULL == p )
        {
            p = new polynode;
            p = pb;
        }
        else
        {
            r->next = pb;
        }       
    }
    else
    {
        r->next = NULL;
    }

    return p;
}

polynode *posit_polynode ( polynode *p )
{
    polynode * t_p = p;

    while ( NULL != t_p )
    {
        t_p->coef = 0 - t_p->coef;
        t_p = t_p->next;
    }

    return p;
}

polynode *minus_polynode ( polynode *pa, polynode *pb )
{
    return add_polynode ( pa, posit_polynode(pb) );
}

polynode *copy ( polynode *p )
{
    polynode *t_p = NULL, *r = NULL, *head = NULL;
    t_p = p;

    while ( NULL != t_p )
    {
        if ( NULL != r )
        {
            r->next = new polynode;
            r = r->next;
        }
        else
        {
            head = r = new polynode;
        }
        r->coef = t_p->coef;
        r->exp = t_p->exp;
        r->next = NULL;
    }

    return head;
}

polynode *multip_polynode ( polynode *pa, polynode *pb )
{
    polynode *t_a = pa, *t_b = pb, *sum = NULL, *head = NULL, *r = NULL;

    while ( NULL != t_a && NULL != pb )
    {
        t_b = pb;
        while ( NULL != t_b )
        {
            if ( NULL != r )
            {
                r->next = new polynode;
                r = r->next;
            }
            else
            {
                head = r = new polynode;
            }
            r->coef = t_a->coef * t_b->coef;
            r->exp = t_a->exp + t_b->exp;
            r->next = NULL;

            t_b = t_b->next;
        }

        sum = add_polynode( sum, head );
        r = head = NULL;
        t_a = t_a->next;
    }

    return sum;
}


int main()
{
    polynode *p = NULL, *q = NULL;

    p = init_polynode ();

    q = init_polynode ();

    //p = add_polynode( p, q );//注意 加法操作完成后 q = NULL;
    //p = minus_polynode ( p, q ); //注意 减法操作完成后 q = NULL;
   
    p = multip_polynode ( p, q );

    print_polynode( p );

    return 0;
}
2011-04-06 22:38
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2011-04-06 22:38
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
不知道 还有没有 错误的地方 测试过的例子暂时没发现
2011-04-06 22:41
Invariably
Rank: 2
等 级:论坛游民
帖 子:54
专家分:46
注 册:2010-9-18
收藏
得分:0 
谢谢版主,我明天再看看,今天有点晚了,谢谢了
2011-04-06 23:29
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:0 
用栈不是很简单吗
2011-04-07 08:05
心流千雪
Rank: 1
等 级:等待验证会员
帖 子:16
专家分:7
注 册:2012-11-30
收藏
得分:0 
.定义一个多项式节点类CNode:
class CNode
{
private:
           int exp;//指数
           float coef;//系数
           CNode *next;
    CNode *prev;
public:
    CNode(float cf,int ep);//cf为系数,ep为指数
    ~CNode();//实现节点脱离链表功能
   
};
CNode *head=NULL;
//完成以下定义
CNode::CNode(float cf,int ep)
{
}
定义一个全局指针CNode *head(指向多项式双向链表中的第一项节点),
要求:
1、CNode的构造函数自动将构造的对象插入head链表中(按指数从大到小排列),在输入指数相同的项时需要进行合并;
2、main函数结束时依次释放(delete q)链表中的的节点。
//验证定义
2012-11-30 10:15
快速回复:一个关于多项式链表的程序,但乘法不能运行,请大家帮忙看看
数据加载中...
 
   



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

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