学生类封装 , (链表)插入排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用

 //学生类封装  ,(链表) 排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用

风骚的全指针结构体走位

 
#include <iostream>
#include <string>
using namespace std;



class CStudent;
typedef struct stunode{
	CStudent *stuPoint;
	struct stunode * next;
}*Linklist;//指针结构体 是对对象的一个映射链表

class CStudent
{
public:
	CStudent();
	CStudent(string name,string num,float score);
	virtual ~CStudent();
public:

	bool init(string name,string num,float score);//对学生进行初始化
	static bool output();//输出
	static bool insertsort();//插入法排序
private:
	 /*struct stunode{
		CStudent *stuPoint;
		struct stunode * next;
	}*Linklist; zhe li de wenti shi xia mian buzhi zen me ding yi*/
	static Linklist linklist;//生成链表的头节点,,,,,,这里仅仅是对静态数据成员进行引用性声明

private:
	static int count;//记录总学生人数的个数
	float score	;//学生的分数
	string name,num;//名字和学号
};


int CStudent::count = 0;
Linklist CStudent::linklist = NULL;//静态数据成员必须在命名空间作用域的某个地方使用类名限定定义性声明
CStudent::CStudent()
{
/*	static struct stunode * last = NULL;
	count++;
	linklist = new (struct stunode *)();
	linklist->next = last;
	linklist->stuPoint = this;
	last = linklist;
	//zhe li de wenti shi buneng diaoyong linklist??? 在构造函数中不能使用this不能赋值给linklist->stupoint???
*/
	//stuPointList[count] = this;
}

CStudent::~CStudent()
{
}

CStudent::CStudent(string name,string num,float score):name(name),num(num),score(score)
{
}
bool CStudent::insertsort()
{
Linklist p = linklist,q=linklist,newhead,comp,last;//用来取 主链的 头节点,下次要取源的节点和生成新链表的 头节点,以及要比较的节点和记录要插入节点的前驱
	for(int i=1;i<=count;i++){
		q = q->next;// 把下次要从源中取的点 先记下
		if(i == 1){newhead = p;}
		else{
			comp = newhead;
			//找到要插入的位置 comp 为与之想比较的节点
			while( comp!= NULL &&p->stuPoint->score < comp->stuPoint->score  )
			{
				last = comp;
				comp = comp->next;
			}
			p->next = comp;
			if(comp == newhead)
			{
				newhead = p;
			}
			else{
				last->next = p;
			}
		}
		last = p;
		p = q;//待插入节点在 源 中取节点
		last->next = NULL;
	}
	linklist = newhead;//新链表的头结点变了
	return true;
}
bool CStudent::init(string name1,string num1,float score1)//:name(name),num(num),score(score)
{
	name = name1;
	num = num1;
	score = score1;

	count++;
	Linklist temp;
	temp = new (struct stunode )();
	temp->next = linklist;
	temp->stuPoint = this;//将每次产生的学生对象的地址 与 映射链表 对接上
	linklist = temp;

	return true;
}
bool CStudent::output()
{
	struct stunode *temp = linklist;
	cout<<"num\t"<<"name\t"<<"score"<<endl;;
	while(temp != NULL)
	{
		cout<<temp->stuPoint->num<<"\t";
		cout<<temp->stuPoint->name<<"\t";
		cout<<temp->stuPoint->score<<endl;

		temp = temp->next;
	}
	return true;
}
int main()
{
	freopen("in.txt","r",stdin);
	CStudent *list;
	string name,num;
	float score;
	while( 1 )
	{
		cin>>name>>num>>score;//脭脷 string脰脨虏脜脛脺cin  string.h虏禄脛脺cin
		if(name == "0")
			break;
		list = new (CStudent);//能否在这里进行初始化 ?
		list->init(name,num,score);
	}
	cout<<"before sort:"<<endl;
	CStudent::output();
	CStudent::insertsort();
	cout<<"after insertsort:"<<endl;
	CStudent::output();

	return 0;
}/*
 * main.cpp
 *
 *  Created on: Jul 21, 2011
 *      Author: root
 */




/*in.txt 附上测试文件
a  1 99
b  2 100
c  3 101
d  4 102
e  5 123
0  0 0
*/













  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值