C语言程序设计的图书管理系统

2024-11-22 17:23:11
推荐回答(4个)
回答1:

#include
#include
#include
#include //输入/输出文件流类
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() //获取姓名
int gettag() //获取删除标记
int getno() //获取读者编号
void setname(char na[]) //设置姓名
{
strcpy(name,na);

}
void delbook()//设置删除标记 1:已删 0:未删
void addreader(int n,char *na)//增加读者
{
tag=0;
no=n;
strcpy(name,na);
for(int i=0;iborbook[i]=0;
}
void borrowbook(int bookid)//借书操作
{
for(int i=0;i{
if (borbook[i]==0)
{
borbook[i]=bookid;
return;

}
}

}
int retbook(int bookid)//还书操作
{
for(int i=0;i{
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;

}
}
return 0;
}
void disp()//读出读者信息
{
cout << setw(5) << no <<腔禅setw(10) << name<<"借书编号:[";
for(int i=0;iif(borbook[i]!=0)
cout << borbook[i] << "|";
cout << "]"<
}
};

//读者类库,实现建立读者的个人资料
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=0;i<=top;i++)
if (read[i].getno()==readerid &&
read[i].gettag()==0)
{
return &read[i];
}
return NULL;
}
void disp() //输出所有读者信息
{
for (int i=0;i<=top;i++)
read[i].disp();
}
void readerdata();//读者库维护
~RDatabase() //析构函数,将read[]写到reader.txt文件中
{
fstream file("reader.txt",ios::out);
for (int i=0;i<=top;i++)
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 退 出"<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 << " 该读者不存在 "<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() //获取姓名
int getno()//获取图书编号
int gettag()//获取删除标记
void setname(char na[])//设置书名
{
strcpy(name,na);
}
void delbook()//删除图书
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? "在架":"已借") <}
};

//图书库类,实现对图书的维护,查找,删除等
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=0;i<=top;i++)
if (book[i].getno()==bookid &&book[i].gettag()==0)
{
return &book[i];
}
return NULL;
}
void bookdata();//图书库维护
void disp()
{
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
book[i].disp();
}
~BDatabase()//析构函数,将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()
{
char choice;
char bname[40];
int bookid;
Book *b;
while (choice!='0')
{
cout <<"\n\n\n\t\t\t图 书 维 护 "<cout<<"\t\t1 新 增\n \t\t2 更 改\n\t\t3 删 除\n\t\t4 查 找\n\t\t5 显 示\n\t\t6 全 删\n\t\t0 退 出"<cin >> choice;
switch (choice)
{
case '1':
cout << "输入图书编号:"<cin >> bookid;
cout << "输入图书书名:"<cin >> bname;
addbook(bookid,bname);
break;
case '2':
cout << "输入图书编号:"<cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout << " 该图书不存在 "<break;
}
cout << "输入新的书名:"<cin >> bname;
b->setname(bname);
break;
case '3':
cout <<" 读入图书编号:"<cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 该图书不存在" << endl;
break;
}
b->delbook();
break;
case '4':
cout << " 读入图书编号:"<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 <
cout <<"\t\t\t1 借 书\n\n\t\t\t2 还 书 \n\n\t\t\t3 图 书 维 护\n\n\t\t\t4 读 者 维 护\n\n\t\t\t0 离 开"<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 <<" 不存在该图书,不能还书" <break;
}
b->retbook();
r->retbook(b->getno());
break;
case '3':
BookDB.bookdata();
break;
case '4':
ReaderDB.readerdata();
break;
default:cout<<"输入错误,请从新输入:";

}
}
}

回答2:

#include "stdio.h"兄陪
#include
#include "conio.h"
#include
#include
#include "tp.h"
#include
#include
FILE *fp,*p ;
void *buf=NULL;

void main()
{ void adm1(),adm2(),build(),huan(),borrow(),dele(),kaiji();
void anniu(int x1,int y1,int x2,int y2,int color,char *f);
void no();
int d=VGA,m=VGAHI;
initgraph(&d,&m,"");
setmouse(0,629,0,459);
settextstyle(3,0,1);
kaiji();
buf=jiantou(buf);
adm1();
adm2();
}

