📄 student.h
字号:
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <string.h>
class CStudentRec
{
public:
CStudentRec(){chFlag = 'N';}; // 默认构造函数
~CStudentRec(){}; // 默认析构函数
friend ostream& operator<< ( ostream& os, CStudentRec& stu );
friend istream& operator>> ( istream& is, CStudentRec& stu );
CStudentRec& operator = (CStudentRec &stu) // 赋值运算符重载
{
strncpy(strName, stu.strName, 20);
strncpy(strID, stu.strID, 10);
for (int i=0; i<3; i++)
fScore[i] = stu.fScore[i];
fAve = stu.fAve;
chFlag = stu.chFlag;
return *this;
}
char chFlag; // 标志,'A'表示正常,'N'表示空
char strName[20]; // 姓名
char strID[10]; // 学号
float fScore[3]; // 三门成绩
float fAve; // 总平均分
};
// CStudentRec类的实现
ostream& operator<< ( ostream& os, CStudentRec& stu )
{
os.write(&stu.chFlag, sizeof(char));
os.write(stu.strName, sizeof(stu.strName));
os.write(stu.strID, sizeof(stu.strID));
os.write((char *)stu.fScore, sizeof(float)*3);
os.write((char *)&stu.fAve, sizeof(float));
return os;
}
istream& operator>> ( istream& is, CStudentRec& stu )
{
char name[20],id[10];
is.read(&stu.chFlag, sizeof(char));
is.read(name, sizeof(name));
is.read(id, sizeof(id));
is.read((char*)stu.fScore, sizeof(float)*3);
is.read((char*)&stu.fAve, sizeof(float));
strncpy(stu.strName, name, sizeof(name));
strncpy(stu.strID, id, sizeof(id));
return is;
}
class CStudentFile
{
public:
CStudentFile(char* filename);
~CStudentFile();
void Add(CStudentRec stu); // 添加记录
void Delete(char* id); // 删除学号为id的记录
void Update(int nRec, CStudentRec stu); // 更新记录号为nRec的内容,nRec从0开始
int Seek(char* id, CStudentRec &stu); // 按学号查找, 返回记录号,-1表示没有找到
int GetRecCount(void); // 获取文件中的记录数
int GetStuRec( CStudentRec* data ); // 获取所有记录,返回记录数
private:
char* strFileName; // 文件名
};
// CStudentFile类的实现
CStudentFile::CStudentFile(char* filename)
{
strFileName = new char[strlen(filename)+1];
strcpy(strFileName, filename);
}
CStudentFile::~CStudentFile()
{
if (strFileName) delete []strFileName;
}
void CStudentFile::Add(CStudentRec stu)
{
// 打开文件用于添加
fstream file(strFileName, ios::out|ios::app|ios::binary );
file<<stu;
file.close();
}
void CStudentFile::Delete(char *id)
{
CStudentRec temp;
int nDel = Seek(id, temp);
if (nDel<0) return;
// 设置记录中的chFlag为'N'
temp.chFlag = 'N';
Update( nDel, temp );
}
void CStudentFile::Update(int nRec, CStudentRec stu)
{
fstream file(strFileName, ios::in|ios::out|ios::binary); // 二进制读写方式
if (!file) {
cout<<"the "<<strFileName<<" file can't open !\n";
return ;
}
int nSize = sizeof(CStudentRec) - 1;
file.seekg( nRec * nSize);
file<<stu;
file.close();
}
int CStudentFile::Seek(char* id, CStudentRec& stu) // 按学号查找
{
int nRec = -1;
fstream file(strFileName, ios::in|ios::nocreate); // 打开文件用于只读
if (!file) {
cout<<"the "<<strFileName<<" file can't open !\n";
return nRec;
}
int i=0;
while (!file.eof()) {
file>>stu;
if ((strcmp(id, stu.strID) == 0) && (stu.chFlag == 'A')){
nRec = i; break;
}
i++;
}
file.close();
return nRec;
}
int CStudentFile::GetRecCount(void)
{
fstream file(strFileName, ios::in|ios::nocreate); // 打开文件用于只读
if (!file) {
cout<<"the "<<strFileName<<" file can't open !\n";
return 0;
}
int nRec = 0;
while (!file.eof()) { // 读出所有记录
CStudentRec data;
file>>data;
if (data.chFlag == 'A') nRec++;
}
file.close();
return nRec;
}
int CStudentFile::GetStuRec( CStudentRec* data)
{
fstream file(strFileName, ios::in|ios::nocreate); // 打开文件用于只读
if (!file) {
cout<<"the "<<strFileName<<" file can't open !\n";
return 0;
}
int nRec = 0;
while (!file.eof()) { // 读出所有记录
CStudentRec stu;
file>>stu;
if (stu.chFlag == 'A') {
data[nRec] = stu;
nRec++;
}
}
file.close();
return nRec;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -