?? reader.h
字號:
#ifndef READER_H
#define READER_H
#include<fstream.h>
#include<string.h>
#include"readerhistory.h"
class reader
{
public:
reader( char* a = " " , char *b =" " , char *c = " " )
{
strcpy(code,a) ;
}
void set() //輸入讀者資料
{
cout<<"請輸入讀者帳號: ";
cin>>code;
cout<<"請輸入讀者姓名: ";
cin>>name;
cout<<"請輸入帳號的密碼: ";
cin >> passward ;
}
char* getcode()
{
return code ;
}
char* getpassward()
{
return passward ;
}
void print() //打印讀者資料
{
cout<<"讀者帳號: "<<code<<endl ;
cout<<"讀者姓名: "<<name<<endl ;
cout<<"讀者帳號密碼: "<<passward<<endl ;
}
void save( fstream f ) //保存讀者的信息在文本中
{
f.seekp( 0, ios::end ) ; // 讀指針移到文件末尾
f.write( (char *) & (*this) , sizeof( *this ) ) ; // 寫入文件
}
void changemessage(fstream f)//修改讀者自己的信息
{
reader one ;
char key[10] ;
f.seekg( 0, ios::end ) ; // 讀指針移到文件末尾
long posEnd = f.tellg() ; // 記錄文件尾位置
f.seekp( 0, ios::beg ) ; // 讀指針移到文件開始
cout << "請再次輸入您的帳號: " ;
cin >> key ;
do {
f.read((char *) & one , sizeof(one)) ;
} while ( strcmp(one.getcode(),key) && f.tellg() != posEnd ) ; //遍歷查找該帳號的讀者
if ( !strcmp(one.getcode(),key) )
{
cout<<"您的帳號信息為:"<<endl ;
one.print();
cout<<"現在開始修改您的信息:"<<endl;
one.set() ;
f.seekp( -long( sizeof( one ) ), ios::cur ); // 指針復位
f.write( (char *) & one , sizeof( one ) ) ; // 重新寫入文件
cout << "您的帳號信息修改后為:" << endl ;
one.print() ;
}
else
{
cout << "對不起,您的帳號輸入有誤!" << endl ;
}
}
void checkbook(fstream f)//查詢書的信息
{
book one ;
int key ;
f.seekg( 0, ios::end ) ; // 讀指針移到文件末尾
long posEnd = f.tellg() ; // 記錄文件尾位置
f.seekp( 0, ios::beg ) ; // 讀指針移到文件開始
cout << "請輸入你要查詢的書的編號: " ;
cin >> key ;
do {
f.read((char *) & one , sizeof(one)) ;
} while ( one.getcode() != key && f.tellg() != posEnd ) ; //遍歷查找書籍
if( one.getcode() == key )
{
one.print() ;
}
else
cout << "對不起,沒有你所要查找的圖書信息" << endl ;
}
void borrow(fstream f)//讀者借書
{
readerhistory history ;
book one ;
int key ;
f.seekg( 0, ios::end ) ; // 讀指針移到文件末尾
long posEnd = f.tellg() ; // 記錄文件尾位置
f.seekp( 0, ios::beg ) ; // 讀指針移到文件開始
cout << "請輸入你要借閱的書的編號: " ;
cin >> key ;
do {
f.read((char *) & one , sizeof(one)) ;
} while ( one.getcode() != key && f.tellg() != posEnd ) ; //遍歷查找圖書
if( one.getcode() == key )
{
one.print() ;
if( one.getcondition() == true )
{
cout << "恭喜!該書成功借出!" << endl ;
history.save(code,true,one);
one.setcondition(false) ;
f.seekp( -long( sizeof( one ) ), ios::cur ); // 指針復位
f.write( (char *) & one , sizeof( one ) ) ; // 重新寫入文件
}
}
else
cout << "對不起,沒有你所要借閱的圖書信息" << endl ;
}
void back(fstream f)//讀者還書
{
readerhistory history ;
book one ;
int key ;
f.seekg( 0, ios::end ) ; // 讀指針移到文件末尾
long posEnd = f.tellg() ; // 記錄文件尾位置
f.seekp( 0, ios::beg ) ; // 讀指針移到文件開始
cout << "請輸入你所要還的書的編號: " ;
cin >> key ;
do {
f.read((char *) & one , sizeof(one)) ;
} while ( one.getcode() != key && f.tellg() != posEnd ) ; //遍歷查找圖書
if( one.getcondition() == false )
{
one.setcondition(true) ;
history.save(code,false,one);
f.seekp( -long( sizeof( one ) ), ios::cur ); // 指針復位
f.write( (char *) & one , sizeof( one ) ) ; // 重新寫入文件
}
else
cout << "對不起,你所要還的圖書信息錯誤" << endl ;
}
private:
char code[10] ; //讀者帳號
char name[10]; //讀者姓名
char passward[10] ; //讀者帳號密碼
};
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -