返回列表 发帖

[VC++] [转帖]图书馆系统(C++)

大型作业---图书系统的

/*** bookfunction.c***/
/*添加图书*/
#include "图书管理.c"
void bookadd()
{
file *fp;
book consultbook;
fp=fopen("book.txt","rb");/*读方式打开文件*/
if(fp==null)
fp=fopen("book.txt","wb"); /*写方式打开文件*/
else/*如果有书就添加*/
{
fclose(fp);
fp=fopen("book.txt","ab");/*追加方式打开文件*/
}
clrscr();/*清屏*/
printf("intput new book num: ");/*逐个输入新图书的资料*/
gets(consultbook.num);
gotoxy(2,4);
printf("input new book name: ");
gets(consultbook.name);
gotoxy(2,5);
printf("please input new book's price: ");
scanf("%d%*c",&consultbook.price);
strcpy(consultbook.person,"");
consultbook.yes=1;/*表示书存在未借出*/
consultbook.next=null;
fwrite(&consultbook,sizeof(book),1,fp);/*块写*/
fclose(fp); /*关闭文件*/
clrscr();
}
/*图书查询*/
void bookconsult()
{
file *fp;
book consultbook;
char bookname[20]; /*书名*/
int flag=0,i=0;
clrscr();
gotoxy(2,3);
printf("input the book name: ");
gets(bookname); /*输入要查询的图书名*/
fp=fopen("book.txt","rb");
if(fp==null)/*没找到文件*/
{
clrscr();
gotoxy(2,3);
printf("error!");
fclose(fp);
return;
}
else
while(!feof(fp))/*查询图书资料*/
{
fread(&consultbook,sizeof(book),1,fp);
if(strcmp(consultbook.name,bookname)==0)/*如果查找到*/
{
clrscr();
gotoxy(2,3+i*5);
printf("this book %s information as follow:",consultbook.name);
gotoxy(2,4+i*5);
printf("this book num is %s",consultbook.num);
gotoxy(2,5+i*5);
printf("this book's price is %d",consultbook.price);
gotoxy(2,6+i*5);
printf("this book's is browwed by %s",consultbook.person);
gotoxy(2,7+i*5);
printf("this book's state is %d",consultbook.yes);
i++;
flag=1; /*查找数据标志*/
}
}
if(flag==0)
{
clrscr();
gotoxy(2,3);
printf("no found this book");
}
fclose(fp);
}
/*删除图书资料*/
void bookdel()
{
file *fp;
book *head,*p,*q;
char bookname[20];
fp=fopen("book.txt","rb");/*读方式打开文件*/
if(fp==null)/*没找到文件*/
{
gotoxy(2,3);
printf("error!");
fclose(fp);
return;
}
head=p=q=(book*)malloc(sizeof(book));/*将文件内容放入链表*/
fread(p,sizeof(book),1,fp); /*读第一条记录*/
while(!feof(fp))
{
q=p;
p=(book*)malloc(sizeof(book));
fread(p,sizeof(book),1,fp); /*读记录*/
q->next=p;/*新记录插入到链表的尾*/
}
p->next=null;/*最后一个结点的后继为空*/
fclose(fp);
clrscr();
printf("input the book name: ");
gets(bookname); /*输入图书名*/
p=head;
while(p!=null)/*按名字查找要删除的图书*/
{
if(strcmp(p->name,bookname)==0)/*找到要删除的图书*/
{
if(p==head) /*如果是第一个结点*/
head=head->next;
else
q->next=p->next;/*不是第一个结点*/
break;
}
q=p; /*指针后移*/
p=p->next;
}
fp=fopen("book.txt","wb");/*删除后从链表的头开始重新保存*/
while(head!=null)
{
fwrite(head,sizeof(book),1,fp);/*块写*/
head=head->next;
}
fclose(fp);
clrscr();
}
/*借书*/
void bookborrow()
{
file *fp;
book consultbook;
member consultmember;
char bookname[20],membername[20];
int flag=0,mflag=0;
clrscr();
gotoxy(2,3);
printf("input the mermber name: ");
gets(membername); /*输入会员名*/
if((fp=fopen("member.txt","rb"))==null)/*没找到文件*/
{
gotoxy(3,3);
printf("cannot open file\n!");
return;
}
else
while(!feof(fp))/*查询会员资料*/
{
fread(&consultmember,sizeof(member),1,fp);
if(strcmp(consultmember.name,membername)==0)
{
mflag=1; /*是会员标志*/
break;
}
}
fclose(fp);
if(mflag==0)
{
gotoxy(2,4);
printf("you not is a member,pleas register!");/*不是会员不能借书*/
return;
}
gotoxy(3,5);
printf("input the book name: ");
gets(bookname); /*输入图书名*/
clrscr();
if((fp=fopen("book.txt","rb+"))==null)/*没找到文件*/
{
gotoxy(2,3);
printf("cannot open file\n!");
return;
}
while(!feof(fp))/*查询图书资料*/
{
fread(&consultbook,sizeof(book),1,fp);
if(strcmp(consultbook.name,bookname)==0)
{
if(consultbook.yes==0)/*图书已经借出的判断*/
{
gotoxy(2,3);
printf("this book has borrowed\n!");
break;
}
else
{
consultbook.yes=0; /*作借出标志*/
strcpy(consultbook.person,membername);/*登记借书会员*/
fseek(fp,-1l*sizeof(book),1);/*从当前位置前移一条记录指针*/
fwrite(&consultbook,sizeof(book),1,fp);/*写记录*/
gotoxy(2,3);
printf("borrowed succ!");
flag=1;/*借出书标志*/
break;
}
}
}
if(flag!=1)
{
clrscr();
gotoxy(2,3);
printf("borrowed fail!\n");/*借书失败*/
}
fclose(fp);
}
/*还书*/
void bookreturn()
{
file *fp;
book consultbook;
char bookname[20];
int flag=0;
clrscr();
if((fp=fopen("book.txt","rb+"))==null)/*没找到文件*/
{
gotoxy(2,3);
printf("cannot open file\n!");
return;
}
printf("input the book name: ");
gets(bookname); /*输入图书名*/
clrscr();
while(!feof(fp))/*查询图书资料*/
{
fread(&consultbook,sizeof(book),1,fp);
if(strcmp(consultbook.name,bookname)==0)
{
if(consultbook.yes==0)/*图书已经借出的判断*/
{
consultbook.yes=1;/*作图书未借书标志,表示已还书*/
strcpy(consultbook.person,"");
fseek(fp,-1l*sizeof(book),1);
fwrite(&consultbook,sizeof(book),1,fp);
gotoxy(2,3);
printf("return book succ!");
flag=1;
break;
}
}
}
if(flag!=1)
printf("return fail!\n");
fclose(fp);
}
/*清除屏幕*/
void clrscr()
{
int i,j;
puttext(2,3,78,23,c);/*刚开始已经用gettext把蓝色的一块地方保存下来,现在再还原*/
gotoxy(2,3);
}
/*会员注册*/
void memberadd()
{
file *fp;
member consultmember;
fp=fopen("member.txt","rb");
if(fp==null)/*如果没会员就新建*/
fp=fopen("member.txt","wb");
else/*如果有会员就添加*/
{
fclose(fp);
fp=fopen("member.txt","ab");/*追加方式打开*/
}
clrscr();
gotoxy(2,3);
printf("intput new member name: ");/*逐个输入会员的资料*/
gets(consultmember.name);
gotoxy(2,4);
printf("input member ***: ");
gets(consultmember.***);
gotoxy(2,5);
printf("please input member age: ");
scanf("%d%*c",&consultmember.age);
fwrite(&consultmember,sizeof(member),1,fp);/*写入记录*/
fclose(fp);
clrscr();
}
/*查询会员*/
void memberconsult()
{
file *fp;
member consultmember;
char membername[20];
int flag=0;
clrscr();
gotoxy(2,3);
printf("input the member name: ");/*输入要查询的会员名*/
gets(membername);
fp=fopen("member.txt","rb");
if(fp==null)/*没找到文件*/
{
clrscr();
gotoxy(2,3);
printf("error!");
fclose(fp);
return;
}
while(!feof(fp))/*查询会员资料*/
{
fread(&consultmember,sizeof(member),1,fp);
if(strcmp(consultmember.name,membername)==0)/*比较*/
{
clrscr();
gotoxy(2,3);
printf("this member %s information as follow:",consultmember.name);
gotoxy(2,4);
printf("this member'*** is %s",consultmember.***);
gotoxy(2,5);
printf("this member 's age is %d",consultmember.age);
flag=1;
break;
}
}
if(flag==0)
{
clrscr();
gotoxy(2,3);
printf("no found this member!");
}
fclose(fp);
}
/*删除会员资料*/
void memberdel()
{
file *fp;
member *head,*p,*q;
char membername[20];
fp=fopen("member.txt","rb");
if(fp==null)/*没找到文件*/
{
gotoxy(2,3);
printf("error!");
fclose(fp);
return;
}
head=p=q=(member*)malloc(sizeof(member));/*将文件内容放入链表*/
fread(p,sizeof(member),1,fp);
while(!feof(fp))
{
q=p;
p=(member*)malloc(sizeof(member));
fread(p,sizeof(member),1,fp);/*读记录*/
q->next=p;/*链入到链表的尾部*/
}
p->next=null;
fclose(fp);
clrscr();
printf("input the member name: ");/*输入会员名*/
gets(membername);
p=head;
while(p!=null)/*按名字查找要删除的书*/
{
if(strcmp(p->name,membername)==0)/*查找会员*/
{
if(p==head)
head=head->next;/*如果是第一条记录*/
else
q->next=p->next;/*不是第一条记录*/
break;
}
q=p; /*没找到,则指针后移继续找*/
p=p->next;
}
fp=fopen("member.txt","wb");/*删除后重新保存*/
while(head!=null)
{
fwrite(head,sizeof(member),1,fp);/*块写*/
head=head->next;
}
fclose(fp);
clrscr();
}
/*查询会员所借的图书*/
void memberbook()
{
file *fp;
book consultbook;
member consultmember;
char bookname[20],membername[20];
int flag=0,mflag=0,i=0;
clrscr();
gotoxy(2,3);
printf("input the mermber name: ");/*输入会员名*/
gets(membername);
if((fp=fopen("member.txt","rb"))==null)/*没找到文件*/
{
gotoxy(3,3);
printf("cannot open file\n!");
return;
}
else
while(!feof(fp))/*查询会员资料,确认会员*/
{
fread(&consultmember,sizeof(member),1,fp);
if(strcmp(consultmember.name,membername)==0)
{
mflag=1;
break;
}
}
fclose(fp);
if(mflag==0)
{
gotoxy(2,4);
printf("not the member!");
return;
}
clrscr();
if((fp=fopen("book.txt","rb+"))==null)/*没找到文件*/
{
gotoxy(2,3);
printf("cannot open file\n!");
return;
}
while(!feof(fp))/*查询图书资料*/
{
fread(&consultbook,sizeof(book),1,fp);
gotoxy(2,3);
printf("this member %s has borrowed book as follow:",membername);
gotoxy(2,4);
printf("********************************************");
if(strcmp(consultbook.person,membername)==0)/*借阅人比较*/
{
gotoxy(2,5+i*4);
printf("book name is %s",consultbook.name);
gotoxy(2,6+i*4);
printf("this book num is %s",consultbook.num);
gotoxy(2,7+i*4);
printf("this book's price is %d",consultbook.price);
printf("\n");
i++;
flag=1;
}
}
if(flag!=1)
{
clrscr();
gotoxy(2,3);
printf("no borrow book!\n");
}
else
{
gotoxy(2,i*4+6);
printf("******total is %d*********",i);/*显示总数*/
}
fclose(fp);
}
/*帮助文件*/
void help()
{
clrscr();
drawframe(10,5,50,8,0,7); /*画边框*/
gotoxy(15,6);
cprintf("click menu to run each funtion");/*输出字符串*/
getch();
clrscr();
}
/***版本信息***/
void ver()
{
clrscr();
drawframe(10,5,50,8,0,7); /*画边框*/
gotoxy(15,6);
cprintf("ver 1.0 finished by zhuyun");/*输出字符串*/
getch();
clrscr();
}

还没我的好

TOP

我编译时说   第三行有问题 
什么问题啊?看不出

TOP

返回列表

Powered by Discuz! 7.2   论坛QQ群:逐梦论坛群

© 2001-2021 Comsenz Inc. 鲁公网安备 37120302000001号