c++图书管理系统 有做过的来
2014-04-27 来自:雨华 1 人回应

文件读写有问题,结构体数据保存时包含了地址,怎么区分开啊?
// new2.cpp : 定义控制台应用程序的入口点。
//

// new.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
#include "string"
#include
#include
#include
#include "fstream"
#define UNIT_COST 0.1 //每本书每天0.1元
#define MAX_DAY 60 //最长借阅 60天
#define MAX_RENEW 30 //续借30天
#define MAX_RENEW_NUM 1 //续借次数 1
using namespace std;
typedef struct BOOK
{
char sheet_number[10];//编号 名称 类别 作者 出版社 出版时间 价格 数量 备注
char name[10];
char writer[10];
char press[10];
char publication_date[10];
int sort;
int price;
int note;
struct BOOK *next;
}Book,*Booklist;
BOOK *a;
struct Password
{
string name;//用户名
string password;//密码
//struct Password *next;
}pass={"admin","123456"},*Passlist;
Password *c;
typedef struct READER
{
char card_number[10];//读者证号 姓名 单位 当前借阅数 密码
char name[10];
char unit[10];
int current_borrow;
char password[6];
struct READER *next;
}*Reader;
READER *b;
class Log_in
{
public:
int Administrator_Load();//管理员登陆
int Reader_Load(Reader &b);//读者登陆
int Log();//登陆界面
int Borrow_Book(Booklist &a,Reader &b);//借书
int Return_Book(Booklist &a,Reader &b);//还书
void Search_Book(Booklist &a);//图书查询
void Load_Book(Booklist &a);
void Delete_Book(Booklist &a);
void Manage_load(Reader &b);
void Manage_Delet(Reader &b);
private:

bool Book_State(string sheet_number);//图书状态 借/还
int pay_cost;//缴费

};
int Log_in::Administrator_Load()
{
Password *p;
p=&pass;
int n=0,f=0;
int i=1;
string na,pas;
while(i!=3)
{
cout<<"--------------------------------------------------------------------------------"< cout cout //cout cin>>na;
cout< cout cin>>pas;
cout< if(na!=p->name||pas!=p->password)
{
cout<<"输入错误,重新输入!"<<endl;
system("cls");
i++;
}
else
{
cout<<"登录成功!"<<endl;
system("cls");
f=1;
}

if(i==3)break;
else
    if(f==1)
    {
while(n!=3)
{
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"                                  管理面板                                       "<<endl;
cout<<"          1.增添读者                                      2.删除读者              "<<endl;
cout<<"          3.增添图书                                      4.删除图书                    "<<endl;
cin>>n;
system("cls");
switch(n)
{
case 1:Manage_load(b); system("cls");break;
case 2:Manage_Delet(b);system("cls");break;
case 3:Load_Book(a); system("cls");break;
case 4:Delete_Book(a);system("cls");break;
case 5:cout<<" "<<endl;break;

}
}break;
}break;
}

system("cls");
return 0;

}
BOOK *Creat_BOOK() //初试化图书链表
{
BOOK *p;

p=new BOOK;
if(!p) exit(1);
p->next=NULL;
return p;
}
READER *Creat_READER() //初试化图书链表
{
READER *p;

p=new READER;
if(!p) exit(1);
p->next=NULL;
return p;
}
int Log_in::Reader_Load(Reader &b)
{

READER p;
//p=new READER;
//p=b;
ifstream file2("Reader.dat",ios::binary|ios::in);
if(!file2)
cerr<<"file2 error!"<<endl;
int n=0,f=0;
int i=1;
string na,pas;
while(i!=3)
{
system("cls");
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"                                  读者登录                                       ";
cout<<"          证件号:                                                                "<<endl;
//cout<<"                                                     按 3 返回上级菜单     "<<endl;
cin>>na;
cout<<endl;
cout<<"          密码:                                                                 ";
cin>>pas;
cout<<endl;
while(!file2.eof())
{
    file2.read((char*)&p,sizeof(p));
    cout<<p.card_number<<endl;

if(na!=p.card_number&&pas!=p.password)
    {cout<<"输入错误,重新输入!"<<endl; system("cls");i++;}
else 
    {
        cout<<"登录成功!"<<endl;
         system("cls");
        f=1;
        break;
    }
    }
}
    if(f==1)
    {
while(n!=4)
{
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"                                  图书管理                                       "<<endl;
cout<<"          1.查询图书                                      2.借书              "<<endl;
cout<<"          3.还书                                          4.返回上级菜单                "<<endl;
cin>>n;
switch(n)
{
case 1:Search_Book(a);cout<<endl; system("cls");break;
case 2:Borrow_Book(a,b);cout<<endl; system("cls");break;
case 3:Return_Book(a,b);cout<<endl; system("cls");break;
case 4:break;

}
}
    }

system("cls");
file2.close();
return 0;

}
int Log_in::Log()
{
int n=0;
while(n!=9)
{
cout<<"--------------------------------------------------------------------------------"< cout cout cin>>n;
system("cls");
switch(n)
{
case 1:Administrator_Load();break;
case 2:Reader_Load(b);break;

}
}
return 0;

}
class Info
{
public:
BOOK *bok;
READER *rea;
//Info():BOOK(bok),READER(rea){}
void Load_Book(Booklist &a);
void Delete_Book(Booklist &a);
void Manage_load(Reader &b);
void Manage_Delet(Reader &b);
int Return_Book(Booklist &a,Reader &b);
private:
};
void Log_in::Search_Book(Booklist &a)
{
BOOK *p;
p=a->next;
char str[10];
cout<<"input the number: ";
cin>>str;
for(;p!=NULL;p=p->next)
{
if(strcmp(p->sheet_number,str)==0)
{
cout<<"编号 名称 类别 作者 出版社 出版时间 价格 数量 备注"< coutsheet_number<<" "<name<<" "<sort<<" "<writer<<" "<press<<" "<publication_date<<" "<price<<" "<note;
}

}

}

void Log_in::Load_Book(Booklist &a) //借阅时以编号作为主键

{
BOOK p;
p=new BOOK;
//fstream file1;
ofstream file1("book.dat",ios::binary|ios::out);
if(!file1)
cerr<<"file1 error!"< //p=new BOOK;
//if(!p) exit(1);
if(a!=NULL)
{
cout cin>>p->sheet_number;
cout<<" 名称:";
cin>>p->name;
cout<<" 数量:";
cin>>p->sort;
cout<<" 出版社名称:";
cin>>p->press;
cout<<" 作者:";
cin>>p->writer;
cout<<" 出版时间:";
cin>>p->publication_date;
cout<<" 价格:";
cin>>p->price;
p->note=1; //1表示没有借出
}
file1.write((char
)&p,sizeof(p));

file1.close();

}
/*
void Info::load_Book()
{
BOOK p;
p=bok->next;
cout<<"输入书的 编号 名称 类别 作者 出版社 出版时间 价格 数量 备注"< cin>>p->sheet_number>>p->name>>p->sort>>p->writer>>p->press>>p->publication_date>>p->price>>p->amount>>p->note;
cout<sheet_number<<" "<name<<" "<sort<<" "<writer<<" "<press<<" "<publication_date<<" "<price<<" "<amount<<" "<note;
}
*/
void Log_in::Delete_Book(Booklist &a)
{
BOOK *p;
fstream file1;
file1.open("book.dat",ios::in|ios::out);
if(!file1)
cerr<<"file1 error!"< p=a;
char num[10];
cout cin>>num;
while(p->next!=NULL)
{
file1.read((char
)&p,sizeof(p));
if(p->sheet_number==num)
delete p;
}
file1.write((char*)&p,sizeof(p));
file1.close();
}
int Log_in::Borrow_Book(Booklist &a,Reader &b)
{
BOOK p;
p=new BOOK;
READER *r;
r=new READER;
fstream file1,file2;
file1.open("book.dat",ios::in|ios::out);
if(!file1)
cerr<<"file1 error!"< file2.open("Reader.dat",ios::in|ios::out);
if(!file2)
cerr char al[10];
char card[10];
cout cin>>al;
while(p->next!=NULL)
{
file1.read((char
)&p,sizeof(p));
if((strcmp(p->sheet_number,al)==0)&&(p->sort>0))
{
cout<<"请输入读者证号:"< cin>>card;
while(r->next!=NULL)
{
file2.read((char*)&r,sizeof(r));
if((strcmp(r->card_number,card)==0)&&(r->current_borrow {
p->sort-=1;
r->current_borrow+=1;
return 0;
}
else
{
if(r->current_borrow>=MAX_DAY)
cout<<"读者已达到最大借阅数!"< if(strcmp(r->card_number,card)!=0)
cout<<"读者证号错误!"< return 0;
}
}
}
else
{
if((p->sort)==0)
cout<<"本书已全部借出!"< if(strcmp(p->sheet_number,al)!=0)
cout<<"对不起本书不存在!"< return 0;
}
}
file1.write((char*)&p,sizeof(p));
file2.write((char*)&r,sizeof(r));
file1.close();
file2.close();
return 0;
}
int Log_in::Return_Book(Booklist &a,Reader &b)
{
BOOK *p;
READER *r;
p=new BOOK;
r=new READER;
fstream file1,file2;
file1.open("book.dat",ios::in|ios::out);
if(!file1)
cerr file2.open("Reader.dat",ios::in|ios::out);
if(!file2)
cerr char al[10];
char card[10];
cout cin>>al;
while(p->next!=NULL)
{
if(strcmp(p->sheet_number,al)==0)
{
cout<<"请输入读者证号:"< cin>>card;
while(r->next!=NULL)
{
if(strcmp(r->card_number,card)==0)
{
p->sort+=1;
r->current_borrow-=1;
return 0;
}
else
{

                  if(strcmp(r->card_number,card)!=0)
                      cout<<"读者证号错误!"<<endl;
                  return 0;
              }
          }
   }
    else
    {
        if(strcmp(p->sheet_number,al)!=0)
            cout<<"对不起本书不存在!"<<endl;
        return 0;
    }
    return 0;
}
file1.write((char*)&p,sizeof(p));
file2.write((char*)&r,sizeof(r));
file1.close();
file2.close();
return 0;

}
void Log_in::Manage_load(Reader &b)
{
READER p;
//p=b;
//fstream file2;
p=new READER;
fstream file2("Reader.dat",ios::in|ios::out);
if(!file2)
cerr<<"file2 error!"< if(b->next==NULL)
{
cout<<" 读者证号:";
cin>>p->card_number;
cout<<" 姓名:";
cin>>p->name;
cout<<" 单位:";
cin>>p->unit;
cout<<" 当前借阅数:";
cin>>p->current_borrow;
cout<<" 密码:";
cin>>p->password;
}
file2.write((char
)p,sizeof(*p));
file2.close();
}
void Log_in::Manage_Delet(Reader &b)
{
READER p;
p=b;
fstream file2;
file2.open("Reader.dat",ios::in|ios::out);
if(!file2)
cerr<<"file2 error!"< char card[10];
cout cin>>card;
for(;p!=NULL;p=p->next)
{
if(p->next!=NULL&&(p->card_number==card))
delete p;
}
file2.write((char
)&p,sizeof(p));
file2.close();
}
int _tmain(int argc, _TCHAR* argv[])
{
// fstream file1,file2,file3;
// BOOK *p1;
// READER *p2;
//Password *p33;
a=Creat_BOOK();
b=Creat_READER();
//c=Creat_Password();
Log_in lo;
lo.Log();
return 0;
}

2014-05-11 来自:短发妮子

CSDN移动问答
http://blog.csdn.net/qq2399431200/article/details/9003578

我曾今写的C语言图书馆管理系统,你可以参考下里面的。其实你的C++版和我这个没多大区别。

您的回应

你还未登陆,不能回应!登陆