void adm1()
{ char adp[20],password[20];
int i,n=0;
textbackground(2);
cleardevice();
fp=fopen("pass","r");
if(fp==NULL) { outtext("no exit ");getch();exit(0);}
for(;;)
{ setcolor(2);
anniu(150,1,300,20,0," ");
setcolor(15);
outtext("input password:"宏氏);
gotoxy(20,1);

gets(adp);
for(i=0;adp[i]!='\0';i++);
if(i==0) exit(0);
fgets(password,i+1,fp);
if(strcmp(password,adp)==0) { cleardevice();outtext("YES");getch();adm2();}
else{ n++;
cleardevice();outtext("error");
for(i=0;i<10;i++) delay(8000);
if(n==2) { cleardevice(); outtext("once again");for(i=0;i<=10;i++) delay(8000);}
if(n==3) { cleardevice(); outtext("3 second out");for(i=0;i<=10;i++) delay(8000);
exit(0);
}
}
fclose(fp);cleardevice();

}
}
void adm2()
{
char a;
int i,j,x,y;
clrscr();
cleardevice();
for(;;)
{ setbkcolor(6);
setcolor(2);
cleardevice();
anniu(12,40,190,70,15,"built a user (B)");
setcolor(2);
anniu(12,90,190,120,15,"return book (R)");
setcolor(2);
anniu(12,140,190,170,15,"borrow books (J)");
setcolor(2);
anniu(12,190,190,220,15,"蔽尘散delete user (D)");
setcolor(2);
anniu(12,240,190,270,15,"exit (E)");
do{

do{
i=readmouse(&x,&y,buf,1);
r.h.ah=1;
int86(0x16,&r,&r);
j=r.h.ah;
if(j!=0) switch(j){
case 0x30:a='b';break;
case 0x13:a='r';break;
case 0x24:a='j';break;
case 0x20:a='d';break;
case 0x12:a='e';break;
}
} while(i!=1&&j==0);
if((x>12&&x<190&&y>40&y<70&&i==1)||a=='b') { setcolor(2);
anniu(12,40,190,70,2,"built a user (B)");
delay(80000);
build();
}
if((x>12&&x<190&&y>90&y<120&&i==1)||a=='r') { setcolor(2);
anniu(12,90,190,120,2,"return book (R)");
delay(80000);
huan();
}
if((x>12&&x<190&&y>140&y<170&&i==1)||a=='j') {setcolor(2);
anniu(12,140,190,170,2,"borrow books (J)");
delay(80000);
borrow();
}
if((x>12&&x<190&&y>190&y<220&&i==1)||a=='d') { setcolor(2);
anniu(12,190,190,220,2,"delete user (D)");
delay(80000);
dele();
}
if((x>12&&x<190&&y>240&y<270&&i==1)||a=='e') {setcolor(2); anniu(12,240,190,270,2,"exit (E)");
closegraph();
showBMP("zz.bmp");
for(i=0;i<100;i++)delay(10000);
exit(0);
}
i=0;j=0;
}while(i==0&&j==0);

}

}
void anniu(int x1,int y1,int x2,int y2,int color,char *f)
{ setfillstyle(1,color);
rectangle(x1,y1,x2,y2);
floodfill(x1+1,y1+1,2);
setcolor(1);
outtextxy(20,y1+10,f);
}

void build()
{ FILE *f;
char id[20],name[20],sex[2],age[3],grad[50];
cleardevice();
printf("ID number:");
scanf("%s",id);
printf("name:");
scanf("%s",name);
printf("sex:");
scanf("%s",sex);
printf("grad:");
scanf("%s",grad);
p=fopen(id,"w+");
if(p==NULL) { no("can't build it");getch();adm2();}

fprintf(p,"%s ",name);
fprintf(p,"%s ",id);
fprintf(p,"%s ",sex);
fprintf(p,"%s ",grad);
no("bulid success");
getch();
fclose(p);
adm2();
}

