由于本人水平低,请教一下高手这个c++程序如何理解,谢谢啦
把比较难点的句子的意思注释在后面,谢谢啦,我只是懂些皮毛罢了,谢谢啦//C++模拟图书馆借书还书过程
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<iomanip.h>
#define LENbookName 100
#define LENnode 100
#define LENreadName 100
#define borrowMAX 3
class BOOKDATA
{
int bookId;
char bookName[LENbookName];
int shelfCount;
int bookCount;
public:
int getBookId(){return bookId;}
char *getBookName(){return bookName;}
int getShelfCount(){return shelfCount;}
int getBookCount(){return bookCount;}
void setBookId(int n){bookId=n;}
void setBookName(char na[]){strcpy(bookName,na);}
void setShelfCount(int n){shelfCount=n;}
void setBookCount(int n){bookCount=n;}
};
class BOOKWAY
{
int LEN;
public:
BOOKDATA node[LENnode];
void showData(int i)
{
cout<<node[i].getBookId()
<<setw(8)<<node[i].getBookName()
<<setw(8)<<node[i].getShelfCount()
<<setw(8)<<node[i].getBookCount()
<<endl;
}
void showData()
{
for(int i=0;i<LEN;i++)
showData(i);
}
void save()
{
fstream f("BookData.bin",ios::out|ios::binary);
for(int i=0;i<LEN;i++)
f.write((char*)&node[i],sizeof(node[i]));
f.close();
}
void open()
{
fstream f("BookData.bin",ios::in|ios::binary);
LEN=-1;
for(;;)
{
if(!f)
break;
LEN++;
f.seekg(LEN*sizeof(node[LEN]));
f.read((char*)&node[LEN],sizeof(node[LEN]));
}
f.close();
}
int BinSearch()
{
int bookId;
cout<<"bookId:";
cin>>bookId;
int low=0,mid,high=LEN-1;
while(low<=high)
{
mid=(low+high)/2;
if(node[mid].getBookId()==bookId)
return mid;
else if(node[mid].getBookId()<bookId)
low=mid+1;
else
high=mid-1;
}
return -1;
}
void newFile()
{
int bookId;
char bookName[LENbookName];
int bookCount;
if(LEN<0)
LEN=0;
for(;;LEN++)
{
cout<<"bookId:";
cin>>bookId;
if(bookId<0)
break;
node[LEN].setBookId(bookId);
cout<<"bookName:";
cin>>bookName;
node[LEN].setBookName(bookName);
cout<<"bookCount:";
cin>>bookCount;
node[LEN].setBookCount(bookCount);
node[LEN].setShelfCount(bookCount);
}
}
};
class READDATA
{
int readId;
char readName[LENreadName];
int borrowCount;
public:
int borrowList[borrowMAX];
int getReadId(){return readId;}
char *getReadName(){return readName;}
int getBorrowCount(){return borrowCount;}
void setReadId(int n){readId=n;}
void setReadName(char na[]){strcpy(readName,na);}
void setBorrowCount(int n){borrowCount=n;}
};
class READWAY
{
int LEN;
public:
READDATA node[LENnode];
void list(int who)
{
cout<<"[";
for(int i=0;i<borrowMAX;i++)
if(node[who].borrowList[i]!=0)
cout<<node[who].borrowList[i]<<" | ";
cout<<"]";
}
void showData(int i)
{
cout<<node[i].getReadId()<<setw(8)<<node[i].getReadName();
list(i);
cout<<setw(8)<<node[i].getBorrowCount()<<endl;
}
void showData()
{
for(int i=0;i<LEN;i++)
showData(i);
}
void save()
{
fstream f("ReadData.bin",ios::out|ios::binary);
for(int i=0;i<LEN;i++)
f.write((char*)&node[i],sizeof(node[i]));
f.close();
}
void open()
{
fstream f("ReadData.bin",ios::in|ios::binary);
LEN=-1;
for(;;)
{
if(!f)
break;
LEN++;
f.seekg(LEN*sizeof(node[LEN]));
f.read((char*)&node[LEN],sizeof(node[LEN]));
}
f.close();
}
void newFile()
{
int readId;
char readName[LENreadName];
if(LEN<0)
LEN=0;
int i;
for(;;LEN++)
{
cout<<"readId:";
cin>>readId;
if(readId==-1)
break;
node[LEN].setReadId(readId);
cout<<"readName:";
cin>>readName;
node[LEN].setReadName(readName);
node[LEN].setBorrowCount(0);
for(i=0;i<borrowMAX;i++)
node[LEN].borrowList[i]=0;
}
}
int BinSearch()
{
int readId;
cout<<"readId:";
cin>>readId;
int low=0,mid,high=LEN-1;
while(low<=high)
{
mid=(low+high)/2;
if(node[mid].getReadId()==readId)
return mid;
else if(node[mid].getReadId()<readId)
low=mid+1;
else
high=mid-1;
}
return -1;
}
};
void main()
{
BOOKWAY bookway;
READWAY readway;
bookway.open();
bookway.showData();
cout<<"=============="<<endl;
readway.open();
readway.showData();
int sn;
for(;;)
{
cout<<"sn:";
cin>>sn;
switch(sn)
{
case 1:
bookway.open();
bookway.newFile();
bookway.save();
break;
case 5:
int temp;
temp=readway.BinSearch();
if(temp==-1)
cout<<"not find"<<endl;
else
readway.showData(temp);
break;
case 11:
readway.open();
readway.newFile();
readway.save();
break;
case 20:
int temp1,temp2,i;
temp1=bookway.BinSearch();
temp2=readway.BinSearch();
if(temp1==-1 || temp2==-1)
cout<<"not find"<<endl;
else
{
if(readway.node[temp2].getBorrowCount()<borrowMAX)//如果没有超出允许借的最大数量
{
bookway.node[temp1].setShelfCount(bookway.node[temp1].getShelfCount()-1);
readway.node[temp2].setBorrowCount(readway.node[temp2].getBorrowCount()+1);
for(i=0;i<borrowMAX;i++)
{
if(readway.node[temp2].borrowList[i]<=0)
{
readway.node[temp2].borrowList[i]=bookway.node[temp1].getBookId();
break;
}
}
bookway.save();
readway.save();
}
else
cout<<"not borrow yet"<<endl;
}
break;
case 21:
int temp3,temp4,j;
temp3=bookway.BinSearch();
temp4=readway.BinSearch();
if(temp3==-1 || temp4==-1)
cout<<"not find"<<endl;
else
{
if(readway.node[temp4].getBorrowCount()>0)//如果该读者当前已借书数量大于0
{
for(j=0;j<borrowMAX;j++)
if(readway.node[temp4].borrowList[j]==bookway.node[temp3].getBookId())
{
readway.node[temp4].borrowList[j]=0;
break;
}
bookway.node[temp3].setShelfCount(bookway.node[temp3].getShelfCount()+1);
readway.node[temp4].setBorrowCount(readway.node[temp4].getBorrowCount()-1);
bookway.save();
readway.save();
}
else
cout<<"no book to back"<<endl;
}
break;
}
}