c++程序,急求高手帮忙!!!

2024-12-16 14:21:00
推荐回答(3个)
回答1:

//修改好了,vc6下编译通过
//修改:qmroom
//2008-11-02 1:19
//blog:http://blog.csdn.net/qmroom
//Email:qmroom#126.com        #=@

#include 
#include 
#include 
#pragma warning(disable:4786)
#include 
#include 
using std::vector;
using std::string;

typedef long BOOL;
typedef char* LPSTR;
#define FALSE 0
#define TRUE 1
#define SUCCEEDED(Status) ((long)(Status) >= 0)
#define FAILED(Status) ((long)(Status)<0)
#define BUFSIZ 512
const int Maxr=100;// 最多的读者 
const int Maxb=100;// 最多的图书 
const int Maxbor=5;//每位读者最多借5本书 

//
// 这中间没有改动的类就不贴出来了,太多了,改了的贴一下
//

class Manager
{
protected:
    vector    admin;
    vector    pwd;
protected:
    virtual int FindIndex(string sName)
    {
        for (int i=0; i < (int)admin.size(); i++)
        {
            if (sName == admin[i])
            {
                return i;
            }
        }
        return -1;
    }
public:
    Manager(){}
    virtual ~Manager(){}
    virtual BOOL Add(string sName, string sPwd)
    {
        if (SUCCEEDED(FindIndex(sName)))
            return FALSE;
        admin.push_back(sName);
        pwd.push_back(sPwd);
        return TRUE;
    }
    virtual BOOL Delete(string sName)
    {
        int index = FindIndex(sName);
        if (SUCCEEDED(index) && index < (int)pwd.size())
        {
            admin.erase(admin.begin()+index);
            pwd.erase(pwd.begin()+index);
            return TRUE;
        }
        return FALSE;
    }
    virtual BOOL Modify(string sName)
    {
        int index = FindIndex(sName);
        if (SUCCEEDED(index) && index < (int)pwd.size())
        {
            string sPwd;
            cout<<"  请输入管理员 "<            sPwd = GetInput();
            pwd[index] = sPwd;
            return TRUE;
        }
        return FALSE;
    }
    virtual int Find(string sName)
    {
        int index = FindIndex(sName);
        if (SUCCEEDED(index))
        {
            cout<<"\t"<        }
        return index;
    }
    virtual BOOL VirfyPassword(string sName, string sPwd)
    {
        int index = FindIndex(sName);
        if (SUCCEEDED(index) && index < (int)pwd.size())
            return sPwd == pwd[index];
        return FALSE;
    }
    inline void Show()
    {
        cout<<"\t"<<"人名"<<"\t"<<"密码"<        for (int i=0; i < (int)admin.size(); i++)
        {
            cout<<"\t"<        }
    }
    inline void Clear()
    {
        admin.clear();
        pwd.clear();
    }
    static string GetInput()
    {
        char buf[BUFSIZ];
        cin >> buf;
        return (const char *)buf;
    }
    inline string InputName()
    {
        string sName;
        cout<<"  请输入管理员名称:";
        sName = GetInput();
        return sName;
    }
    inline string InputPwd()
    {
        string sPwd;
        cout<<"  请输入管理员密码:";
        sPwd = GetInput();
        return sPwd;
    }
    inline int Count()
    {
        return admin.size();
    }
    inline void Menu()
    {
        int choice=1;
        string sName;
        while (choice!=0) 
        { 
            cout<<"  管理员维护 1:新增 2 更改密码 3:删除 4:查找 5:显示 6:全删 0: 退出 =>"; 
            
            cin>>choice; 
            switch(choice)
            {
            case 1:
                sName = InputName();
                if (!Add(sName, InputPwd()))
                    cout<<"添加失败,人名已经存在"<                break;
            case 2:
                if (!Modify(InputName()))
                    cout<<"没有找到"<                break;
            case 3:
                if (!Delete(InputName()))
                    cout<<"没有找到"<                break;
            case 4:
                if (!Find(InputName()))
                    cout<<"没有找到"<                break;
            case 5:
                Show();
                break;
            case 6:
                Clear();
                break;
            }
        }

    }
};

