| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 459 人关注过本帖
标题:由于本人水平低,请教一下高手这个c++程序如何理解,谢谢啦
只看楼主 加入收藏
shijingze
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-6-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
由于本人水平低,请教一下高手这个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;
}
}
搜索更多相关主题的帖子: return 图书馆 句子 
2011-06-21 15:45
红色警戒
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:19
帖 子:444
专家分:2967
注 册:2005-11-20
收藏
得分:10 
多学学基础的东西吧,谁有那么多时间给你看。

2011-06-21 17:13
samuelchen
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:73
专家分:179
注 册:2011-6-10
收藏
得分:10 
学习写点注释吧。


--------------------------
多么美好的清晨。
2011-06-22 09:42
快速回复:由于本人水平低,请教一下高手这个c++程序如何理解,谢谢啦
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039381 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved