第二次C++实验

//定义一个多项式节点类Node,定义一个全局指针Node *head(指向多项式链表中的第一项节点),
//要求:Node的构造函数自动将构造的对象插入head链表中;
//class Node
//{
//private:
//       int exp;//指数
//       float coef;//系数
//       Node *next;
//public:
// Node();//从键盘接收输入的系数,指数
// Node(float cf,int ep);//

//};
//Node *head=NULL;
//完成以下定义
//Node::Node()
//{
//}
//完成以下定义
//Node::Node(float cf,int ep)
//{
//}
//验证定义
//void main()
//{
//}


#include<iostream>
using namespace std;
class Node
{
private:
        int exp;//指数
        float coef;//系数
        Node *next;

public:
Node (int a);
Node();//从键盘接收输入的系数,指数
Node(float cf,int ep);
void show();
};
Node *head=NULL;
//完成以下定义
Node::Node(int a)
{
a=0;
cout<<"Using construcor !!!";
}
Node::Node()
{

if(head==NULL)
{
head=this;
this->next =NULL;
}
else
{
this->next =head;
head=this;
}
cin>>this->coef >>this->exp ;
// cout<<"Using construcor!!"<<endl;
}
Node::Node(float cf,int ep)
{
if(head=NULL)
{
head=this;
this->next=NULL;
}
else
{
this->next=head;
head=this;
}
this->coef=cf;
this->exp=ep;
// cin>>this->coef;
// cin>>this->exp;
}
//完成以下定义


void Node::show () //链表的输出 //在类外时,其自动生成的this指针是不能移动的
{
Node *p=head;
while(p)
{   
// cout<<this->coef<<" "<<this->exp<<endl; //在类外时,this指针是不能移动的
cout<<p->coef <<" "<<p->exp <<endl;
p=p->next;
}

}
//验证定义
void main()
{
Node A[3];
A[0].show();
Node A2(2.1,3);
A2.show ();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值