void main() 
{
    int choice=1,bookid,readerid; 
    RDatabase ReaderDB; 
    Reader *r; 
    BDatabase BookDB; 
    Book *b; 
    Manager    admin;
    string sName, sPwd;
    
    admin.Add("admin","admin");
    do
    {
        cout<<"  请输入管理员名称[默认名称admin]:";
        sName = Manager::GetInput();
        cout<<"  请输入管理员密码[默认密码admin]:";
        sPwd = Manager::GetInput();
        if (admin.VirfyPassword(sName, sPwd))
        {
            break;
        }
        else
        {
            cout<<"名称或密码错误,请重新输入[Ctril+C 退出程序]"<        }
    }while(1);

    while (choice!=0) 
    { 
        cout<<" 图书馆结束系统"<        cout<<" 1:借 书"<        cout<<" 2:还 书 "<        cout<<" 3:图书维护 "<        cout<<" 4:读者维护 "<        cout<<" 5:管理员维护 "<        cout<<" 0:离 开=>"<        cin>>choice; 
        switch(choice) 
        { 
        case 1: 
            cout<<" 借书 读者编号:"; 
            cin>>readerid; 
            cout<<"图书编号: "; 
            cin>>bookid; 
            r=ReaderDB.query(readerid); 
            if(r=NULL) 
            { 
                cout<<"不存在该读者,不能借书"<                break; 
            } 
            b=BookDB.query(bookid); 
            if(b==NULL) 
            { 
                cout<<"不存在该图书,不能借书"<                break; 
            } 
            if(b->borrowbook()==0) 
            { 
                cout<<"该图书已经借出,不能借书"<                break; 
            } 
            r->borrowbook(b->getno()); 
            break; 
        case 2: 
            cout<<"还书 读者编号:"; 
            cin>>readerid; 
            cout<<"图书编号:"; 
            cin>>bookid; 
            r=ReaderDB.query(readerid); 
            if(r=NULL) 
            { 
                cout<<"不存在该读者,不能还书"<                break; 
            } 
            b=BookDB.query(bookid); 
            if(b==NULL) 
            { 
                cout<<"不存在该图书,不能借书"<                break; 
            } 
            b->retbook(); 
            r->retbook(b->getno() ); 
            break; 
        case 3: 
            BookDB.bookdata(); 
            break; 
        case 4: 
            ReaderDB.readerdata(); 
            break; 
        case 5:
            admin.Menu();
            break;
        } 
    } 
}

回答2:

