李枭龙 2015-01-10 14:03 采纳率: 100%
浏览 2096
已采纳

在C++中出现的一个问题。

在增添借阅者时输入学号后退出,求助,谢谢。

 #include <iostream> 
using namespace std;
#include <string> 
#include <fstream>
#include <iomanip>  


const int Maxr=300; 

const int Maxb=300; 

const int Maxbor=10;








class Reader 
{ 
private: 
int tag; 
int No; 
char name[20]; 
int borbook[Maxbor]; 
public: 
Reader() {} 
char *getname() {return name;} 
int gettag() {return tag;}  
int getno() {return No;} 

void setname(char na[]) 
{ 
strcpy(name,na); 
} 
void delbook(){ tag=1; } 
void addreader(int n,char *na) 
{ 
tag=0; 
No=n; 
strcpy(name,na); 
for(int i=0;i<Maxbor;i++) 
borbook[i]=0;
} 
void borrowbook(int bookid)
{   
for(int i=0;i<Maxbor;i++) 
{ 
if (borbook[i]==0) 
{ 
borbook[i]=bookid; 
return; 
} 
} 
} 
int retbook(int bookid) 
{ 
for(int i=0;i<Maxbor;i++) 
{ 
if(borbook[i]==bookid) 
{ 
borbook[i]=0; 
return 1; 
} 
} 
return 0; 
} 
void disp() 
{ 
cout << setw(10) << No <<setw(15) << name<<"借书检索号:[";
for(int i=0;i<Maxbor;i++) 
if(borbook[i]!=0) 
cout << borbook[i] << "|"; 
cout << "]"<<endl; 
} 
}; 
class RDatabase 
{ 
private: 
    int top; 
Reader read[Maxr]; 
public: 
RDatabase() 
{ 
Reader s; 
top=-1;
fstream file("reader.txt",ios::in);  
while (1) 
{ 
file.read((char *)&s,sizeof(s)); 
if (!file)break;
top++; 
read[top]=s; 
} 
file.close(); 
} 
void clear() 
{ 
top=-1; 
} 
int addreader(int n,char *na) 
{ 
Reader *p=query(n); 
if (p==NULL) 
{ 
top++; 
read[top].addreader(n,na); 
return 1; 
} 
return 0; 
} 
Reader *query(int readerid) 
{ 
for (int i=0;i<=top;i++) 
if (read[i].getno()==readerid && 
read[i].gettag()==0) 
{ 
return &read[i];  
} 
return NULL; 
} 
void disp() 
{ 
for (int i=0;i<=top;i++) 
read[i].disp(); 
}
void readerdata(); 
~RDatabase() 
{ 
fstream file("reader.txt",ios::out); 
for (int i=0;i<=top;i++) 
if (read[i].gettag()==0) 
file.write((char *)&read[i],sizeof(read[i])); 
file.close(); 
} 
}; 
void RDatabase::readerdata() 
{ 
char choice; 
char rname[20]; 
int readerid; 
Reader *r; 




cout <<"\n\n\t\t\t借阅者信息\n\n\n\t\t 1 增加借阅者信息\n\n\t\t 2 修改借阅者信息\n\n\t\t 3 删除借阅者信息\n\n\t\t 4 查询借阅者信息\n\n\t\t 0 退出"<<endl; 
cin >> choice; 
switch (choice) 
{ 
case '1': 
cout << "输入借阅者学号:"; 
cin >> readerid; 
cin >> rname; 
addreader (readerid,rname); 
break; 
case '2': 
cout << "输入借阅者学号:"; 
cin >> readerid; 
 r=query(readerid); 
if (r==NULL) 
cout << " 借阅者信息错误"<<endl; 
break; 

cout << "重新输入姓名:"; cin >> rname;
r->setname(rname); 
break; 
case '3': 
    cout << " 输入借阅者学号:"; 
cin >> readerid; r=query(readerid); 
if (r==NULL) 
cout <<" 借阅者信息错误" << endl; 
break; 
r->delbook(); 
break;
case '4': 
cout << "输入借阅者学号:"; 
cin >> readerid; 
r=query(readerid); 
if (r==NULL) 
{ 
cout <<"学号信息错误"<< endl; 
} r->disp(); 
break; 
break; 
case '5': 
disp(); 
break; 

default:cout<<"信息错误,请重新输入:";
    break; 
} 
} 

class Book 
{ 
private: 
float price; 
char author[10]; 
int tag; 
int No; 
char place[40];
char name[20]; 
int onshelf; 
public: 
Book(){} 
char *getname() { return name; } 
int getNo(){ return No; } 
char *getplace(){ return place;}
int gettag(){ return tag; } 
char*getauthor(){return author;} 
char getprice(){return price;} 
void setname(char na[]) 
{ 
strcpy(name,na); 
} 
void setauthor(char au[]) 
{ 
strcpy(author,au);} 
void delbook(){ tag=1;} 
void addbook(int N,char *na,char*au,int pr,char *bp)
{ 
tag=0; 
No=N; 
price=pr; 
strcpy(author,au); 
strcpy(name,na); 
onshelf=1; 
} 
int borrowbook() 
{ 
if (onshelf==1) 
{ 
onshelf=0; 
return 1; 
} 
return 0; 
} 
void retbook() 
{ 
onshelf=1; 
} 
void disp() 
{ 
cout<< setw(6)<< "检索号: " << No << setw(18) <<"书名: "<< name << setw(10) <<"作者: "<<author<<setw(18)<<"单价: "<<price<<setw(10) <<(onshelf==1? "有现存":"已借完") <<endl; 
} 
};  
class BDatabase 
{ 

private: 
int top;
Book book[Maxb]; 
public: 
BDatabase() 
{ 
Book b; 
top=-1; 
fstream file("book.txt",ios::in); 
while (1) 
{ 
file.read((char *)&b,sizeof(b)); 
if (!file) break; 
top++; 
book[top]=b; 
} 
file.close(); 
} 
void clear() 
{ 
top=-1; 
} 
int addbook(int n,char *na,char*au,int pr,char *bp) 
{ 
Book *p=query(n); 
if (NULL==p) 
{ 
top++; 
book[top].addbook(n,na,au,pr,bp); 
return 1; 
} 
return 0; 
} 
Book *query(int bookid) 
{ 
for (int i=0;i<=top;i++) 
if (book[i].getNo()==bookid &&book[i].gettag()==0) 
{ 
return &book[i]; 
} 
return NULL; 
} 
void bookdata(); 
void disp() 
{ 
for (int i=0;i<=top;i++) 
if (book[i].gettag()==0) 
book[i].disp(); 
} 
~BDatabase() 
{ 
fstream file("book.txt",ios::out); 
for (int i=0;i<=top;i++) 
if (book[i].gettag()==0) 
file.write((char *)&book[i],sizeof(book[i])); 
file.close(); 
} 
}; 
void BDatabase::bookdata() 
{ 
char choice; 
char bauthor[10];
int bprice; 
char bname[40]; 
int bookid; 
char bplace[40];
Book *b; 



cout <<"\n\n\n\t\t\t图书信息"<<endl<<endl; cout<<"\t\t1 增加图书信息\n \t\t2 修改图书信息\n\t\t3 删除图书信息\n\t\t4 查询图书信息\n\t\t0 退出"<<endl; 
cin >> choice; 
switch (choice) 
{ 
case '1': 
cout << "输入图书检索号:"<<endl; 
cin >> bookid; 
cout << "输入图书书名:"<<endl; 
cin >> bname; 
cout<<" 输入作者名:"; 
cin>>bauthor;
cout << "输入图书价格:"<<endl; 
cin >> bprice; 
cout << "输入图书出版社:"<<endl; 
cin >> bplace; 
addbook(bookid,bname,bauthor,bprice,bplace); 
break;
case '2': 
cout << "输入图书检索号:"<<endl; 
cin >> bookid; 
b=query(bookid); 
if (b==NULL) 
{ 
cout << " 信息错误"<<endl; 
break; 
} 
cout << "重新输入书名:"<<endl; 
cin >> bname; 
b->setname(bname); 
break; 
case '3': 
cout <<" 读入图书检索号:"<<endl; 
cin >> bookid; 
b=query(bookid); 
if (b==NULL) 
{ 
cout <<" 信息错误" << endl; 
break; 
} 
b->delbook(); 
break; 
case '4': 
cout << " 读入图书检索号:"<<endl; 
cin >> bookid; 
b=query(bookid); 
if (b==NULL) 
{ 
cout <<" 信息错误"<< endl; 
break; 
} 
b->disp(); 
break; 

default:cout<<"信息错误,请重新输入:"; 
} 
} 

void main()
{ 
char choice; 
int bookid,readerid; 
RDatabase ReaderDB; 
Reader *r; 
BDatabase BookDB; 
Book *b; 



cout <<endl<<endl<<"\t\t\t 图书管理系统\n\n\n"; 

cout <<"\t\t\t1 借还情况\n\n\t\t\t2 图书信息表\n\n\t\t\t3 借阅者信息表\n\n\t\t\t0 退出"<<endl; 
cin >> choice; 
switch (choice) 
{ 
case '1': 
cout <<"\t\t\t1 借书\n\n\t\t\t2 还书\n\n\t\t\t0 退出"<<endl;
cin>>choice;
switch(choice)
{

case '1':
cout <<" 借阅者学号:"; 
cin >>readerid; 
cout <<" 图书检索号:"; 
cin >>bookid; 
r=ReaderDB.query(readerid); 
if (NULL==r) 
{ 
cout <<" 借阅者信息错误,不能借书"<< endl; 
break; 
} 
b=BookDB.query(bookid); 
if (b==NULL) 
{ 

    cout<<"图书信息错误,不能借书"<<endl;
    break;
}
if(b->borrowbook()==0)
{
    cout<<"该图书没有现存,不能借书"<<endl;
    break;
}
r->borrowbook(b->getNo());
break;
case '2':
    cout<<"借阅者学号:";
    cin>>readerid;
    cout<<"图书检索号:";
    cin>>bookid;
    r=ReaderDB.query(readerid);
    if(r==NULL)
    {
        cout<<"借阅者信息错误,不能还书"<<endl;
        break;
    }
    b=BookDB.query(bookid);
    if(b==NULL)
    {
        cout<<"图书信息错误,不能还书"<<endl;
        break;
    }
    b->retbook();
    r->retbook(b->getNo());
    break;
}



case '2':
    BookDB.bookdata();
break; 
case '3': 
ReaderDB.readerdata(); 
break; 
default:cout<<"信息错误,请从新输入:"; 
} 
}

  • 写回答

6条回答 默认 最新

  • threenewbee 2015-01-10 14:23
    关注

    你要让它不退出,需要外侧包一个死循环,参考

    http://codepad.org/XAeNGCSO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证