第三次C++实验

来源:互联网 发布:淘宝4.6.1 编辑:程序博客网 时间:2024/06/04 19:15
//第三次实验题目:
//1.定义一个多项式节点类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)链表中的的节点。
//验证定义
//void main()
//{
//}


#include<iostream>
using namespace std;
class CNode
{
private:
        int exp;//指数
        float coef;//系数
        CNode *next;
CNode *prev;
public:
CNode(float cf,int ep);//cf为系数,ep为指数
~CNode();//实现节点脱离链表功能
void show();
void cycle();

};
CNode *head=NULL;
//完成以下定义
CNode::~CNode ()
{ cout<<"this is a disconstruct!!!!";}
CNode::CNode(float cf,int ep)
{
CNode *p1;
p1=this;
this->coef =cf;
this->exp =ep;
this->next =NULL;
if(head==NULL)
head=p1;
else
p1->next =this;
}
void CNode::cycle ()
{
float i;
int j;
cout<<"please input under data:"<<endl;
while(this->exp !=0)
{
cin>>i>>j;
CNode *p2;
p2= new CNode(i,j);
}
}
void CNode::show ()
{
CNode *q;
q=head;
while (q)
{
cout<<"this list is blow:"<<endl;
cout<<coef<<" "<<exp<<endl;
q=q->next ;
}
}
int main()
{

CNode A;
A.cycle ();
A.show ();
return 0;
}
原创粉丝点击