代码:
#include
#include
#include
#include
const int Maxr=100;// 最多的读者
const int Maxb=100;// 最多的图书
const int Maxbor=5;//每位读者最多借5本书
class Reader // 读者类
{
int tag ;//删除标记
int no;//读者编号
char name[50];// 读者姓名
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 borbook[i]=0;
}
void borrowbook(int bookid)//借书操作
{
for(int i=0;i {
if( borbook[i]==0)
{
borbook[i]=bookid;
return;
}
else
cout<<"您已经借满5本,不能再借!"< }
}
int retbook(int bookid)//还书操作
{
for (int i=0;i {
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
void disp()//输出读者信息
{
cout< <<"借书编号:[";
for (int i=0;i< Maxbor;i++)
if(borbook[i]!=0)
cout< cout<<"]"< }
};

class RDatabase // 读者库类
{ int top ;//读者纪录指针
Reader read[Maxr];//读者纪录

public:
RDatabase()//构造函数,将reader.txt读到read[]中
{
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;//000000000
}
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() //析构函数,将read[]写入reader.txt文件中
{
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()
{
int choice=1;
char rname[20];
int readerid;
Reader *r;
while(choice!=0)
{
cout<<"读者维护 1:新增 2:更改 3:删除 4:查找 5:显示 6:全删 0:退出 =>";
cin>>choice;
switch(choice)
{
case 1:
cout<<" 输入读者编号:";
cin>>readerid;
cout<<" 输入读者姓名:";
cin>>rname;
addreader(readerid,rname);
break;
case 2:
cout<<" 输入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<" 该读者不存在"< break;
}
cout<<" 输入新的姓名:";
cin>>rname;
r->setname(rname);
break;
case 3:
cout<<" 输入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<" 该读者不存在"< break;
}
r->delbook();
break;
case 4:
cout<<" 输入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<" 该读者不存在"< break;
}
r->disp();
break;
case 5:
disp();
break;
case 6:
clear();
break;
}

}
}

class Book //图书类
{
int tag; //删除标志 1:已删 0:未删
int no;//图书编号
char name[50];//书名
int onshelf; //是否在架 1:在架 0:已借
public:
Book(){}
char *getname(){return name;}//获取姓名
int getno(){return no;}//获取图书编号
int gettag(){return tag;} //获得删除标志
void setname(char na[])//设置书名
{
strcpy(name,na);
}
void delbook() { tag=1;}// 删除图书
void addbook(int n,char*na)//增加图书
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}

int borrowbook()//借书操作
{
if(onshelf==1)
{
onshelf=0;//...........................
return 1;
}
return 0;
}
void retbook()//还书操作
{
onshelf=1;
}
void disp() //输出图书
{
cout<}
};

class BDatabase //图书库类
{
int top; //图书记录指针
Book book[Maxb]; //图书记录
public:
BDatabase() //构造函数,将book.txt读到book[]中
{
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) //增加图书
{
Book *p=query(n);
if(p==NULL)
{
top++;
book[top].addbook(n,na);
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() //析构函数,将book[]写入book,txt文件中
{
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()
{
int choice=1;
char bname[50];
int bookid;
Book *b;
while (choice!=0)
{
cout<<"图书维护 1:新增 2 更改 3:删除 4:查找 5:显示 6:全删 0: 退出 =>";

cin>>choice;
switch(choice)
{
case 1:
cout<<"输入图书编号";
cin>>bookid;
cout<<"输入图书书名";
cin>>bname;
addbook(bookid,bname);
break;
case 2:
cout<<"输入图书编号";//----------------------------------------------------
cin>>bookid;
b=query(bookid);
if(b=NULL)
{
cout<<"该图书不存在"< break;
}
cout<<"输入新的书名:";
cin>>bname;
b->setname(bname);
break;
case 3:
cout<<"输入图书编号:";
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"< break;
}
b->delbook();
break;
case 4:
cout<<"输入图书编号:";

cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"< break;
}
b->disp();
break;
case 5:
disp();
break;
case 6:
clear();
break;
}
}
}

void main()
{

int choice=1,bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
Book *b;
while (choice!=0)
{
cout<<" 图书馆结束系统"< cout<<" 1:借 书"< cout<<" 2:还 书 "< cout<<" 3:图书维护 "< cout<<" 4:读者维护 "< cout<<" 0:离 开=>"< cin>>choice;
switch(choice)
{
case 1:
cout<<" 借书 读者编号:";
cin>>readerid;
cout<<"图书编号: ";
cin>>bookid;
r=ReaderDB.query(readerid);
if(r=NULL)
{
cout<<"不存在该读者,不能借书"< break;
}
b=BookDB.query(bookid);
if(b==NULL)
{
cout<<"不存在该图书,不能借书"< break;
}
if(b->borrowbook()==0)
{
cout<<"该图书已经借出,不能借书"< break;
}
r->borrowbook(b->getno());
break;
case 2:
cout<<"还书 读者编号:";
cin>>readerid;
cout<<"图书编号:";
cin>>bookid;
r=ReaderDB.query(readerid);
if(r=NULL)
{
cout<<"不存在该读者,不能还书"< break;
}
b=BookDB.query(bookid);
if(b==NULL)
{
cout<<"不存在该图书,不能借书"< break;
}
b->retbook();
r->retbook(b->getno() );
break;
case 3:
BookDB.bookdata();
break;
case 4:
ReaderDB.readerdata();
break;
}
}
}

回答3: