不知道怎么错了高救命小弟谢谢了
#include <iostream>#include <fstream>
#include <stdio.h>
#include <conio.h>
#define filename "F:\\book.dat"
#define student "F:\\student.dat"
using namespace std;
class Book
{ private:
char booknum[5];
// int booknum;
char bookname[41];
char author[21];
char id[16];
char state[10];
char bookkind[26];
public:int book1num;
Book()
{
//strcpy(booknum,"");
strcpy(bookname,"null");
strcpy(author,"null");
strcpy(id,"null");
strcpy(state,"null");
strcpy(bookkind,"null");
}
void setbookinfo()
{ cin.ignore();
cout<<"请输入书名:"<<endl;
cin.getline(bookname,40);
// if(strlen(bookname)>40)
// {
// cout<<"太长,前40字符个有效\n";
// bookname[40]='\0';
// }
cout<<"请输入书号:"<<endl;
cin.getline(id,25);
// if(strlen(id)>15)
// {
// cout<<"太长,前15字符个有效\n";
// id[15]='\0';
// }
cout<<"请输入书的作者:"<<endl;
cin.getline(author,20);
// if(strlen(author)>20)
// {
// cout<<"太长,前20字符个有效\n";
// author[20]='\0';
// }
cout<<"请输入书的数量:"<<endl;
cin>>book1num;//zj
// if(strlen(booknum)>2)
// {
// cout<<"太长,前2字符个有效\n";
// booknum[2]='\0';
// }
cin.clear();
cin.ignore();
cout<<"请输入书的种类:"<<endl;
cin.getline(bookkind,20);
// if(strlen(bookkind)>25)
// {
// cout<<"太长,前25字符个有效\n";
// bookkind[25]='\0';
// }
cout<<"请输入书的状态:(在馆/借出)"<<endl;
cin.getline(state,10);
// if(strlen(state)>4)
// {
// cout<<"太长,前4字符个有效\n";
// state[4]='\0';
// }
}
void getbookinfo()
{
cout.width(5);
cout<<id;
cout.width(21);
cout<<bookname;
cout.width(15);
cout<<author;
cout.width(15);
cout<<bookkind;
cout.width(5);
cout<<book1num; //zj
cout.width(5);
cout<<state<<endl;
}
char *getid()
{return id;}
char *getbookname()
{return bookname;}
char *getkind()
{return bookkind;}
char *getauthor()
{return author;}
char *getstate()
{return state;}
// char *getnum()
//{return book1num;}
};
class user
{
private:
char studentid[15];
char name[21];
char age[5];
char phonenum[15];
char cclass[10];
char password[21];
public:
void setdetail()
{
cout<<"请输入学号:";
cin.getline(studentid,24);
if(strlen(studentid)>14)
{
cout<<"太长,前14字符个有效\n";
studentid[14]='\0';
}
cout<<"请输入用户名:";
cin.getline(name,31);
if(strlen(name)>20)
{
cout<<"太长,前20字符个有效\n";
name[20]='\0';
}
cout<<"请输入年龄:";
cin.getline(age,10);
if(strlen(age)>4)
{
cout<<"太长,前4字符个有效\n";
age[4]='\0';
}
cout<<"请输入联系电话:";
cin.getline(phonenum,34);
if(strlen(phonenum)>14)
{
cout<<"太长,前20字符个有效\n";
phonenum[14]='\0';
}
cout<<"请输入班级:";
cin.getline(cclass,19);
if(strlen(cclass)>9)
{
cout<<"太长,前9字符个有效\n";
cclass[9]='\0';
}
cout<<"请输入密码:";
cin.getline(password,31);
if(strlen(password)>20)
{
cout<<"太长,前20字符个有效\n";
password[20]='\0';
}
cin.clear();
cin.ignore();
}
void getdetail()
{ cout.width(17);
cout<<"学号";
cout.width(17);
cout<<studentid;
cout.width(21);
cout<<"用户名";
cout.width(21);
cout<<name;
cout.width(10);
cout<<"年龄"<<endl;
cout.width(10);
cout<<age<<endl;
cout.width(17);
cout<<"联系电话";
cout.width(17);
cout<<phonenum;
cout.width(21);
cout<<"班级";
cout.width(21);
cout<<cclass;
cout.width(10);
cout<<"密码";
cout.width(10);
cout<<password<<endl;
}
void setuserinfo()//zj
{
// char n[21];char p[21];
cout<<"\n\n\t\t请输入您的用户名: ";
cin.getline(name,30);
if(strlen(name)>20)
{
cout<<"太长,前20字符个有效\n";
name[20]='\0';
}
cout<<" \n\n\t\t 请输入您的密码: ";
int i;
for(i=0; i <29; i++)
{
password[i]=getch();
if((int)password[i]==13) break;
putchar('*');
}
password[i]='\0';cout<<endl;
//cin.getline(password,30);
if(strlen(password)>20)
{
cout<<"太长,前20字符个有效\n";
password[20]='\0';
}
}
void getuseinfo()
{
cout.width(21);
cout<<name;
cout.width(21);
cout<<password;
}
char *id()
{ return studentid; }
char *username()
{return name;}
char *studentage()
{return age;}
char *studentphonenum()
{return phonenum;}
char *studentpwd()
{return password;}
};
class nomaluser
{ private:
Book book1;//zj
user user1;
public:
void displaybook() //显示所有图书的信息
{
fstream file(filename,ios::in|ios::binary);
if(!file)
{
cout<<"打开失败!!!"<<endl;
return;
}
cout.width(7);
cout<<"书号";
cout.width(21);
cout<<"书名";
cout.width(15);
cout<<"作者";
cout.width(15);
cout<<"种类";
cout.width(5);
cout<<"数量";
cout.width(5);
cout<<"状态"<<endl;
while(file.read((char *)&book1,sizeof(book1)))
{
book1.getbookinfo();
}
file.close();
system("pause");
system("cls");
return;
}
void findbook() //查找图书
{
bool c = false;
char bookName1[20];
cout<<endl;
cout<<"输入你要查找的书名 :";
cin.ignore();
cin>>bookName1;
fstream file(filename,ios::binary|ios::in);
while(file.read((char*)&book1,sizeof(book1)))
{
if(strcmp(book1.getbookname(),bookName1)==0)
{
c=true;
cout<<endl;
book1.getbookinfo();
system("pause");
system("cls");
}
}
file.close();
if(!c)
cout<<"对不起,查无此书!!"<<endl;
system("pause");
system("cls");
return;
}
void madifyuserinfo() //修改图书信息
{
bool a=false;
user user2;
cin.ignore();cin.clear();
user2.setuserinfo();
system("cls");
fstream file(student,ios::binary|ios::in|ios::out);
if(!file)
{
cout<<"文件打开失败!!"<<endl;
return;
}
while(file.read((char*)&user1,sizeof(user1)))
{
if(strcmp(user1.username(),user2.username())==0&&strcmp(user1.studentpwd(),user2.studentpwd())==0)
{
int pos = file.tellg();
a=true;
cout<<" ***************您原来的信息如下*************** "<<endl;
/*cout.width(7);
cout<<"书号";
cout.width(21);
cout<<"书名";
cout.width(15);
cout<<"作者";
cout.width(15);
cout<<"种类";
cout.width(5);
cout<<"数量";
cout.width(5);
cout<<"状态"<<endl;*/
user1.getdetail();
cout<<endl;
cout<<"请重新输入您的信息: "<<endl;
//cin.ignore();
cin.clear();
user1.setdetail();
cout<<" 修改成功"<<endl;
system("pause");
system("cls");
file.seekg(pos-sizeof(user1),ios::beg);
file.write((char *)&user1,sizeof(user1));
file.close();
break;
}
}
}
void displayuserinfo() //显示本人用户信息
{
user1.getdetail();
}
void givebackbook() //还书
{ char sISBN1[16];
bool b = false;
cout<<" 请输入所还书的书籍号";
cin.ignore();
cin>>sISBN1;
fstream file(filename,ios::binary|ios::in);
while(file.read((char*)&book1,sizeof(book1)))
{
if(strcmp(book1.getid(),sISBN1)==0)
{
b = true;
book1.book1num++;
if (book1.book1num>0)
strcpy(book1.getstate(),"在馆");
system("pause");
system("cls");
}
}cout<<"还书成功"<<endl;
if(!b)
{
cout<<"输入了没有的书籍号"<<endl;
system("pause");
system("cls");
}
}
void lendbook() //借书
{
bool a = false;
char bookName1[20];
cout<<"请 输 入 你 要 借 阅 的 书 名 :";
cin.ignore();
cin>>bookName1;
fstream file(filename,ios::binary|ios::in|ios::out);
while(file.read((char*)&book1,sizeof(book1)))
{
if(strcmp(book1.getbookname(),bookName1)==0)
{
cout<<book1.book1num<<endl;
book1.book1num--;
if (book1.book1num==0)
{ strcpy(book1.getstate(),"借出");
}
if (book1.book1num<0)//zj
break;//zj
a = true;//zj
cout<<" 借书成功 "<<endl;
int pos=(-1)*sizeof(book1);
file.seekp(pos,ios::cur);//zj
file.write((char *)&book1,sizeof(book1));
file.close();
cout<<"*************书籍的信息**********"<<endl;
cout<<endl;
book1.getbookinfo();
system("pause");
system("cls");
break;
}
}
if(!a)
{
cout<<"此书已借出或还未入库"<<endl;
system("pause");
system("cls");
}
}
};
class adminstrator
{
private:
user user2;
Book book2;
public:
void displaybook() //显示图书信息
{
fstream file(filename,ios::in|ios::binary);
if(!file)
{
cout<<"打开失败!!!"<<endl;
return;
}
cout.width(7);
cout<<"书号";
cout.width(21);
cout<<"书名";
cout.width(15);
cout<<"作者";
cout.width(15);
cout<<"种类";
cout.width(5);
cout<<"数量";
cout.width(5);
cout<<"状态"<<endl;
while(file.read((char *)&book2,sizeof(book2)))
{
book2.getbookinfo();
}
file.close();
system("pause");
system("cls");
return;
}
void addbook() //增加图书
{
ofstream file(filename,ios::out|ios::app|ios::binary);
if(!file)
{
cout<<"文件打开失败!!"<<endl;
return;
}
book2.setbookinfo();
file.write((char *)&book2,sizeof(book2));
file.close();
system("pause");
system("cls");
return ;
}
void deletebook() //删除图书
{
}
void madifybook() //修改图书信息
{
bool a=false;
char id1[16];
cout<<"请输入要修改的书的编号 :";
fflush(stdin);
cin>>id1;
system("cls");
fstream file(filename,ios::binary|ios::in|ios::out);
if(!file)
{
cout<<"文件打开失败!!"<<endl;
return;
}
while(file.read((char*)&book2,sizeof(book2)))
{
if(strcmp(book2.getid(),id1)==0)
{
int pos = file.tellg();
a=true;
cout<<" ***************原来的书籍信息*************** "<<endl;
cout.width(7);
cout<<"书号";
cout.width(21);
cout<<"书名";
cout.width(15);
cout<<"作者";
cout.width(15);
cout<<"种类";
cout.width(5);
cout<<"数量";
cout.width(5);
cout<<"状态"<<endl;
book2.getbookinfo();
cout<<endl;
cout<<"请重新输入图书信息: "<<endl;
book2.setbookinfo();
cout<<" 修改成功"<<endl;
system("pause");
system("cls");
file.seekg(pos-sizeof(book2),ios::beg);
file.write((char *)&book2,sizeof(book2));
file.close();
break;
}
}
}
void deleteuser() //删除用户
{
}
void displayuser() //显示所有用户
{
fstream file(student,ios::in|ios::binary);
if(!file)
{
cout<<"文件打开失败!!"<<endl;
return;
}
while(file.read((char *)&user2,sizeof(user2)))
{
user2.getdetail();
}
file.close();
return;
}
void findbook() //查找图书
{
bool c = false;
char bookName1[20];
cout<<endl;
cout<<"输入你要查询的书名 :";
cin.ignore();
cin>>bookName1;
fstream file(filename,ios::binary|ios::in);
while(file.read((char*)&book2,sizeof(book2)))
{
if(strcmp(book2.getbookname(),bookName1)==0)
{
c=true;
cout<<endl;
cout.width(7);
cout<<"书号";
cout.width(21);
cout<<"书名";
cout.width(15);
cout<<"作者";
cout.width(15);
cout<<"种类";
cout.width(5);
cout<<"数量";
cout.width(5);
cout<<"状态"<<endl;
book2.getbookinfo();
system("pause");
system("cls");
}
}
if(!c)
cout<<"对不起,查无此书!!"<<endl;
}
};
class usermanager
{
private:user user3;
adminstrator book3;
nomaluser book4;
public:
void register1() //注册
{ fstream file(student,ios::out|ios::app|ios::binary);
if(!file)
{
cout<<"打开文件失败!!"<<endl;
return;
}
cin.ignore();//zj
cin.clear();//zj
user3.setdetail();
file.write((char*)&user3,sizeof(user3));
file.close();
return;
}
void login() //登陆
{
fstream file(student,ios::in|ios::binary);//zj
if(!file)
{
cout<<"文件打开失败!!!!"<<endl;
return;
}
cin.clear();//zj
cin.ignore();//zj
repit: user user4;//zj
user4.setuserinfo();
bool right=false;
// file.write((char*)&user1,sizeof(user1));
// file.close();
while(file.read((char *)&user3,sizeof(user3)))
{
if(strcmp(user3.username(),user4.username())==0&&strcmp(user3.studentpwd(),user4.studentpwd())==0)//zj
{ right=true;
//cout<<"光临!!"<<endl;
if(strcmp(user4.username(),"zj")==0&&strcmp(user4.studentpwd(),"123")==0)
{
cout<<"\n\n\t\t\t∵ 恭喜! ∵ \n\n\t\t\t登录成功! ! \n\n\n"<<endl;
cout<<"欢迎管理员进入!!"<<endl;
_sleep(3000);
system("cls");
bool bIsContinue=true;
char choice;
while(bIsContinue)
{
cout<<"1.查找图书"<<endl;
cout<<"2.增加图书"<<endl;
cout<<"3.修改图书"<<endl;
cout<<"4.删除图书"<<endl;
cout<<"5.显示所有图书信息"<<endl;
cout<<"6.删除用户"<<endl;
cout<<"7.显示所有用户信息"<<endl;
cout<<"8.退出"<<endl;
cin>>choice;
switch(choice)
{
case '1':
book3.findbook();
break;
case '2':
book3.addbook();
break;
case '3':
book3.madifybook();
break;
case '4':
book3.deletebook();
break;
case '5':
book3.displaybook();
break;
case '6':
book3.deleteuser();
break;
case '7':
book3.displayuser();
break;
case '8':
bIsContinue=false;
break;
default:
cout<<"您的输入有误,请重新输入"<<endl;
} //zj
}
}
else
{ cout<<"\n\n\t\t\t∵ 恭喜! ∵ \n\n\t\t\t登录成功! ! \n\n\n"<<endl;
cout<<"欢迎光临!!"<<endl;
_sleep(1000);
system("cls");
char choice1='1';
while(choice1)
{
cout<<"1.显示所有图书信息"<<endl;
cout<<"2.查找图书"<<endl;
cout<<"3.借书图书"<<endl;
cout<<"4.还书图书"<<endl;
cout<<"5.修改用户信息"<<endl;
cout<<"6.显示用户信息"<<endl;
cout<<"7.退出"<<endl;
cin>>choice1;
switch(choice1)
{
case '1':
book4.displaybook();
break;
case '2':
book4.findbook();
break;
case '3':
book4.lendbook();
break;
case '4':
book4.givebackbook();
break;
case '5':
book4.madifyuserinfo();
break;
case '6':
book4.displayuserinfo();
break;
case '7':exit(0);
default:
cout<<"您的输入有误,请重新输入"<<endl;
}
}// break; //zj
}
}
}
if(!right)//这里还有些问题你自己解决
{
cout<<user3.username()<<endl;
cout<<user4.username()<<endl;
// cout<<user1.studentpwd()<<endl;
cout<<"\n\t对不起!!您输入的用户名或密码错误!\n\t请重新输入!!\n";
}
file.close();
return;
}
};
int main()
{
system("color d");
system("cls");
char choice; //zj
usermanager user1;
while(choice!=3)//zj
{
cout<<"1* 新用户注册"<<endl;
cout<<" 2* 登陆"<<endl;
cout<<" 3* 退出 "<<endl;
cout<<"请选择:(1~3)"<<endl;
cin>>choice;
switch(choice)
{
case '1':
user1.register1();
break;
case '2':
user1.login();
break;
case '3':
return 0;
default:
cout<<"您输入错误,请重新输入!!"<<endl;
break;
}
} //zj
system("pause");
return 0;
}