源代码如下:
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<stdio.h>
using namespace std
const int maxb=10000 //最多的图书
class book//图书类
{
int tag //删除标记1:已删0:未删
int number //isbn书号
char name[20] //书名
char author[10]//主编
char number2[10]//版次
char position[20]//出版社
char time[20]//出版年
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
}
扩展资料
1、源程序中,很多符号都是成对匹配出现的,为避免遗漏必须配对使用的符号。
2、用花括号括起来的部分,但从程序结构清晰,便于阅读、理解、维护的角度出发,建议在书写程序时应遵循以下规则,以养成良好的编程习惯。
3、一个说明或一条语句占一行,与该结构开始处的左花括号对齐。
#include#include#include#include#defineMax2typedefstructbook//书的信息{charbook_num[10]//书号charbook_name[20]//书名charbook_writer[10]//作者intbook_xy//书的剩余数量intbook_kc//书的总数量structbook*next}BKtypedefstructborrow//被借书的信息{charborrow_book_num[10]//借书书号charlimit_date[8]//借书归还日期}BOtypedefstructreader//读者信息{charreader_num[10]//读者号charreader_name[10]//读者姓名intright//已借数量BOborrow[Max]//已借书信息structreader*next}RDBK*h_bookRD*h_readervoidLogin()intMenu()voidInit()voidInit_book()voidMenu_select()voidInsert_New_Book()voidFind_Book()voidadd_reader()voidPrint_reader()voidPrint_book()voidBorrow_Book()voidReturn_Book()voidSave()voidSave_Book()voidSave_Reader()voidLoad()voidLoad_Reader()voidLoad_Book()voidLogin(){system("cls")printf("\n\n\n\t\t\t欢迎使用图书管理系统\n")printf("\n\n\t\t本程序分析者秦义学号:200907014122\n\n\n\n\n\n\n")//以前是设计者xxxxprintf("\n\n\n\t\t按任意键进入系统")getch()system("cls")}intMenu()/*主菜单*/{intdmprintf("\n\t\t图书管理系统主菜单\n")printf("=================================================\n")printf("☆\t0----退出系统☆\n")printf("☆\t1----采编入库☆\n")printf("☆\t2----登记读者☆\n")printf("☆\t3----借阅登记☆\n")printf("☆\t4---还书管理☆\n")printf("☆\t5---查询图书信息☆\n")printf("☆\t6----查询读者信息☆\n")printf("=================================================\n")printf("请选择相应的代码:")for(){scanf("%d",&dm)if(dm6)printf("\n错误!请重新输入:")elsebreak}returndm}voidMenu_select()/*主菜单选择函数*/{for(){switch(Menu())/*功能选择*/{case0:system("cls")Save()printf("\n\n\t文件保存成功!\n")printf("\n\n\t欢迎下次使用本系统!\n")getch()exit(0)case1:Insert_New_Book()breakcase2:add_reader()breakcase3:Borrow_Book()breakcase4:Return_Book()breakcase5:Print_book()breakcase6:Print_reader()breakdefault:printf("\n错误!")exit(0)}}}voidInit()/*初始化*/{Init_book()}voidInit_book()/*初始化图书信息*/{BK*p0printf("\n图书初始化开始,请输入图书信息..\n包括编号.书名.数量..\n")p0=(BK*)malloc(sizeof(BK))h_book=p0printf("\n请输入图书信息:\n")printf("图书编号:")/*输入图书编号(唯一)*/scanf("%s",p0->book_num)printf("图书名称:")/*输入图书名称*/scanf("%s",p0->book_name)printf("图书作者:")/*输入图书作者*/scanf("%s",p0->book_writer)printf("图书数量:")/*输入图书数量*/scanf("%d",&p0->book_kc)p0->book_xy=p0->book_kc/*开始时图书现有量和库存量相等*/p0->next=NULLprintf("\n图书信息初始化完毕!按任意键继续下一步 *** 作..\n")getch()system("cls")}voidInsert_New_Book()/*新书入库*/{BK*p,*p0,*p1p=p1=h_bookprintf("\n新书入库模块\n")printf("\n请输入新书信息..\n包括书号.书名.数量..\n")p0=(BK*)malloc(sizeof(BK))printf("图书编号:")scanf("%s",p0->book_num)while(strcmp(p0->book_num,p1->book_num)!=0&&p1->next!=NULL)p1=p1->nextif(strcmp(p0->book_num,p1->book_num)==0)/*此处分两种情况,若图书编号存在,则直接进库,只须输入书的数量*/{printf("\n此编号图书已存在!!直接入库!\n")printf("图书数量:")scanf("%d",&p0->book_kc)p1->book_kc+=p0->book_kcp1->book_xy+=p0->book_kc}else/*若不存在,则需要输入其他的信息,然后在进行插入 *** 作*/{printf("图书名称:")scanf("%s",p0->book_name)printf("图书作者:")scanf("%s",p0->book_writer)printf("图书数量:")scanf("%d",&p0->book_kc)while(p->next){p=p->next}if(h_book==NULL)h_book=p0/*此处分两种情况,链表中没有数据,head直接指向p0处*/elsep->next=p0/*此处分两种情况,链表中有数据,链表中最后元素的next指向p0处*/p0->next=NULLp0->book_xy=p0->book_kc}printf("\n新书入库完毕!按任意键继续下一步 *** 作..\n")getch()system("cls")}voidadd_reader()/*添加读者*/{RD*p0intiprintf("\n读者初始化开始,请输入读者信息..\n包括书证号.姓名..\n")p0=(RD*)malloc(sizeof(RD))/*申请新结点存储空间*/h_reader=p0printf("\n请输入读者的信息:\n")printf("读者书证号:")scanf("%s",p0->reader_num)printf("读者姓名:")scanf("%s",p0->reader_name)p0->right=0for(i=0iborrow[i].borrow_book_num,"0")/*所借图书直接置为0(即没有借书)*/strcpy(p0->borrow[i].limit_date,"0")}p0->next=NULLprintf("\n读者信息初始化完毕!按任意键继续下一步 *** 作..\n")getch()system("cls")}voidBorrow_Book()/*借书模块*/{BK*p0RD*p1charbo_num[10],rea_num[10],lim_date[8]intip0=h_bookp1=h_readerprintf("\n借书模块\n")printf("\n请输入借书的读者书证号:")scanf("%s",rea_num)while(p1->next!=NULL&&strcmp(rea_num,p1->reader_num)!=0)//查找读者号{p1=p1->next}if(p1->next==NULL&&strcmp(rea_num,p1->reader_num)!=0){printf("\n此读者编号不存在!按任意键返回..\n")gotoEND}printf("\n请输入你要借的书的编号:")scanf("%s",bo_num)while(strcmp(bo_num,p0->book_num)!=0&&p0->next!=NULL)//查找书号{p0=p0->next}if(p0->next==NULL&&strcmp(bo_num,p0->book_num)!=0){printf("\n此图书编号不存在!按任意键返回..\n")}elseif(p0->book_xyright>=Max){printf("\n不好意思,借书数目已满!不能借书!\n按任意键返回.")}elseif(strcmp(p1->borrow[0].borrow_book_num,"0")!=0){for(i=0iborrow[i].borrow_book_num,bo_num)==0){printf("\n抱歉!同一个读者不能同借两本相同的书!\n")}elseif(strcmp(p1->borrow[i].borrow_book_num,"0")==0){printf("\n请输入你要归还图书的日期:")scanf("%s",lim_date)strcpy(p1->borrow[p1->right++].borrow_book_num,bo_num)strcpy(p1->borrow[p1->right-1].limit_date,lim_date)p0->book_xy--printf("\n读者编号%s借书完毕!按任意键继续下步 *** 作..",p1->reader_num)}}}else{printf("\n请输入你要归还图书的日期:")scanf("%s",lim_date)strcpy(p1->borrow[p1->right++].borrow_book_num,bo_num)strcpy(p1->borrow[p1->right-1].limit_date,lim_date)p0->book_xy--printf("\n读者编号%s借书完毕!按任意键继续下步 *** 作..",p1->reader_num)}END:getch()system("cls")}voidReturn_Book()/*还书模块*/{BK*pRD*qinti,j,find=0charreturn_book_num[10],return_reader_num[10]p=h_bookq=h_readerprintf("\n还书模块\n")printf("\n请输入要还书的读者编号:")scanf("%s",return_reader_num)while(q->next!=NULL&&strcmp(return_reader_num,q->reader_num)!=0)q=q->nextif(q->next==NULL&&strcmp(return_reader_num,q->reader_num)!=0){find=2printf("\n此读者编号不存在!按任意键返回..\n")gotoend}printf("\n请输入读者还书的编号:")scanf("%s",return_book_num)while(p->next!=NULL&&strcmp(return_book_num,p->book_num)!=0)p=p->nextif(p->next==NULL&&strcmp(return_book_num,p->book_num)!=0){find=2printf("\n错误!此图书编号不存在!按任意键返回..\n")gotoend}for(i=0iborrow[i].borrow_book_num)==0)/*如果此读者借了此书*/{find=1for(j=ijborrow[j].borrow_book_num,q->borrow[j+1].borrow_book_num)strcpy(q->borrow[j].limit_date,q->borrow[j+1].limit_date)}strcpy(q->borrow[Max-1].borrow_book_num,"0")strcpy(q->borrow[Max-1].limit_date,"0")p->book_xy++q->right--printf("\n编号%s的读者还书完毕!按任意键继续下步 *** 作..",return_reader_num)}if(find==0)printf("\n错误!此读者未借此书!按任意键返回..\n")end:getch()system("cls")}voidPrint_book()/*查询图书信息*/{BK*pp=h_bookprintf("\n图书信息如下:\n\n")printf("图书编号\t图书名称\t图书作者\t现有\t\t库存\n")while(p!=NULL){printf("%s\t\t%s\t\t%s\t\t%d\t\t%d\n",p->book_num,p->book_name,p->book_writer,p->book_xy,p->book_kc)p=p->next}printf("\n图书信息打印完毕!按任意键继续下一步 *** 作..")getch()system("cls")}voidPrint_reader()/*查询读者信息*/{RD*pintip=h_readerprintf("\n读者信息如下:\n\n")printf("读者书证号\t\t读者姓名\n")printf("\n")while(p!=NULL){printf("\t%s\t\t%s",p->reader_num,p->reader_name)for(i=0iborrow[i].borrow_book_num)printf("\t\t%s",p->borrow[i].limit_date)}printf("\n")p=p->next}printf("\n读者信息打印完毕!按任意键继续下一步 *** 作..")getch()system("cls")}//文件模块voidSave()/*保存信息*/{Save_Reader()Save_Book()}voidSave_Reader()/*保存读者信息*/{FILE*fp_readerRD*p,*p0p=h_readerif((fp_reader=fopen("Reader.txt","wb"))==NULL)/*创建文件,进行保存*/{printf("\n文件保存失败!\n请重新启动本系统\n")exit(0)}while(p!=NULL){if(fwrite(p,sizeof(RD),1,fp_reader)!=1)/*将链表中的信息写入文件中*/printf("\n写入文件失败!\n请重新启动本系统!\n")p0=pp=p->nextfree(p0)/*释放所有结点*/}h_reader=NULLfclose(fp_reader)/*关闭文件*/}voidSave_Book()/*保存图书信息*/{FILE*fp_book/*创建文件型指针*/BK*p,*p0p=h_bookif((fp_book=fopen("Book.txt","wb"))==NULL)/*创建文件,进行保存*/{printf("\n文件保存失败!\n请重新启动本系统\n")exit(0)}while(p!=NULL){if(fwrite(p,sizeof(BK),1,fp_book)!=1)/*将链表中的信息写入文件中*/printf("\n写入文件失败!\n请重新启动本系统!\n")p0=pp=p->nextfree(p0)}h_book=NULLfclose(fp_book)/*关闭文件*/}voidLoad()/*加载信息*/{Load_Reader()Load_Book()}voidLoad_Reader()/*加载读者信息*/{RD*p1,*p2,*p3FILE*fp/*创建文件型指针*/fp=fopen("book.txt","rb")/*打开文件*/p1=(RD*)malloc(sizeof(RD))fread(p1,sizeof(RD),1,fp)h_reader=p3=p2=p1while(!feof(fp))/*读出信息,重新链入链表*/{p1=(RD*)malloc(sizeof(RD))fread(p1,sizeof(RD),1,fp)p2->next=p1p3=p2p2=p1}p3->next=NULLfree(p1)fclose(fp)/*关闭文件*/}voidLoad_Book()/*加载图书信息*/{BK*p1,*p2,*p3FILE*fp/*创建文件型指针*/fp=fopen("Book.txt","rb")/*打开文件*/p1=(BK*)malloc(sizeof(BK))fread(p1,sizeof(BK),1,fp)h_book=p3=p2=p1while(!feof(fp))/*读出信息,重新链入链表*/{p1=(BK*)malloc(sizeof(BK))fread(p1,sizeof(BK),1,fp)p2->next=p1p3=p2p2=p1}p3->next=NULLfree(p1)fclose(fp)/*关闭文件*/}voidmain(){FILE*fp_book,*fp_reader/*创建文件型指针*/Login()if((fp_book=fopen("Book.txt","rb"))==NULL||(fp_reader=fopen("Reader.txt","rb"))==NULL){Init()}elseLoad()Menu_select()/*调用主菜单*/}换了一个,你看看:#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>//输入/输出文件流类
using namespace std
const int Maxr=100//最多的读者
const int Maxb=100//最多的图书
const int Maxbor=5//每位读者最多借五本书
//读者类,实现对读者的信息的描述
class Reader
{
private:
int tag//删除标记 1:已删 0:未删
int no//读者编号
char name[10]//读者姓名
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}//设置删除标记 1:已删 0:未删
void addreader(int n,char *na)//增加读者
{
tag=0
no=n
strcpy(name,na)
for(int i=0i<Maxbori++)
borbook[i]=0
}
void borrowbook(int bookid)//借书 *** 作
{
for(int i=0i<Maxbori++)
{
if (borbook[i]==0)
{
borbook[i]=bookid
return
}
}
}
int retbook(int bookid)//还书 *** 作
{
for(int i=0i<Maxbori++)
{
if(borbook[i]==bookid)
{
borbook[i]=0
return 1
}
}
return 0
}
void disp()//读出读者信息
{
cout <<setw(5) <<no <<setw(10) <<name<<"借书编号:["
for(int i=0i<Maxbori++)
if(borbook[i]!=0)
cout <<borbook[i] <<"|"
cout <<"]"<<endl
}
}
//读者类库,实现建立读者的个人资料
class RDatabase
{
private:
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()//关闭 reader.txt
}
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
}
Reader *query(int readerid)//按编号查找
{
for (int i=0i<=topi++)
if (read[i].getno()==readerid &&
read[i].gettag()==0)
{
return &read[i]
}
return NULL
}
void disp() //输出所有读者信息
{
for (int i=0i<=topi++)
read[i].disp()
}
void readerdata()//读者库维护
~RDatabase() //析构函数,将read[]写到reader.txt文件中
{
fstream file("reader.txt",ios::out)
for (int i=0i<=topi++)
if (read[i].gettag()==0)
file.write((char *)&read[i],sizeof(read[i]))
file.close()
}
}
void RDatabase::readerdata()
{
char choice
char rname[20]
int readerid
Reader *r
while (choice!='0')
{
cout <<"\n\n\t\t\t读 者 维 护\n\n\n\t\t 1 新 增\n\n\t\t 2 更改\n\n\t\t 3 删 除\n\n\t\t 4 查 找\n\n\t\t 5 显 示\n\n\t\t 6 全 删\n\n\t\t 0 退出"<<endl
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 <<" 该读者不存在 "<<endl
break
}
cout <<"输入新的姓名:"
cin >>rname
r->setname(rname)
break
case '3':
cout <<" 输入读者编号:"
cin >>readerid
r=query(readerid)
if (r==NULL)
{
cout <<" 该读者不存在" <<endl
break
}
r->delbook()
break
case '4':
cout <<"读入读者编号:"
cin >>readerid
r=query(readerid)
if (r==NULL)
{
cout <<"该读者不存在"<<endl
break
}
r->disp()
break
case '5':
disp()
break
case '6':
clear()
break
default:cout<<"输入错误,请从新输入:"break
}
}
}
//图书类,实现对图书的描述,图书的编号,书名,借出,还入等
class Book
{
private:
int tag//删除标记 1:已删 0:未删
int no//图书编号
char name[20]//书名
int onshelf//是否再架 1:再架 2:已借
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 <<setw(6) <<no <<setw(18) <<name <<setw(10)
<<(onshelf==1? "在架":"已借") <<endl
}
}
//图书库类,实现对图书的维护,查找,删除等
class BDatabase
{
private:
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 (NULL==p)
{
top++
book[top].addbook(n,na)
return 1
}
return 0
}
Book *query(int bookid)//查找图书
{
for (int i=0i<=topi++)
if (book[i].getno()==bookid &&book[i].gettag()==0)
{
return &book[i]
}
return NULL
}
void bookdata()//图书库维护
void disp()
{
for (int i=0i<=topi++)
if (book[i].gettag()==0)
book[i].disp()
}
~BDatabase()//析构函数,将book[]写到book.txt文件中
{
fstream file("book.txt",ios::out)
for (int i=0i<=topi++)
if (book[i].gettag()==0)
file.write((char *)&book[i],sizeof(book[i]))
file.close()
}
}
void BDatabase::bookdata()
{
char choice
char bname[40]
int bookid
Book *b
while (choice!='0')
{
cout <<"\n\n\n\t\t\t图 书 维 护 "<<endl<<endl
cout<<"\t\t1 新 增\n \t\t2 更 改\n\t\t3 删 除\n\t\t4 查 找\n\t\t5 显示\n\t\t6 全 删\n\t\t0 退 出"<<endl
cin >>choice
switch (choice)
{
case '1':
cout <<"输入图书编号:"<<endl
cin >>bookid
cout <<"输入图书书名:"<<endl
cin >>bname
addbook(bookid,bname)
break
case '2':
cout <<"输入图书编号:"<<endl
cin >>bookid
b=query(bookid)
if (b==NULL)
{
cout <<" 该图书不存在 "<<endl
break
}
cout <<"输入新的书名:"<<endl
cin >>bname
b->setname(bname)
break
case '3':
cout <<" 读入图书编号:"<<endl
cin >>bookid
b=query(bookid)
if (b==NULL)
{
cout <<" 该图书不存在" <<endl
break
}
b->delbook()
break
case '4':
cout <<" 读入图书编号:"<<endl
cin >>bookid
b=query(bookid)
if (b==NULL)
{
cout <<" 该图书不存在"<<endl
break
}
b->disp()
break
case '5':
disp()
break
case '6':
clear()
break
default:cout<<"输入错误,请从新输入:"
}
}
}
//main() 函数的实现,程序的主界面的引导
void main()
{
char choice
int bookid,readerid
RDatabase ReaderDB
Reader *r
BDatabase BookDB
Book *b
while(choice!='0')
{
cout <<endl<<endl<<"\t\t\t 图 书 管 理 系 统\n\n\n"
cout <<"\t\t\t1 借 书\n\n\t\t\t2 还 书 \n\n\t\t\t3 图 书 维护\n\n\t\t\t4 读 者 维 护\n\n\t\t\t0 离 开"<<endl
cin >>choice
switch (choice)
{
case '1':
cout <<" 借书 读者编号:"
cin >>readerid
cout <<" 图书编号: "
cin >>bookid
r=ReaderDB.query(readerid)//按编号查找
if (NULL==r)
{
cout <<" 不存在该读者,不能借书"<<endl
break
}
b=BookDB.query(bookid)
if (b==NULL)
{
cout <<" 不存在该图书,不能借书"<<endl
break
}
if (b->borrowbook()==0)
{
cout <<" 该图书已借出,不能借书"<<endl
break
}
r->borrowbook(b->getno())
break
case '2':
cout<<"还书\n 读者编号:"
cin >>readerid
cout <<" 图书编号:"
cin >>bookid
r=ReaderDB.query(readerid)
if (r==NULL)
{
cout <<" 不存在该读者,不能还书" <<endl
break
}
b=BookDB.query(bookid)
if (b==NULL)
{
cout <<" 不存在该图书,不能还书" <<endl
break
}
b->retbook()
r->retbook(b->getno())
break
case '3':
BookDB.bookdata()
break
case '4':
ReaderDB.readerdata()
break
default:cout<<"输入错误,请从新输入:"
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)