图书馆管理系统代码 要正确无误的C程序代码
发布网友
发布时间:2023-03-31 13:29
我来回答
共2个回答
热心网友
时间:2023-11-11 10:59
在邮箱 来分
热心网友
时间:2023-11-11 11:00
#include"stdio.h"
#include"string.h"
struct book /*定义结构体*/
{ char number[20];
char name[20];
char press[20];
char writer[20];
int n; /*现有量*/
int m; /*总量*/
}a[10];
int b; /*输入书的数量*/
void input() /*输入图书信息函数*/
{
FILE *fp;
char str2[10]={" "};
int i;
if((fp=fopen("d:\\wen","w"))==NULL)
{ printf("Open failed\n");
return; }
printf("\nHow many books do you want to input?\n");
scanf("%d",&b);
for(i=0;i<b;i++)
{
printf("Enter the %d book's number name press writer now all\n",i+1);
scanf("%s%s%s%s%d%d",&a[i].number,&a[i].name,&a[i].press,&a[i].writer,&a[i].n,&a[i].m);
fwrite(&a[i],sizeof(struct book),1,fp);
}
fclose(fp);
}
void Search() /*查询书籍*/
{
FILE *fp;
int w,i; /*w查询方式使用的数据*/
char str[20]; /*查询书籍的编码书名出版社作者*/
if((fp=fopen("d:\\wen","r"))==NULL)
{ printf("Open failed\n");
return; }
for(i=0;i<b;i++)
if(fread(&a[i],sizeof(struct book),1,fp)!=1)
printf("file read error\n");
printf("search by number--6\n");
printf("search by name--7\n");
printf("search by writer--8\n");
printf("search by press--9\n");
scanf("%d",&w);
printf("Enter the word you want to search:\n");
scanf("%s",str);
switch(w)
{
case 6:
{for(i=0;i<b;i++)
if(strcmp(a[i].number,str)==0)
{printf("number: %s name: %s press: %s writer: %s now: %d all:%d\n",a[i].number,a[i].name,a[i].press,a[i].writer,a[i].n,a[i].m);
break;
}
}
break;
case 7:
{for(i=0;i<b;i++)
if(strcmp(a[i].name,str)==0)
{printf("number: %s name: %s press: %s writer: %s now: %d all:%d\n",a[i].number,a[i].name,a[i].press,a[i].writer,a[i].n,a[i].m);
break;}
}
break;
case 8:
{for(i=0;i<b;i++)
if(strcmp(a[i].press,str)==0)
{printf("number: %s name: %s press: %s writer: %s now: %d all:%d\n",a[i].number,a[i].name,a[i].press,a[i].writer,a[i].n,a[i].m);
break;}
}
break;
case 9:
{for(i=0;i<b;i++)
if(strcmp(a[i].writer,str)==0)
{ printf("number: %s name: %s press: %s writer: %s now: %d all:%d\n",a[i].number,a[i].name,a[i].press,a[i].writer,a[i].n,a[i].m);
break; }
}
}
fclose(fp);
}
void borrow() /*借阅图书函数*/
{ FILE *fp;
int i;
char xuehao[20],number2[20];
char jieyue[20]={"borrow:"};
if((fp=fopen("d:\\wen00.txt","w"))==NULL)
{ printf("Open failed\n");
return; }
printf("Enter your number:\n");
scanf("%s",xuehao);
printf("Enter the book's number which you want to borrow\n");
scanf("%s",number2);
for(i=0;i<10;i++)
if(strcmp(a[i].number,number2)==0)
if(a[i].n>0)
{a[i].n--;
printf("borrow sucess!\n");
if(fwrite(&xuehao[20],sizeof(xuehao[20]),1,fp)!=1)
printf("file write error\n");
if(fwrite(&jieyue[20],sizeof(jieyue),1,fp)!=1)
printf("file write error\n");
if(fwrite(&a[i],sizeof(struct book),1,fp)!=1)
printf("file write error\n");
break;
}
else
printf("borrow failed!");
fclose(fp);
}
void giveback() /*归还图书函数*/
{FILE *fp;
int i;
char xuehao[20],number2[20];
char guihuan2[20]={"give back"};
if((fp=fopen("d:\\wen00.txt","w"))==NULL)
{printf("cannot open file\n");
return;
}
printf("Enter your number:\n");
scanf("%s",xuehao);
printf("Enter the book's number which you want to give back\n");
scanf("%s",number2);
for(i=0;i<1000;i++)
if(strcmp(a[i].number,number2)==0)
{ if(fwrite(&xuehao[20],sizeof(xuehao),1,fp)!=1)
printf("file write error\n");
if(fwrite(&guihuan2[20],sizeof(guihuan2),1,fp)!=1)
printf("file write error\n");
if(fwrite(&a[i],sizeof(a[i]),1,fp)!=1)
printf("file write error\n");
break;
}
printf("give back sucess!\n");
fclose(fp);
}
void adddelet() /*增加删除功能*/
{ FILE *fp;
char number3[20];
int s,i,j,t; /*t为增加或删除 s增加书的数量*/
if((fp=fopen("d:\\wen00.txt","w"))==NULL)
{printf("cannot open file\n");
return ;
}
printf("Chose what you want to do:");
printf("add books--11\n");
printf("delet books--12\n");
scanf("%d",&t);
switch(t)
{
case 11:
{printf("Enter the book'number which you want to add:");
scanf("%s",number3);
printf("How many books do you want to add?:");
scanf("%d",&s);
for(i=0;i<10;i++)
if(strcmp(a[i].number,number3)==0)
a[i].m=a[i].m+s;
} break;
default :
{printf("Enter the book'number which you want to delet:");
scanf("%s",number3);
for(i=0;i<10;i++)
if(strcmp(a[i].number,number3)==0)
for(;i<10;i++)
{ for(j=0;j<20;j++)
a[i].number[j]=a[i+1].number[j];
for(j=0;j<20;j++)
a[i].name[j]=a[i+1].name[j];
for(j=0;j<20;j++)
a[i].press[j]=a[i+1].press[j];
for(j=0;j<20;j++)
a[i].writer[j]=a[i+1].writer[j];
a[i].n=a[i+1].n;
a[i].m=a[i+1].m;
}
}
}
fclose(fp);
}
void main() /*主函数*/
{
int q;
printf("Welcome to labrary!\nChose what you want to do:\n");
printf("Input book's information--1;\n");
printf("serach books--2;\n");
printf("borrow books--3;\n");
printf("add or delet books--4;\n");
printf("give back books--5;\n");
printf("quit--6;\n");
printf("Please chose zhe number:");
while(1)
{
switch(getchar())
{
case '1': input();break;
case '2': Search();break;
case '3': borrow() ;break;
case '4': adddelet();break;
case '5': giveback();break;