📄 图书管理.c
字号:
#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)))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -