您好,登錄后才能下訂單哦!
#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 ERROR 0
/*定義學生類型,用于存放借出的書籍*/
struct student {
int carnum;
char lendbook[10];
}student[1000];
/*定義書的類型*/
struct book
{
int booknum;
char bookname[20];
float bookprice;
char bookcreat[20];
int turefalse; /*用于借書和還書模塊判斷一本書是否借出的條件*/
}book[1000];
/*借書卡的數(shù)據(jù)類型*/
struct car
{
int carnum;
char sex[5];
char studentname[20];
int studentclass;
}car[1000];
/*查詢圖書信息模塊*/
/*(1)按書名查找*/
findbook()
{
FILE *fp;
char bookname[10];
int i;
fp=fopen("book.txt","r");
printf("請輸入你要查找的書名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(structbook),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)==0)
{
if(book[i].turefalse==1)
{
printf(" 書號 書名 作者 \n %d %s %s \n",book[i].booknum,book[i].bookname,book[i].bookcreat);
printf("此書現(xiàn)在無人借閱\n按任意鍵返回\n");}
else {printf("這本書已經(jīng)有人借出\n");fclose(fp);return 0;}
fclose(fp);
return 0;
}
}
printf("抱歉無此書!\n");
fclose(fp);
return 0;
}
/*(2)按作者查找*/
findbook1()
{
FILE *fp;
char bookcreat[10];
int i;
fp=fopen("book.txt","r");
printf("請輸入作者名:\n");
scanf("%s",bookcreat);
for(i=0;fread(&book[i],sizeof(structbook),1,fp)!=0;i++)
{
if(strcmp(bookcreat,book[i].bookcreat)==0)
{
if(book[i].turefalse==1)
{
printf(" 書號 書名 作者\n %d %s %s\n",book[i].booknum,book[i].bookname,book[i].bookcreat);
printf("此書現(xiàn)在無人借閱\n按任意鍵返回\n");
}
else {printf("此書已借出!\n");fclose(fp);
return 0;
}
fclose(fp);
return 0;
}
}
printf("沒有此書籍\n");
fclose(fp);
return 0;
}
chabook()/*查找圖書主模塊 */
{
char ch6;
do
{
printf("!?。g迎進入圖書查詢系統(tǒng)?。?!\n");
printf(" 1: 按書名查找\n");
printf(" 2: 按作者查找\n");
printf(" 0: 返回\n");
printf("請輸入0━2:\n");
ch6=getch();
switch(ch6)
{
case '1':findbook();getch();break;
case '2':findbook1();getch();break;
case '0':break;
default:printf("無此操作!\n");getch();break;
}
}while(ch6!='0');
return 0;
}
/*借書與還書模塊*/
/*(1).借書模塊*/
lendbook()
{
FILE *fp,*fp2;
int i,n;
int carnum;
system("cls");
printf("\t\t*****歡迎進入借書系統(tǒng)*****\n\n");
printf("請輸入借書人的:\n卡號:\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(structcar),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();return0;}
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 0;
}
}
printf("不存在這樣的書,任意鍵返回...\n");
fclose(fp);
getch();
return 0;
}
}
printf("你的卡號不存在,請申請新卡,按任意鍵返回...\n");
fclose(fp);
getch();
return 0;}
/*(2).還書函數(shù)*/
huanbook()
{
FILE *fp,*fp2; /*定義兩個文件指針,fp2用于修改數(shù)據(jù)時設立臨時文件用,防止數(shù)據(jù)遭破壞*/
int i,n;
int carnum;
char lendbook[10];
system("cls");
printf("\t\t@@@@歡迎進入還書界面@@@@");
printf("\n請你輸入你的\n卡號:\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r");/*讀取卡號記錄*/
for(i=0;fread(&car[i],sizeof(structcar),1,fp)!=0;i++) /*for循環(huán)判斷卡號是否存在*/
{
if(car[i].carnum==carnum) /*卡號存在,進入下一循環(huán)*/
{
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) /*借閱了該書,進入下一循環(huán),否則出錯顯示*/
{
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); /*將還的書的原來狀態(tài)設為無人借閱的*/
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("沒這樣的卡,和管理員聯(lián)系,按任意鍵返回...\n"); /*出錯提示*/
fclose(fp);
getch();
return 0;}
/*(3).借書統(tǒng)計*/
lendcount()
{
FILE *fp;
int i,n=0;
fp=fopen("record.txt","r");
for(i=0;fread(&student[i],sizeof(structstudent),1,fp)!=0;i++)
{
printf("卡號 借出的書籍 \n");
printf("%d %s\n",student[i].carnum,student[i].lendbook);
n=n+1;
}
fclose(fp);
printf("目前共有%d本書借出\n",n);
printf("按任意鍵...\n");
getch();
return n;
}
/*會員卡管理模塊*/
/*(1).增加會員卡*/
addcar()
{
FILE *fp;
int i=0;
fp=fopen("car.txt","a+");
printf("卡號:\n");
scanf("%d",&car[i].carnum);
printf("性別:\n");
scanf("%s",car[i].sex);
printf("姓名:\n");
scanf("%s",car[i].studentname);
printf("班級:\n");
scanf("%d",&car[i].studentclass);
fwrite(&car[i],sizeof(structcar),1,fp);
fclose(fp);
printf("輸入完畢,任意鍵返回...\n");
getch();
}
/*(2).刪除會員卡*/
delcar()
{
FILE *fp,*fp2;
int i;
int carnum;
char choice;
fp=fopen("car.txt","r");
fp2=fopen("bookl.txt","w");
printf("要刪除的卡號(不存在則不作改動):\n");
scanf("%d",&carnum);
for(i=0;fread(&car[i],sizeof(structcar),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按任意鍵返回...\n");
getch();
return 0;
}
else
{
printf("操作已取消!\n按任意鍵返回...\n");
getch();
return 0;
}
}
/*(3).會員卡統(tǒng)計*/
carcount()
{
FILE *fp;
int i,n=0;
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(structcar),1,fp)!=0;i++)
{
printf("第%d張卡 \n",i+1);
printf("卡號 性別 姓名 班級 \n");
printf(" %d %5s %10s %3d\n\n",car[i].carnum,car[i].sex,car[i].studentname,car[i].studentclass);
n=n+1;
}
fclose(fp);
printf("目前共有%d個會員。\n",n);
printf("按任意鍵返回...\n");
getch();
}
/*圖書管理模塊*/
/*(1).增加圖書信息*/
addbook()
{
FILE *fp;
int i=0;
char choice='y';
fp=fopen("book.txt","a+");
while(choice=='y'||choice=='Y')
{
printf("書號:\n");
scanf("%d",&book[i].booknum);
printf("書名:\n");
scanf("%s",book[i].bookname);
printf("作者:\n");
scanf("%s",book[i].bookcreat);
printf("單價:\n");
scanf("%f",&book[i].bookprice);
printf("請你設定書的狀態(tài)(1在架/0不在):\n");
scanf("%d",&book[i].turefalse);
fwrite(&book[i],sizeof(struct book),1,fp);
printf("是否要輸入下本書(y/n)\n");
scanf("%s",&choice);
}
fclose(fp);
}
/*(2).刪除圖書信息*/
delbook()
{
FILE *fp,*fp2;
int i;
char bookname[10],choice;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("書名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(structbook),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)!=0)
{
fwrite(&book[i],sizeof(struct book),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("確認刪除(y/n)?\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
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);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意鍵返回...\n");
getch();
return 0;
}
else
{
printf("按任意鍵返回...\n");
getch();
return 0;
}
}
/*(3)修改圖書*/
changebook()
{
FILE *fp,*fp2;
char bookname[10],choice;
int i;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("書名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(structbook),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,bookname)==0)
{
printf("你所要修改的書的資料如下,請選擇你要修改的內容:\n");
printf("序號 書名 作者 \n");
printf("%d %s %s\n",book[i].booknum,book[i].bookname,book[i].bookcreat); printf("1:修改書號:\n");
printf("2: 修改書名:\n");
printf("3: 修改作者:\n");
printf("4: 修改單價:");
printf("請輸入1━3:");
choice=getch();
switch(choice)
{
case '1':
{
printf("新書號:\n");
scanf("%d",&book[i].booknum);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '2':
{
printf("新書名:\n");
scanf("%s",book[i].bookname);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '3':
{
printf("新的作者:\n");
scanf("%s",book[i].bookcreat);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '4':
{
printf("新的單價:");
scanf("%f",book[i].bookprice);
fwrite(&book[i],sizeof(struct book),1,fp2); }
default:printf("沒有這樣的操作!");break;
}
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(structbook),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意鍵返回...\n");
getchar();
return 0;
}
/*(4).圖書統(tǒng)計*/
bookcount()
{
FILE *fp;
int i,n=0;
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(structbook),1,fp)!=0;i++)
{
if(book[i].booknum!=0&&strlen(book[i].bookname)!=0&&strlen(book[i].bookcreat)!=0)
{
printf("第%d本書\n序號 書名 作者 單價 狀態(tài) \n",i+1);
printf(" %d %10s ",book[i].booknum,book[i].bookname);
printf("%15s %4.2f %d(1在/0不在)\n",book[i].bookcreat,book[i].bookprice,book[i].turefalse);n=n+1;
}
}
fclose(fp);
printf("目前共有%d本書.\n",n);
printf("按任意鍵返回...\n");
}
/*結束界面函數(shù)*/
end()
{ system("cls");
printf("\t\t********************************\n");
printf("\t\t********************************\n");
printf("\t\t**** 謝謝使用本系統(tǒng) , 再見! ****\n");
printf("\t\t********************************\n");
printf("\t\t********************************\n");
}
/*主函數(shù)*/
main()
{
char ch2,ch3,ch5;
do
{ system("cls");
printf("\t\t********************************\n");
printf("\t\t********************************\n");
printf("\t\t***** 歡迎進入圖書管理系統(tǒng) *****\n");
printf("\t\t********************************\n");
printf("\t\t********************************\n");
printf(" 請選擇:\n");
printf(" 1:查閱圖書 \n");
printf(" 2:借閱圖書 \n");
printf(" 3:管理系統(tǒng) \n");
printf(" 4:還書 \n");
printf(" 0:退出 \n");
printf("請輸入0━4:\n");
ch2=getch();
switch(ch2)
{
case '1':chabook();break;
case '2':lendbook();;break;
case '3':{
do
{
system("cls");
printf("\t\t*******歡迎進入管理系統(tǒng)*******\n");
printf(" 1:增加圖書 \n");
printf(" 2:刪除圖書 \n");
printf(" 3:修改圖書 \n");
printf(" 4:圖書統(tǒng)計 \n");
printf(" 5:會員管理系統(tǒng) \n");
printf(" 0:返回 \n");
printf("請輸入0━5:\n");
ch3=getch();
switch(ch3)
{
case '1':addbook();
break;
case '2':delbook();break;
case '3':changebook();break;
case '4':bookcount();getch();break;
case '5':{
do
{
system("cls");
printf("\t\t····歡迎進入學生管理系統(tǒng)····\n");
printf(" 1: 申請新卡\n");
printf(" 2: 刪除卡號\n");
printf(" 3: 借書統(tǒng)計\n");
printf(" 4: 卡號統(tǒng)計\n");
printf(" 0: 返回\n");
printf("請輸入0━4:\n");
ch5=getch();
switch(ch5)
{
case '1':addcar();break;
case '2':delcar();break;
case '3':lendcount();break;
case '4':carcount();break;
case '0':break;
default:printf("無此操作!\n");getch();break;
}
}while(ch5!='0');}break;
case '0':break;
default:printf("無此操作!\n");getch();break;
}
} while(ch3!='0');}break;
case '4':huanbook();break;
case '0':break;
default:printf("無此操作!\n");getch();break;
}
}while(ch2!='0');
}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。