学生类封装 , (链表)插入排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用
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
*/
相关文章推荐
- 使用链表与指针,动态开辟空间,录入学生学号及成绩,并输出满足条件的学生。
- C++第4周项目5 - 数组作数据成员(扩展1动态开辟空间)
- C语言单向动态链表程序,实现链表的建立,合并,重新排序,链表元素的插入与删除,以及根据元素成员的值进行元素删除。
- 动态开辟的链表 可以当做数组使用
- 为数组动态开辟空间,合理使用内存资源
- 第3周项目5--数组作数据成员,动态分配内存开辟空间
- 使用Python创建MySQL数据库实现字段动态添加以及动态的插入数据
- 6.对数组进行排序、求最大值和求元素和的函数采用静态成员函数的方式封装成数组算法类模板ArrayAlg,并采用相关数据进行测试。
- 使用Python创建MySQL数据库实现字段动态增加以及动态的插入数据
- 关于动态存储分配函数的调用,在已经过排序的数组中查找及删除内容的操作,余数的分析,删除字符数组中的空格,对链表的逆置,在源字符串中查找子字符串的个数,函数指针以及函数的调用,循环赋值带来的问题以及插入
- 静态数据成员 静态函数成员 以及 const 成员变量
- 指针作数据成员建立动态数组
- MyBatis使用动态SQL批量插入数据
- 使用接口进行封装以及数据传输
- Mybatis动态插入数据(使用trim标签)
- {希尔排序、快速排序、动态数组、单链表、字符串转整型}大综合
- JAVA连接mysql数据库,动态创建表以及动态插入数据
- javascript数组元素的添加、删除与插入以及参数数组的使用
- OC中Block语法、Block使用以及通过Block实现数组排序
- 动态开辟空间new和delete的使用实例