| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 432 人关注过本帖
标题:一个编程题.求教
只看楼主 加入收藏
心流千雪
Rank: 1
等 级:等待验证会员
帖 子:16
专家分:7
注 册:2012-11-30
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
一个编程题.求教
.定义一个多项式节点类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)链表中的的节点。

以上是题目。
现在主要是要求1做不出来....跪求..
搜索更多相关主题的帖子: private 指数 public 多项式 
2012-11-30 10:07
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:2 
程序代码:
#include<iostream>
using namespace std;
//定义节点类
class Node
{
private:
    int exp;//指数
    float coef;//系数
    Node *next;
public:
    Node(int ep,float cf);//cf为系数,ep为指数
    Node();//从键盘接收输入的系数,指数
    ~Node();
    void output();//题目未要求 不过可以用来测试

};
Node *head=NULL;
Node::Node(int ep,float cf)
{
    exp=ep;
    coef=cf;
    next=NULL;
}
//题目要求的是在构造函数里面进行初始化
Node::Node()
{
    int exp_;
    float coef_;
    cout<<"please input exp : ";
    cin>>exp_;
    cout<<"please input coef : ";
    cin>>coef_;
    if (head == NULL)   //楼主你的就是要在这里判断一下指数是否相等 你添加一下代码
    {
        head=new Node(exp_,coef_);
    }
    else
    {
        while (head->next != NULL)
        {
            head=head->next;
        }
        head->next=new Node(exp_,coef_);
    }
    

}
//释放链表
Node::~Node()
{
    if(this->next != NULL)
        this->next=NULL;
}
void Node::output()
{
    Node *temp=head;
    while (temp != NULL)
    {
        cout<<temp->coef<<"^"<<temp->exp<<endl;
        temp=temp->next;
    }

}

int main()
{
    Node n1; 
    n1.output();

    return 0;
}

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-11-30 11:28
lyj123
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:60
专家分:152
注 册:2010-11-15
收藏
得分:8 
程序代码:
#include<iostream>
#include<cassert>
using namespace std;
//既然允许全局了,就不用封装当前指针了。
//定义节点类
class Node
{
public:
    int exp;//指数
    float coef;//系数
    Node *next;

    Node(int ep,float cf,Node*p=NULL):exp(ep),coef(cf),next(p){}//cf为系数,ep为指数
    Node();
    ~Node();
    //void build(int,float);
    //void output();//题目未要求 不过可以用来测试

};
Node *head=NULL;
void build(int ep,float cf)//插入,合并同类项(虽然不符合异常安全)
{
//cout<<"head0: "<<head->exp<<endl;
    Node*p=new Node(ep,cf);
    assert(p!=head);
   
    Node* curr=head;//我觉得要这样写,不要把head给改掉了
    assert(head!=NULL);
    //cout<<"head1: "<<head->exp<<endl;
    if(ep>curr->exp){/*cout<<"it is >"<<endl;*/p->next=head;head=p;}else
    if(ep==curr->exp){curr->coef+=cf;/*cout<<"coef"<<curr->coef<<endl;*/}else{  

     while(curr!=NULL){
        if(ep>=curr->exp)break;//为了插入到和它相同指数的数之前
             if(curr->next==NULL){curr->next=new Node(ep,cf,curr->next);return;}
        curr=curr->next;
     }
    Node* Ix=head;
    while(Ix->next!=curr)Ix=Ix->next;
         if(curr->exp==p->exp){curr->coef+=cf;}else{
     p->next=curr;
     Ix->next=p;}
    }
    //cout<<"head2: "<<head->exp<<endl;
}
//题目要求的是在构造函数里面进行初始化
void f(){
    int exp_;
    float coef_;
   
    do{
    cout<<"please input exp : ";
    cin>>exp_;
    cout<<"please input coef : ";
    cin>>coef_;
    if(!cin.good()){cout<<"the end"<<endl;break;}
    if (head == NULL)  
    {
        //cout<<"head is empty"<<endl;
        head=new Node(exp_,coef_);
       
    }
    else
    {
        build(exp_,coef_);
    }
    }while(cin.good());
}
Node::Node(){}
//释放链表
Node::~Node()
{
    Node* p=head;
    if(head!=NULL){
        p=head;
        head=head->next;
        delete p;       
        }
}
void output()
{
    Node *temp=head;
    while (temp != NULL)
    {
        cout<<temp->coef<<"x^"<<temp->exp<<endl;
        temp=temp->next;
    }
}
int main()
{
    f();
    output();
    return 0;
}


