📄 filerandom.cpp
字号:
// FileRandom.cpp: implementation of the CFileRandom class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "..\StudentRecords.h"
#include "FileRandom.h"
//#include <fstream>
//using namespace std;
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFileRandom::CFileRandom()
{
head=0;
end=0;
}
CFileRandom::~CFileRandom()
{
}
void CFileRandom::Read(CString filename)
{
//声明一个指向student结构的指针p
student *p;
//两个成员变量赋值
head=0;
end=0;
//打开文件studentlist.bin进行读操作
//fstream file;
//file.open(filename,ios::in|ios::binary);
CFile file;
file.Open(filename,CFile::modeRead|CFile::typeBinary,0);
//内置指针置文件结尾
file.SeekToEnd();//file.seekg(0,ios::end);
//文件的长度(字节数)赋值给a
long a=file.GetLength();//long a=file.tellg();
if(a==0)
{
MessageBox(NULL,"没有存档数据!","提示",MB_OK|MB_ICONASTERISK);
return;
}
//每个结构(记录)所占的字节数
long b=sizeof(student);
//a的值变成记录的条数,即存储student结构的个数
a=a/b;
//内置指针置文件首
file.SeekToBegin();//file.seekg(0,ios::beg);
for(;a>0;a--)
{
//分配一块student结构的内存空间
//空间首地址赋给p
p=new student;
//读出一条记录,放置到刚分配好的内存空间
//file.read((char*)p,sizeof(student));
file.Read((char*)p,sizeof(student));
//第一次执行循环体时,head等于0,执行该段代码:
//将p值赋给head和end,此时,head end p同时指向刚分配的存储空间
//该存储空间的两个成员变量prior和next都赋0
if(head==0)
{
head=p;
end=p;
head->prior=0;
head->next=0;
}
//第二次执行循环体时,执行以下代码:
//把新分配的空间的地址(p值)赋给上一次分配空间的next值
//把上次分配空间的首地址赋给本次分配空间的prior值
//end值指向本次分配的空间
//本次分配空间的next值赋0
else
{
end->next=p;
p->prior=end;
end=p;
end->next=0;
}
}
//
file.Close();//file.close();
}
void CFileRandom::add(char item[])
{
//分配一块student结构的内存空间
//空间首地址赋给p
student *p;
p=new student;
//系统分配失败,返回
if(!p)
{
MessageBox(NULL,"内存分配错误!","提示",MB_OK|MB_ICONASTERISK);
return;
}
strcpy(p->item,item);
//文档是空文档
if(head==0)
{
head=end=p;
p->prior=p->next=NULL;
}
//添加到链表结尾
else
{
p->prior=end;
end->next=p;
end=p;
end->next=0;
}
}
void CFileRandom::Remove(char item[])
{
//定义一个student类型的指针并指向链表首
student *p=head;
//循环直到链表尾
while(p)
{
//如果找到要删除的记录
if(!strcmp(p->item,item))
{
//如果要删除的项不是链表的第一项
if(p->prior)
{
//并且不是链表的最后一项
if(p->next)
{
//链表操作,删除记录
p->next->prior=p->prior;
p->prior->next=p->next;
}
//不是链表的第一项,但是链表的最后一项
else
{
//链表操作,删除尾项
p->prior->next=0;
end=p->prior;
}
}
//如果要删除的项是链表的第一项
else
{
//并且不是链表的最后一项
if(p->next)
{
//链表操作,删除首项
p->next->prior=0;
head=p->next;
}
//既是首项,又是尾项,即链表中只有一项
else
{
head=end=0;
}
}
return;
}
p=p->next;
}
MessageBox(NULL,"没有找到相关记录!","提示",MB_OK|MB_ICONASTERISK);
}
void CFileRandom::DescentSort(int col,int type)
{
int j=0;
char temp1[512];
//定义一个指向student结构的指针,并指向链表开头
student *p=head;
//计算链表个数
for(;p!=0;p=p->next)
j++;
//p指向链表开头
p=head;
CString tmp,tmp1,tmp2;
//循环j*j进行排序,这是一种最常用的排序方法
for(int i=0;i<j*j;i++)
{
//链表个数大于1个,进行排序
if(j!=1)
{
tmp1=p->item;
tmp2=p->next->item;
for(int i=1;i<col;i++)
{
tmp=tmp1.Left(tmp1.Find(';',0)+1);
tmp1=tmp1.Right(tmp1.GetLength()-tmp.GetLength());
}
for(int i=1;i<col;i++)
{
tmp=tmp2.Left(tmp2.Find(';',0)+1);
tmp2=tmp2.Right(tmp2.GetLength()-tmp.GetLength());
}
tmp1=tmp1.Left(tmp1.Find(';',0));//得到当前记录的指定数部份字串
tmp2=tmp2.Left(tmp2.Find(';',0));//得到下一条记录的指定数部份字串
if(type==1)
{
//链表中该结点的指定数据的值小于下一结点的分数值,互换
if(atoi(tmp1)<atoi(tmp2))
{
strcpy(temp1,p->item);
strcpy(p->item,p->next->item);
strcpy(p->next->item,temp1);
}
p=p->next;
//操作到链表结尾,继续反向链表开头,直至循环j*j次,完成排序
if(p->next==0) p=head;
}
if(type==0)
{
//链表中该结点的指定数据的值大于下一结点的分数值,互换
if(atoi(tmp1)>atoi(tmp2))
{
strcpy(temp1,p->item);
strcpy(p->item,p->next->item);
strcpy(p->next->item,temp1);
}
p=p->next;
//操作到链表结尾,继续反向链表开头,直至循环j*j次,完成排序
if(p->next==0) p=head;
}
}
}
}
void CFileRandom::Display(CString &cstr)
{
//cstr是一个CString型字符串,它返回所有记录
//每条记录占一行
student* p;
CString tempstr=_T("");
for(p=head;p!=0;p=p->next)
{
tempstr=p->item;
cstr+=tempstr;
}
}
//链表存盘
void CFileRandom::save(CString filename)
{
CFile file;
file.Open(filename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,0);
student *p;
for(p=head;p!=0;p=p->next)
file.Write((char*)p,sizeof(student));
file.Close();
}
void CFileRandom::change(char item[])
{
//定义一个student类型的指针并指向链表首
student *p=head;
//循环直到链表尾
while(p)
{
//如果找到要修改的记录
if(!strcmp(p->item,item))
{
//进行修改
strcpy(p->item,item);
return;
}
p=p->next;
}
MessageBox(NULL,"没有找到相关记录!","提示",MB_OK|MB_ICONASTERISK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -