您的位置:首页 > 其它

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

2011-07-21 11:58 661 查看
//学生类封装 ,(链表) 排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用
风骚的全指针结构体走位
#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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