求一个C语言编程 图书管理系统
发布网友
发布时间:2022-06-01 02:02
我来回答
共2个回答
热心网友
时间:2023-11-10 16:52
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_INIT_SIZE 10
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
struct student /*定义学生类型,用于存放借出的书籍*/
{
int carnum;
char lendbook[10];
}student[1000];
struct employ /*定义职工类型*/
{
int employnum;
char employname[15];
int employage;
char employsex[2];
char employleve[10];
long int employtage;
}employ[50];
struct book /*定义书的类型*/
{
int booknum;
char bookname[10];
char bookcreat[10];
int turefalse; /*用于借书和还书模块判断一本书是否借出的条件*/
}book[1000];
struct car /*借书卡的数据类型*/
{
int carnum;
char studentname[10];
int studentclass;
}car[100];
huanbook() /*还书函数*/
{
FILE *fp,*fp2; /*定义两个文件指针,fp2用于修改数据时设立临时文件用,防止数据遭破坏*/
int i,n;
int carnum;
char lendbook[10];
printf("请你输入你的卡号\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r"); /*读取卡号记录*/
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++) /*for循环判断卡号是否存在*/
{
if(car[i].carnum==carnum) /*卡号存在,进入下一循环*/
{
n=i;
fclose(fp);
printf("请输入你要还的书的名字\n");
scanf("%s",lendbook);
fp=fopen("record.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++) /*判断是否借阅了输入的书*/
{
if(strcmp(student[i].lendbook,lendbook)==0) /*借阅了该书,进入下一循环,否则出错显示*/
{
fclose(fp);
fp=fopen("record.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
if(strcmp(student[i].lendbook,lendbook)==0)
{
continue; /*删除还掉的书的借*录*/
}
fwrite(&student[i],sizeof(struct student),1,fp2); /*写入原来没还的书的记录*/
}
fclose(fp);
fclose(fp2);
fp=fopen("record.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
fwrite(&student[i],sizeof(struct student),1,fp); /*将借*录信息写回*/
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w"); /*清临时文件的记录*/
fclose(fp2);
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++) /*将书的记录写入临时文件,防止因为修改信息破坏以前的记录*/
{
if(i==n)
{
book[i].turefalse=1;
fwrite(&book[i],sizeof(struct book),1,fp2); /*将还的书的原来状态设为无人借阅的*/
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp); /*将临时文件写回*/
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w"); /*清临时文件*/
fclose(fp2);
printf("还书完毕,按任意键返回\n");
getch();
return 1;
}
}
printf("你没有借这样的书,任意键返回\n"); /*出错提示*/
fclose(fp);
getch();
return 0;
}
}
printf("系统没这样的卡,和管理员联系,按任意键返回\n"); /*出错提示*/
fclose(fp);
getch();
}
findbook()
{
FILE *fp;
char bookname[10];
int ture,i;
fp=fopen("book.txt","r");
printf("请输入你要查找的书名\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)==0)
{
if(book[i].turefalse==1)
{
printf("这本书的详细资料是:%d %s %s 此书现在无人借阅\n按任意键返回\n",book[i].booknum,book[i].bookname,book[i].bookcreat);
}
else {printf("这本书已经有人借出\n");fclose(fp);return 0;}
fclose(fp);
return FALSE;
}
}
printf("没有你要查询的书籍\n");
fclose(fp);
return FALSE;
}
findbook1()
{
FILE *fp;
char bookcreat[10];
int ture,i;
fp=fopen("book.txt","r");
printf("请输入你要查找的作者名\n");
scanf("%s",bookcreat);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookcreat,book[i].bookcreat)==0)
{
if(book[i].turefalse==1)
{
printf("这本书的详细资料是:%d %s %s 此书现在无人借阅\n按任意键返回\n",book[i].booknum,book[i].bookname,book[i].bookcreat);
}
else {printf("这本书已经有人借出\n");fclose(fp);return 0;}
fclose(fp);
return FALSE;
}
}
printf("没有你要查询的书籍\n");
fclose(fp);
return FALSE;
}
lendcount()
{
FILE *fp;
int i,n=0;
fp=fopen("record.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
printf("卡号:%d 借出的书籍:%s \n",student[i].carnum,student[i].lendbook);
n=n+1;
}
fclose(fp);
printf("目前共有%d本书借出\n",n);
printf("按任意键\n");
getch();
return n;
}
chabook()
{
char ch5;
do
{
printf("---------------欢迎进入图书查询系统!--------------\n");
printf(" 1:<按书名查找>\n");
printf(" 2:<按作者查找>\n");
printf(" 0:<返回>\n");
printf("请输入0--2,其他输入非法!\n");
scanf("%s",&ch5);
switch(ch5)
{
case '1':findbook();getch();break;
case '2':findbook1();getch();break;
case '0':break;
default:printf("无此操作\n");getch();break;
}
}while(ch5!='0');
return FALSE;
}
lendbook()
{
FILE *fp,*fp2;
int i,n;
int carnum;
printf("请你输入你的卡号\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
if(car[i].carnum==carnum)
{
n=i;
fclose(fp);
printf("请输入你要借阅的书的名字\n");
scanf("%s",student[n].lendbook);
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,student[n].lendbook)==0)
{
if(book[i].turefalse==0) {printf("对不起,此书有人借出,请借其他书\n");fclose(fp);getch();return;}
else
fclose(fp);
fp=fopen("record.txt","a+");
student[n].carnum=carnum;
fwrite(&student[n],sizeof(struct student),1,fp);
fclose(fp);
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,student[n].lendbook)==0)
{
book[i].turefalse=0;
fwrite(&book[i],sizeof(struct book),1,fp2);
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w");
fclose(fp2);
printf("借书完毕,按任意键返回\n");
getch();
return;
}
}
printf("不存在这样的书,任意键返回\n");
fclose(fp);
getch();
return;
}
}
printf("你的卡号不存在,请申请新卡,按任意键返回\n");
fclose(fp);
getch();
}
carcount()
{
FILE *fp;
int i,n=0;
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
printf("第%d张卡<卡号:%d 姓名:%s 班级:%d>\n",i+1,car[i].carnum,car[i].studentname,car[i].studentclass);
n=n+1;
}
fclose(fp);
printf("目前共有%d本书\n",n);
printf("按任意键\n");
getch();
}
delcar()
{
FILE *fp,*fp2;
int i;
int carnum;
char choice;
fp=fopen("car.txt","r");
fp2=fopen("bookl.txt","w");
printf("请输入你要删除的卡号\n");
printf("如果你输入的卡号存在,系统自动删除该信息!如果不存在,系统不做任何改动\n");
scanf("%d",&carnum);
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
if(car[i].carnum!=carnum)
{
fwrite(&car[i],sizeof(struct car),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("是否真的要删除该卡?删除后该书籍的所有信息将无法恢复《Y/N》\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("car.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp2)!=0;i++)
{
fwrite(&car[i],sizeof(struct car),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getch();
return;
}
else
{
printf("按任意键返回\n");
getch();
return;
}
}
addcar()
{
FILE *fp;
int i=0;
fp=fopen("car.txt","a+");
printf("请你输入卡号\n");
scanf("%d",&car[i].carnum);
printf("请你输入学生姓名\n");
scanf("%s",car[i].studentname);
printf("请你输入班级\n");
scanf("%d",&car[i].studentclass);
fwrite(&car[i],sizeof(struct car),1,fp);
fclose(fp);
printf("输入完毕,任意键返回\n");
getch();
}
热心网友
时间:2023-11-10 16:52
#include "stdio.h"
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct book{
char number[20];
char bookname[20];
int count;
int price;
char zhuanye[20];
char author[20];
char chubanshe[20];
char beizhu[100];
struct book *next;
};
struct book *creat() //创建链表
{
struct book *p,*head,*end;
head=end=p=(struct book *)malloc(sizeof(struct book));
printf("输入编号为0结束输入\n");
do
{
printf("\n教材编号:");
gets(p->number);
if(strcmp(p->number,"0")==0){ end->next=NULL; return head;}
end=p;
printf("\n书名:");
gets(p->bookname);
printf("\n数量:");
scanf("%d",&p->count);
printf("\n价格:");
scanf("%d",&p->price);
getchar();
printf("\n专业:");
gets(p->zhuanye);
printf("\n作者:");
gets(p->author);
printf("\n出版社:");
gets(p->chubanshe);
printf("\n备注:");
gets(p->beizhu);
p=(struct book *)malloc(sizeof(struct book));
end->next=p;
}while(1);
}
void print(struct book *head) /*输出链表*/
{
struct book *p;
p=head;
if(p==NULL) { printf("您未打开任何文件,无法显示"); return;}
do{
printf("\n\n教材编号:%s",p->number);
printf("\n书名:%s",p->bookname);;
printf("\n数量:%d",p->count);
printf("\n价格:%d",p->price);
printf("\n专业:%s",p->zhuanye);
printf("\n作者:%s",p->author);
printf("\n出版社:%s",p->chubanshe);
printf("\n备注:%s",p->beizhu);
p=p->next;
}while(p!=NULL);
}
void save(struct book *head) //保存链表
{FILE *fp;
struct book *p;
char filename[20];
int ch;
printf("请输入文件名(包括后缀):");
scanf("%s",filename);
if((fp=fopen(filename,"r"))!=NULL)
{
printf("\n该文件已存在,是否覆盖?1--是 2---否");
scanf("%d",&ch);
if(ch!=1)
return;
}
if((fp=fopen(filename,"w"))==NULL) { printf("\n保存文件失败");getchar();return;}
p=head;
do{
fprintf(fp,"\n%s",p->number);
fprintf(fp,"\n%s",p->bookname);;
fprintf(fp,"\n%d",p->count);
fprintf(fp,"\n%d",p->price);
fprintf(fp,"\n%s",p->zhuanye);
fprintf(fp,"\n%s",p->author);
fprintf(fp,"\n%s",p->chubanshe);
fprintf(fp,"\n%s",p->beizhu);
p=p->next;
}while(p!=NULL);
fprintf(fp," over");
printf("\n保存成功");
getchar();
getchar();
fclose(fp);
}
struct book *openfile() //打开文件
{struct book *p,*f,*head;
FILE *fp;
char filename[20];
printf("\n请输入文件名(包括路径和后缀):");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL){ printf("\n文件找不到,请检查是否有该文件和路径是否正确");getchar();return NULL;}
head=f=p=(struct book *)malloc(sizeof(struct book));
fscanf(fp,"%s%s%d%d%s%s%s%s",p->number,p->bookname,&p->count,&p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
while(!feof(fp))
{ p=(struct book *)malloc(sizeof(struct book));
f->next=p;
fscanf(fp,"%s%s%d%d%s%s%s%s",p->number,p->bookname,&p->count,&p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
if(strcmp(p->number,"over")==0){ f->next=NULL; printf("\n文件打开成功,你可以显示此信息");getchar(); return head; }
f=p;
}
return head;
}
void namesort(struct book *head) //按书名排序
{ struct book *p,*t,*f,*h;
char ch[20];
int i;
h=t=f=p=head;
if(head==NULL) {printf("您为打开任何文件");getchar();return; };
f=p->next;
for(p=head;p->next!=NULL;p=p->next)
{
for(t=head,f=t->next;t->next!=NULL;f=f->next,t=t->next)
{
if(strcmp(t->bookname,f->bookname)>0)
{
strcpy(ch,t->number );
strcpy(t->number,f->number);
strcpy(f->number,ch);
strcpy(ch,t->bookname );
strcpy(t->bookname,f->bookname);
strcpy(f->bookname,ch);
i=t->count ;
t->count=f->count;
f->count=i;
i=t->price ;
t->price=f->price;
f->price=i;
strcpy(ch,t->zhuanye);
strcpy(t->zhuanye,f->zhuanye);
strcpy(f->zhuanye,ch);
strcpy(ch,t->author);
strcpy(t->author,f->author);
strcpy(f->author,ch);
strcpy(ch,t->chubanshe);
strcpy(t->chubanshe,f->chubanshe);
strcpy(f->chubanshe,ch);
strcpy(ch,t->beizhu);
strcpy(t->beizhu,f->beizhu);
strcpy(f->beizhu,ch);
}
}
}
print(h);
}
void chubanshesort(struct book *head) //按出版社排序
{ struct book *p,*t,*f,*h;
char ch[20];
int i;
h=t=f=p=head;
if(head==NULL) {printf("您为打开任何文件");getchar();return; }
f=p->next;
for(p=head;p->next!=NULL;p=p->next)
{
for(t=head,f=t->next;t->next!=NULL;f=f->next,t=t->next)
{
if(strcmp(t->chubanshe,f->chubanshe)>0)
{
strcpy(ch,t->number );
strcpy(t->number,f->number);
strcpy(f->number,ch);
strcpy(ch,t->bookname );
strcpy(t->bookname,f->bookname);
strcpy(f->bookname,ch);
i=t->count ;
t->count=f->count;
f->count=i;
i=t->price ;
t->price=f->price;
f->price=i;
strcpy(ch,t->zhuanye);
strcpy(t->zhuanye,f->zhuanye);
strcpy(f->zhuanye,ch);
strcpy(ch,t->author);
strcpy(t->author,f->author);
strcpy(f->author,ch);
strcpy(ch,t->chubanshe);
strcpy(t->chubanshe,f->chubanshe);
strcpy(f->chubanshe,ch);
strcpy(ch,t->beizhu);
strcpy(t->beizhu,f->beizhu);
strcpy(f->beizhu,ch);
}
}
}
print(h);
}
void booknamesearch(struct book *head) //按书名查找
{ struct book *p;
char name[20];
int c;
if(head==NULL) {printf("您为打开任何文件");getchar();return ;}
printf("查找专业--1, 查找书名--2:");
scanf("%d",&c);
getchar();
if(c==2)
printf("请输入要查找的书名:");
else printf("请输入要查找的专业:");
gets(name);
p=head;
do{ if(c==2)
if(strcmp(p->bookname,name)==0)
{
printf("教材编号:%s\n书名:%s\n数量:%d\n价格:%d\n专业:%s\n作者:%s\n出版社:%s\n备注:%s\n",p->number,p->bookname,p->count,p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
}
if(c==1)
if(strcmp(p->zhuanye,name)==0)
{
printf("教材编号:%s\n书名:%s\n数量:%d\n价格:%d\n专业:%s\n作者:%s\n出版社:%s\n备注:%s\n",p->number,p->bookname,p->count,p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
}
p=p->next;
}while(p!=NULL);
printf("查找完毕");
return;
}
struct book *add(struct book *head) //增加记录
{ struct book *p,*e,*f,*h;
if(head==NULL) {printf("您为打开任何文件");getchar(); return NULL;}
h=f=e=head;
p=(struct book *)malloc(sizeof(struct book));
printf("\n教材编号:");
gets(p->number);
printf("\n书名:");
gets(p->bookname);
printf("\n数量:");
scanf("%d",&p->count);
printf("\n价格:");
scanf("%d",&p->price);
getchar();
printf("\n专业:");
gets(p->zhuanye);
printf("\n作者:");
gets(p->author);
printf("\n出版社:");
gets(p->chubanshe);
printf("\n备注:");
gets(p->beizhu);
if(strcmp(f->number,p->number)>0) { p->next=f;h=p;printf("\n添加成功");return h;}
if(f->next==NULL) { f->next=p;p->next=NULL;printf("\n添加成功");return h;}
do{
if(f->next!=NULL)
if(strcmp(f->number,p->number)>0)
{
e->next=p;p->next=f;printf("\n添加成功");return h;
}
if(f->next==NULL)
{
f->next=p;
p->next=NULL;
printf("\n添加成功");
return h;
}
f=f->next;
e=e->next;
}while(1);
}
struct book *delet(struct book *head) //删除记录
{ struct book *p,*e;
char num[20];
if(head==NULL) {printf("您为打开任何文件");getchar();return NULL;}
printf("请输入要删除的教材编号:");
scanf("%s",num);
p=e=head;
if(strcmp(p->number,num)==0) { head=head->next; print(head);return head;}
else p=p->next;
do{
if(strcmp(p->number,num)==0)
{
if(p->next!=NULL)
e->next=p->next;
if(p->next==NULL) e->next=NULL;
print(head);
return head;
}
p=p->next;
e=e->next;
}while(p!=NULL);
printf("搜索完毕,未找到该记录");
}
struct book *change(struct book *head) //修改记录
{ struct book *p;
char num[20];
if(head==NULL) {printf("您为打开任何文件");getchar();return NULL;}
printf("请输入你要修改的教材编号:");
scanf("%s",num);
getchar();
p=head;
do{
if(strcmp(p->number,num)==0)
{
printf("教材编号:%s\n书名:%s\n数量:%d\n价格:%d\n专业:%s\n作者:%s\n出版社:%s\n备注:%s\n",p->number,p->bookname,p->count,p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
printf("\n教材编号:");
gets(p->number);
printf("\n书名:");
gets(p->bookname);
printf("\n数量:");
scanf("%d",&p->count);
printf("\n价格:");
scanf("%d",&p->price);
getchar();
printf("\n专业:");
gets(p->zhuanye);
printf("\n作者:");
gets(p->author);
printf("\n出版社:");
gets(p->chubanshe);
printf("\n备注:");
gets(p->beizhu);
printf("修改成功");
return head;
}
p=p->next;
}while(p!=NULL);
printf("未找到您要修改的记录");
return head;
}
int mima()
{ FILE *fp;
char mima1[20],mima2[20];
if((fp=fopen("mima","r"))==NULL)
{ printf("您尚未设置密码请输入:");
scanf("%s",mima1);
printf("请再次输入密码:");
scanf("%s",mima2);
if(strcmp(mima1,mima2)!=0) { printf("两次密码不一样");return 0;}
else {
fp=fopen("mima","w");
fprintf(fp,"%s",mima1);
printf("密码设置成功");
return 1;
}
}
printf("请输入初始化密码:");
scanf("%s",mima1);
fscanf(fp,"%s",mima2);
if(strcmp(mima1,mima2)==0)
{
printf("密码正确");
printf("请输入你要删除的文件名:");
scanf("%s",mima1);
if(remove(mima1)==0)
{
printf("删除成功");
getchar();
return 1;
}
else {
printf("删除失败,请检查是否存在该文件或路径是否正确");
getchar();
return 0;
}
}
else {
printf("密码错误");
getchar();
return 0;
}
}
void main()
{
struct book *head=NULL;
char i;
do{
printf(" *** 欢迎使用教材管理系统 ***\n");
printf("1---录入图书信息\n\n2---保存图书信息\n\n3---显示图书信息\n\n4---打开图*录\n\n5---增加一个记录\n\n6---删除一个记录\n\n7---修改一个记录\n\n8---查找\n\ns---初始化系统\n\ne---退出系统\n");
printf("\n请输入你要的功能编号:");
i=getchar();
getchar();
system("cls");
switch(i)
{
case '1': head=creat();save(head);break;
case '2': save(head);break;
case '3': print(head);break;
case '4': head=openfile();break;
case '5': head=add(head);break;
case '6': head=delet(head);break;
case '7': head=change(head);break;
case '8': booknamesearch(head);break;
case 's': mima();break;
case 'e': exit(0);
default :break;
}
getchar();
system("cls");
}while(1);
}
改下就是图书管理系统了
呵呵