#include <stdlib.h>
#include <conio.h>
struct BOOK
{
int id,usr[10],total,store,days[10];
char name[30],author[20];
}books[100];
/*上面是結(jié)構(gòu)體的定義,用于存放書籍及借書的信息。*/
void page_title(char *menu_item)
{
system("cls");
printf(">>> 圖 書 管 理 系 統(tǒng) <<<nn- %s -nn",menu_item);
}
/*上面是打印頁眉的函數(shù),同時通過參數(shù)menu_item,可以顯示當(dāng)前的狀態(tài)。*/
void return_confirm(void)
{
printf("n按任意鍵返回……n");
getch();
}
/*上面是返回前請求確認(rèn)的函數(shù),以便在返回前觀察結(jié)果*/
int search_book(void)
{
int n,i; printf("請輸入圖書序號:");
scanf("%d",&i);
for(n=0;n<100;n++)
{
if(books[n].id==i)
{
printf("書名:%sn",books[n].name);
printf("作者:%sn",books[n].author);
printf("存數(shù):%dn",books[n].store);
printf("總數(shù):%dn",books[n].total);
return n;
} }
printf("n輸入錯誤或無效圖書序號.n");
return -1;
}
/*上面的函數(shù)是在數(shù)組中找到圖書號匹配的記錄,
顯示其信息并返 回數(shù)組下標(biāo),如果找不到相應(yīng)記錄則提示錯誤并返回-1。*/
void book_out(void)
{
int n,s,l,d; page_title("借閱圖書");
if((n=search_book())!=-1&&books[n].store>0)
{
printf("請輸入借書證序號:");
scanf("%d",&s);
printf("請輸入可借天數(shù):");
scanf("%d",&d);
for(l=0;l<10;l++)
{
if(books[n].usr[l]==0)
{
books[n].usr[l]=s; books[n].days[l]=d; break;
}
}
books[n].store--;
}
if(n!=-1&&books[n].store==0)
printf("此書已經(jīng)全部借出.n");
return_confirm();
} /*上面是借書的函數(shù),首先調(diào)用找書函數(shù)*/
void book_in(void)
{
int n,s,l; page_title("歸還圖書");
if((n=search_book())!=-1&&books[n].store<books[n].total)
{
printf("借閱者圖書證列表:n");
for(l=0;l<10;l++)
if (books[n].usr[l]!=0)
printf("[%d] - %d天n",books[n].usr[l],books[n].days[l]);
printf("請輸入借書證序號:");
scanf("%d",&s);
for(l=0;l<10;l++)
{
if(books[n].usr[l]==s)
{
books[n].usr[l]=0;
books[n].days[l]=0;
break;
}
}
books[n].store++;
}
if(n!=-1&&books[n].store==books[n].total)
printf("全部入藏.n");
return_confirm();
}
void book_add(void)
{
int n;
page_title("注冊新書");
for(n=0;n<100;n++)
if(books[n].id==0) break;
printf("序號:");
scanf("%d",&books[n].id);
printf("書名:");
scanf("%s",&books[n].name);
printf("作者:");
scanf("%s",&books[n].author);
printf("數(shù)量:");
scanf("%d",&books[n].total);
books[n].store=books[n].total;
return_confirm();
}
void book_del(void)
{
int n;
page_title("注銷舊書");
if((n=search_book())!=-1) books[n].id=0;
printf("該書已注銷.n");
return_confirm();
}
void main(void)
{
menu: page_title("操作選單");
printf("請用數(shù)字鍵選擇操作nn");
printf("1 借閱圖書n2 歸還圖書nn");
printf("3 注冊新書n4 注銷舊書nn");
printf("n0 退出n");
switch(getch())
{
case '1' : book_out();break;
case '2' : book_in();break;
case '3' : book_add();break;
case '4' : book_del();break;
case '0' : exit(0);
}
goto menu;
}
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<stdio.h>
using namespace std;
const int Maxb=10000; //最多的圖書
class Book//圖書類
{
int tag; //刪除標(biāo)記1:已刪0:未刪
int number; //ISBN書號
char name[20]; //書名
char author[10]; //主編
char number2[10];//版次
char position[20];//出版社
char time[20];//出版年
int price;//定價
int onshelf; //是否在架1:在架0:已借
public:
Book() {}
char *getname() { return name; } //獲取姓名
int getnumber() { return number; } //獲取ISBN書號
int gettag() { return tag; } //獲取刪除標(biāo)記
char *getauthor() {return author;} //獲取主編
char *getnumber2() {return number2;} //獲取版次
char *getposition() {return position;} //獲取出版社
char *gettime() {return time;} //獲取出版年
char getprice() {return price;} //獲取圖書定價
void delbook() { tag=1; } //刪除圖書
void addbook(int n,char *na,char *au,char *n2,char *da,char *ti,int pr) //增加圖書
{
tag=0;
number=n;
price=pr;
strcpy(name,na);
strcpy(author,au);
strcpy(number2,n2);
strcpy(position,da);
strcpy(time,ti);
onshelf=1;
}
void disp() //輸出圖書
{
cout << setw(10) << number << setw(10) << name << setw(10)
<< setw(10)<<author<<setw(10)<<number2<<setw(10)<<position<<setw(10)<<time<<setw(10)<<price<<endl;
}
};
class BDatabase //圖書庫類
{
int top; //圖書記錄指針
Book book[Maxb]; //圖書記錄
public:
BDatabase() //構(gòu)造函數(shù),將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,char *au, char *n2, char *da,char *ti,int pr) //增加圖書
{
Book *p=search1(n);
if (p==NULL)
{
top++;
book[top].addbook(n,na,au,n2,da,ti,pr);
return 1;
}
return 0;
}
Book *search1(int bookid) //查找圖書
{
for (int i=0;i<=top;i++)
if (book[i].getnumber()==bookid &&
book[i].gettag()==0)
return &book[i];
return NULL;
}
Book *search2(int bookid,char *name) //按書名查找圖書
{
for(int i=0;i<=top;i++)
if(strcmp(book[i].getname(),name)==0)
{bookid=book[i].getnumber();
return &book[i];
}
return NULL;
}
Book *search3(int bookid,char *author) //按主編查找圖書
{
for(int i=0;i<=top;i++)
if(strcmp(book[i].getauthor(),author)==0)
{bookid=book[i].getnumber();
return &book[i];
}
return NULL;
}
void bookdata(); //圖書庫維護(hù)
void disp()
{
cout<<setw(10)<<"圖書書號"<<setw(10)<<"圖書名字"<<setw(10)<<"圖書主編"<<setw(10)<<"版次"<<setw(10)<<"出版社"<<setw(10)<<"出版年"<<setw(10)<<"價格"<<endl<<endl<<endl<<endl;
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
book[i].disp();
}
~BDatabase() //析構(gòu)函數(shù),將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;
int choice2=1;
int choice3=1;
int choice4;
char bname[40];
char editor[40];
char banci[40];
char position[40];
char year[40];
int value;
int bookid;
Book *b;
while (choice!=0)
{
cout<<endl<<endl;
cout<<" **************************** "<<endl;
cout<<" **** 1添加圖書 **** "<<endl;
cout<<" **** 3 刪除圖書 **** "<<endl;
cout<<" **** 4 圖書查詢 **** "<<endl;
cout<<" **** 5 顯示圖書 **** "<<endl;
cout<<" **** 6 全部刪除 **** "<<endl;
cout<<" **** 7 借書 **** "<<endl;
cout<<" **** 8 還書 **** "<<endl;
cout<<" **** 0 退出 **** "<<endl;
cout<<" ****************************"<<endl<<endl;
cout<<endl<<"請按鍵選擇您需要的操作:";
cin>>choice;
while(choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6&&choice!=0){
cout<<endl<<" ** 您輸入的編號在菜單里不存在,請重新輸入 **"<<'a'<<endl<<endl;
cout<<" 請選擇您需要的操作:";
cin>>choice;
}
switch (choice)
{
case 1:
cout <<"輸入ISBN書號(一定為數(shù)字否則會異常):";
cin >> bookid;
cout <<"輸入書名:";
cin >> bname;
cout <<"輸入主編:";
cin >>editor;
cout <<"輸入版次(一定為數(shù)字否則會異常):";
cin>>banci;
cout<<"輸入出版社:";
cin>>position;
cout<<"輸入出版年(一定為數(shù)字否則會異常):";
cin>>year;
cout<<"輸入價格(一定為數(shù)字否則會異常):";
cin>>value;
addbook(bookid,bname,editor,banci,position,year,value);
cout<<"ISBN書號"<<bookid<<"添加成功,如需返回主菜單請按1,退出系統(tǒng)請按0(一定要輸入數(shù)字)";
cin>>choice4;
while (choice4!=0&&choice4!=1)
{
cout<<"輸入錯誤請重新輸入"<<endl;
cin>>choice4;}
switch (choice4)
{
case 1:
choice=1;
break;
case 0:
choice=0;
break;}
break;
case 3:
cout << " 輸入ISBN書號:";
cin >> bookid;
b=search1(bookid);
if (b==NULL)
{
cout << " 該圖書不存在" << endl;
break;
}
b->delbook();
break;
case 4:
cout<<"查找方式:"<<endl<<"1按ISBN書號查詢 2按書名查詢 3按主編查詢 0退出:";
cin>>choice3;
switch(choice3)
{
case 1:
{cout << " 輸入ISBN書號:"; //按ISBN書號查詢
cin >> bookid;
b=search1(bookid);
if (b==NULL)
{
cout << " 該圖書不存在" << endl;
break;
}
b->disp();
}
break;

case 2:
{
cout<<"請輸入書名:";
cin>>bname;
b=search2(bookid,bname);
if(b==NULL)
{
cout<<"該圖書不存在??!"<<endl;
break;
}
b->disp();
}
break;
case 3:
{
cout<<"請輸入主編:";
cin>>editor;
b=search3(bookid,editor);
if(b==NULL)
{
cout<<"該主編不存在!"<<endl;
break;
}
b->disp();
}
break;
}
break;
case 5:
disp();
break;
case 6:
clear();
break;
}
}
cout<<endl<<" ****** 慢走 ******"<<endl<<endl<<endl;
};
int main()
{
BDatabase BookDB;
cout<<endl<<endl<<endl;
cout<<" Welcome to the library of SCU "<<endl;
cout<<" 歡 迎 來 到 四 川 大 學(xué) 圖 書 館 "<<endl;
cout<<endl<<endl<<"請輸入0進(jìn)入圖書館"<<endl;
int w;
cin>>w;
if(w==0)
BookDB.bookdata();
system("pause");
return 0;
}
愛華網(wǎng)



