学生类封装 , (链表)插入排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用
来源:互联网 发布:spss11.0 mac版 编辑:程序博客网 时间:2024/06/10 00:25
//学生类封装 ,(链表) 排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用
风骚的全指针结构体走位
#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虏禄脛脺cinif(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 99b 2 100c 3 101d 4 102e 5 1230 0 0
*/
- 学生类封装 , (链表)插入排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用
- 使用链表与指针,动态开辟空间,录入学生学号及成绩,并输出满足条件的学生。
- 字符数组动态开辟空间和静态开辟空间
- C++第4周项目5 - 数组作数据成员(扩展1动态开辟空间)
- 用malloc函数开辟一个动态存储区,存入五个学生的成绩,输出不合格成绩
- 动态的开辟、数组、排序
- 使用静态成员函数 统计学生成绩
- 为数组动态开辟空间,合理使用内存资源
- 关于多维数组动态开辟空间的一些疑问
- 第十周刷OJ题 统计程序设计基础课程学生的平均成绩(静态成员函数的使用复习)
- 二维数组的动态开辟
- 动态开辟空间new和delete的使用实例
- C++ 动态开辟空间
- C++ 动态开辟空间
- 开辟动态空间
- 6.7 编写一个程序,设计一个类 score 用于统计一个班的学生成绩,其中使用一个静 态数据成员 sumfs 存储总分和一个静态成员函数 rsumfs()返回该总分
- 动态开辟的链表 可以当做数组使用
- 如何动态开辟二维空间的问题,
- 算法与追MM,太强,不要错过
- .BAT简化编译过程
- EJB:First component in name xxx not found - Java / Java EE
- 检测点1.1
- C++之路 堆、栈和静态数据
- 学生类封装 , (链表)插入排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用
- 刚才发生的事似曾相识? 是前生今世么
- 教你一步步用Spring集成XFire开发WebService
- c++ using namespace 详解
- Resolved TFS backup issue>[ Backup Plan Verifications ] A backup plan already exists for this Tfs co
- 典型微软BI平台部署详解(二)
- java位运算
- Spring MVC
- sharepoint "+"号问题