void huan()
{
FILE *fp,*p;
char id[20],a[5][50],name[20],c;
int i=0,m,k;
cleardevice();
printf("input ID number:");
scanf("%s",id);
p=fopen(id,"r");
if(p==NULL) { no("user no exit");getch();fclose(p);adm2();}
fscanf(p,"%s",name);
fclose(p);
fp=fopen(name,"r");
while(!feof(fp)){ fscanf(fp,"%s",a[i++]); }

for(k=0;k fclose(fp);
p=fopen(name,"w+");
for(;;)
{
printf("\n\npress the book number that you want to return. \n\nPress 6 if you want to return all. \n\npress 7 to cancle.\n");
scanf("%d",&m);
if(m==7)
{ for(k=0;k {
fprintf(p,"%s ",a[k]);
}
fclose(p);
adm2();
}
if(m==6) remove(name);
if(m<6)
{
for(k=0;k { if(k!=m-1)
fprintf(p,"%s ",a[k]);
}
printf("book %d has returned.",m);
}
printf("\ncontinue? (Y/N)");
c=getch();
if(c=='y') continue;
if(c=='n') { fclose(p);adm2(); }
}
}

void borrow()
{ FILE *f,*m,*p;
char id[20],book[5][50],name[20],c;
int i=0,n=0,k;
window(1,1,80,25);cleardevice();
printf("please input the user's ID:");
scanf("%s",id);
m=fopen(id,"r");
if(m==NULL) {no("user no exit"); getch();fclose(m);adm2();}
fscanf(m,"%s",name);
fclose(m);
p=fopen(name,"r");
if(p==NULL);
else
{ while(!feof(p))
{
fscanf(p,"%s",book[i]);
i++;
}
i--;
for(k=0;k }
fclose(p);
f=fopen(name,"w");
printf("\nhow many book do you want to borrow? <=4:");
do{printf("\ninput:");scanf("%d",&n); }while(i+n>=5);
for(k=i;k { printf("\nbook %d :",k+1);
gets(book[k]);
}
for(k=0;k fputc('\0',f);
printf("success");
fclose(f);adm2();

}
void kaiji()
{ int i,j;
char a[20]="BOOK software";
setbkcolor(0);
for(i=0;i<=50;i++)
{
cleardevice();
setcolor(i/2);
line(50,100,50,350);
line(200,100,200,350);
line(350,100,350,350);
/*arc(125, 100, 0, 180,75);画圆弧*/
ellipse(125, 100, 0, 180,75,40);/*椭圆*/
ellipse(275, 100, 0, 180,75,40);
ellipse(275, 350, 0, 180,75,40);
ellipse(125, 350, 0, 180,75,40);
settextstyle(3,0,4);
outtextxy(380,145+i,a);
settextstyle(1,0,1);
outtextxy(450,450,"producer: fuquan");
for(j=0;j<5;j++) delay(5000);
}

}

void dele()
{ FILE *fp,*f;
char id[20],a[4][20],c,b[5][50];
int i=0,j=0,k;
window(1,1,80,25);cleardevice();
printf("Input ID number:");
scanf("%s",id);
fp=fopen(id,"r");
if(fp==NULL) { no("user no exit");getch(); fclose(fp);adm2();}
else while(!feof(fp))
{ fscanf(fp,"%s",a[i++]);}
printf("\nname: %s",a[0]);
printf("\nID: %s",a[1]);
printf("\nsex: %s",a[2]);
printf("\ngrad: %s",a[3]);
f=fopen(a[0],"r");
if(f==NULL);
else {
while(!feof(f))
{ fscanf(f,"%s",b[j++]);}
for(k=0;k }
printf("\nDo you want to delete? (Y/N): ");
c=getch();
fclose(fp);
fclose(f);
if(c=='y') { remove(id);remove(a[0]);no("success");getch();adm2();}
if(c=='n') adm2();
}

void no(char *f)
{ setfillstyle(1,3);
rectangle(260,200,390,220);
floodfill(301,201,1);
setcolor(1);
outtextxy(270,200,f);
}

回答3:

我们也开始做课程设计了呢~~~ 这是我同学做的:

#include
#include
#include

struct BOOK
{
int id,usr[10],total,store,days[10];
char name[31],author[21];
}books[100];
/*上面是结构体的定义,用于存放书籍及借书的信息。*/

void page_title(char *menu_item)
{
clrscr();
printf(">>> 图 书 管 理 系 统 <<<\n\n- %s -\n\n",menu_item);
}
/*上面是打印页眉的函数,同时通过参数menu_item,可以显示当前的状态。*/

void return_confirm(void)
{
printf("\n按任意键返回……\n");
getch();
}
/*上面是返回前请求确认的函数,以便在返回前观察结果*/

int search_book(void)
{
int n,i;
printf("请输入图书序号:");
scanf("%d",&i);
for(n=0;n<100;n++)
{
if(books[n].id==i)
{
printf("书名:%s\n",books[n].name);
printf("作者:%s\n",books[n].author);
printf("存数:%d of ",books[n].store);
printf("%d\n",books[n].total);
return n;
}
}
printf("\n输入错误或无效图书序号.\n");
return -1;
}
/*上面的函数是在数组中找丛指到图书号匹配的记录,显示其信息并返
回数组下标,如果找不到相应记录则提示错误并返回-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("请输入可借天数:");
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("脊亮此书已经全部借出.\n");
return_confirm();
}
/*上面是借书的函数,首先调用找书函数*/

void book_in(void)
{
int n,s,l;
page_title("归还图书");
if((n=search_book())!=-1&&books[n].store{
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("数量:");
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("请用数字键选择操作\n\n");
printf("1 借阅图书\n2 归还图书\n\n");
printf("3 注册新书\n4 注销旧书\n\n");
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;
}
{
int n;
page_title("广?症慕");
if((n=search_book())!=-1) books[n].id=0;
printf("乎慕厮广?.\n");
return_confirm();
}

void main(void)
{
menu: page_title("荷恬佥汽");
printf("萩喘方忖囚佥夲荷恬\n\n");
printf("1 処堋夕慕\n2 拷珊夕慕\n\n");
printf("3 广过仟慕\n4 广?症慕\n\n");
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;
}

回答4:

上次百度了一个,我看看啊