[ 本帖最后由 lyj123 于 2012-12-2 06:35 编辑 ]

相互帮助

2012-12-01 16:08
lyj123
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:60
专家分:152
注 册:2010-11-15
收藏
得分:10 
封装了指针的链表
程序代码:
#include<iostream>
#include<cassert>
using namespace std;
//既然允许全局了,就不用封装当前指针了。
//然而C++希望封装指针,所以建议不用全局head,而是定义一个
//表类和一个节点类。一张表就定义一个表的对象
//定义节点类,这只是一个节点!!!而不是一张表
class Node
{
public://这里可以定义为私有(保护)类型,
//多加几个函数就行,比较符合封装思想
    int exp;//指数
    float coef;//系数
    Node *next;

    Node(int ep,float cf,Node*p=NULL):exp(ep),coef(cf),next(p){}//cf为系数,ep为指数
    Node();
    ~Node(){}
    //void build(int,float);
    //void output();//题目未要求 不过可以用来测试

};
//定义表
/*不要弄混概念,在一个节点的构造函数中不可能初始化一张表!
    因为在构造函数中,这个节点还没有创建完成*/
class list{
private:
    Node*head;
    Node*curr;
public:
    list();//这个构造函数里可以进行所需的初始化
    ~list();
    void clear();//清除
    void build(int&,float&);//创建
    void display();//输出
    //bool is_empty();
    //bool is_full();
    //void insert();
    //这些没要求就不写了
};
//Node *head=NULL;  //已经封装了head
//题目要求的是在构造函数里面进行初始化
list::list(){   
    int exp_;
    float coef_;
   
    do{
    cout<<"please input exp : ";
    cin>>exp_;
    cout<<"please input coef : ";
    cin>>coef_;
    if(!cin.good()){cout<<"the end"<<endl;break;}
    if (head == NULL)  
    {head=new Node(exp_,coef_);}
    else
    { build(exp_,coef_);}
    }while(cin.good());
}
void list::build(int &ep,float &cf)//插入,合并同类项(虽然不符合异常安全)
{
    Node*p=new Node(ep,cf);
    assert(p!=head);   
    curr=head;
    assert(head!=NULL);
    if(ep>curr->exp){p->next=head;head=p;}else
    if(ep==curr->exp){curr->coef+=cf;/*cout<<"coef"<<curr->coef<<endl;*/}else{  

     while(curr!=NULL){
        if(ep>=curr->exp)break;//为了插入到和它相同指数的数之前
             if(curr->next==NULL){curr->next=new Node(ep,cf,curr->next);return;}
        curr=curr->next;
     }
    Node* Ix=head;
    while(Ix->next!=curr)Ix=Ix->next;//这个地方有点不舒服,不知道有没有高效点的算法
         if(curr->exp==p->exp){curr->coef+=cf;}else{
     p->next=curr;
     Ix->next=p;}
    }
}

Node::Node(){}
//释放链表
list::~list(){clear();}
void list::clear(){
    if(head!=NULL){
        curr=head;
        head=head->next;
        delete curr;       
        }
}
void list::display()
{
    curr=head;
    while (curr != NULL)
    {
        cout<<curr->coef<<"x^"<<curr->exp<<endl;
        curr=curr->next;
    }
}
int main()
{
    list lis;
    lis.display();
    return 0;
}


[ 本帖最后由 lyj123 于 2012-12-2 06:37 编辑 ]

相互帮助

2012-12-01 18:03
lyj123
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:60
专家分:152
注 册:2010-11-15
收藏
得分:0 
回复 2楼 mmmmmmmmmmmm
在Node()的构造函数里根本不可能进行链表初始化,它只是一个节点!
    您的代码测试失败

相互帮助

2012-12-02 06:37
心流千雪
Rank: 1
等 级:等待验证会员
帖 子:16
专家分:7
注 册:2012-11-30
收藏
得分:0 
Cnode
2012-12-04 09:35
快速回复:一个编程题.求教
数据加载中...
 
   



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

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