⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 data.h

📁 大一时写的一个课设
💻 H
字号:
#ifndef CLASS_H
#define CLASS_H
#include <fstream.h>
#include <stdlib.h>
#include "reader.h"
#include "book.h"
const int Maxr=50;
const int Maxb=200;  
//定义一个抽象数据类
class data
{
	protected:
		book Book[Maxb];//书籍类对象数组
		reader Read[Maxr];//读者类对象数组
		fstream datafile;
		int r;
		int b;
	public:
		//构造函数,用于打开文件
		data()
		{
			r=-1;
			b=-1;
			datafile.open("f://reader.txt",ios::in|ios::binary);
			if(!datafile)
			{
				cout<<"出现故障,打开读者文件失败"<<endl;
				exit(0);
			}
			while(1)
			{
				if(!datafile)
				{
					break;
				}
				else
				{
					r++;
					datafile.read((char*)&Read[r],sizeof(Read[r]));	
				}
			}
			datafile.close();
			datafile.open("f://book.txt",ios::in|ios::binary);
			if(!datafile)
			{
				cout<<"出现故障,打开书籍文件失败"<<endl;
				exit(0);
			}
			while(1)
			{
				if(!datafile)
				{
					break;
				}
				else
				{
					b++;
					datafile.read((char*)&Book[b],sizeof(Book[b]));
				}			
			}
			datafile.close();
		}
		//析构函数,用于刷新文件
		~data()
		{
			datafile.open("f://reader.txt",ios::out|ios::binary);
			if(!datafile)
			{
				cout<<"出现故障,刷新读者文件失败"<<endl;
				exit(0);
			}	
			for(int i=0;i<r;i++)
			{
				if(Read[i].gettag()==1)
				{
					datafile.write((char*)&Read[i],sizeof(Read[i]));
				}
			}
			datafile.close();
			datafile.open("f://book.txt",ios::out|ios::binary);
			if(!datafile)
			{
				cout<<"出现故障,刷新书籍文件失败"<<endl;
				exit(0);
			}	
			for(i=0;i<b;i++)
			{
				if(Book[i].gettag()==1)
				{
					datafile.write((char*)&Book[i],sizeof(Book[i]));
				}
			}
			datafile.close();
		}
		//纯虚函数,用于显示书籍信息
		virtual void Binfo()=0;
		//纯虚函数,用于显示读者信息
		virtual void Rinfo()=0;
};
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -