效果圖如下:
C語言代碼如下:
//全部都是原創的,自己寫的,歷時近五天。
//運用了鏈表進行數據處理,然後用了文件存儲數據。每次處理後又重新存進文件。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//這個student就是讀者,也就是借書證的信息
struct student
{
char stu_num[10];
char stu_name[20];
char borrow_book[50];
struct student *next;
}*stu1,*stu2;
//這是書籍信息
struct book
{
char book_num[10];
char book_name[60];
char book_writer[20];
int book_price;
int here;
char borrow_book_man[20];
struct book *next;
}*book1,*book2;
//創建卡,也就是新的借書證,有了新讀者
int creat_ka(struct student *head1,int n)
{
FILE *fp;
int i;
struct student *tail,*pnew;
tail=head1;
system("cls");
for(i=0;i<n;i++)
{
tail=tail->next;
}
pnew=(struct student *)malloc(sizeof(struct student));
printf("輸入新辦的借書證號:");
scanf("%s",&pnew->stu_num);
printf("輸入新的讀者名:");
scanf("%s",pnew->stu_name);
strcpy(pnew->borrow_book,"無");
pnew->next=NULL;
tail->next=pnew;
fp=fopen("ka.txt","a");
fwrite(pnew,sizeof(struct student),1,fp);
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return ++n;
}
//增加書
int add_book(struct book *head,int n)
{
FILE *fp;
struct book *tail,*pnew;
int i;
tail=head;
system("cls");
for(i=0;i<n;i++)
{
tail=tail->next;
}
pnew=(struct book *)malloc(sizeof(struct book));
printf("新書的書號:\n");
scanf("%s",pnew->book_num);
printf("新書的書名:\n");
scanf("%s",pnew->book_name);
printf("新書作者:\n");
scanf("%s",pnew->book_writer);
printf("單價:\n");
scanf("%d",&pnew->book_price);
printf("請你設定書的狀態(1在架/0不在):\n");
scanf("%d",&pnew->here);
pnew->next=NULL;
tail->next=pnew;
fp=fopen("book.txt","a");
fwrite(pnew,sizeof(struct book),1,fp);
fclose(fp);
printf("***************************[操作成功]*****************************\n\n");
return ++n;
}
//刪除書
int del_book(struct book *head,int n)
{
FILE *fp;
struct book *tail,*pnew1,*pnew2;
char booknum[10];
int i,j,biaozhi_1;
tail=head->next;
pnew1=head;
pnew2=head->next;
system("cls");
printf("輸入要刪除的書的書號:\n");
scanf("%s",booknum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->book_num,booknum)==0)
{
i++;
biaozhi_1=0;
break;
}
tail=tail->next;
}
if(biaozhi_1)
{
printf("不存在這個書號\n");
return 0;
}
for(j=1;j<i;j++)
{
pnew1=pnew1->next;
}
pnew1->next=tail->next;
free(tail);
fp=fopen("book.txt","w");
if(pnew1->next)
{
while(pnew2->next);
{
fwrite(pnew2,sizeof(struct book),1,fp);
pnew2=pnew2->next;
}
}
else
{
fwrite(NULL,0,1,fp);
}
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return --n;
}
//註銷借書證
int del_ka(struct student *head,int n)
{
FILE *fp;
struct student *tail,*pnew1,*pnew2;
char ka_num[10];
int i,j,biaozhi_2;
tail=head->next;
pnew1=head;
pnew2=head->next;
system("cls");
printf("要刪除的卡號:\n");
scanf("%s",ka_num);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->stu_num,ka_num)==0)
{
i++;
biaozhi_2=0;
break;
}
tail=tail->next;
}
if(biaozhi_2)
{
printf("不存在這個卡號\n");
return 0;
}
for(j=1;j<i;j++)
{
pnew1=pnew1->next;
}
pnew1->next=tail->next;
free(tail);
fp=fopen("ka.txt","w");
if(pnew1->next)
{
while(pnew2->next);
{
fwrite(pnew2,sizeof(struct student),1,fp);
pnew2=pnew2->next;
}
}
else
{
fwrite(NULL,0,1,fp);
}
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return --n;
}
//按照書名搜索書籍信息
int search_book_1(struct book *head)
{
int i;
char bookname[50];
struct book *tail;
tail=head->next;
system("cls");
printf("請輸入要找的書的書名:");
scanf("%s",bookname);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->book_name,bookname)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("書籍庫爲空\n");
}
else
{
if(tail==0)
{
printf("書庫中沒有該書\n");
return 0;
}
}
system("cls");
printf("書號:%s 書名:%s 價格:%d元 狀態:",tail->book_num,tail->book_name,tail->book_price);
if(tail->here==0)
{
printf("不在\n");
}
else
printf("在架\n");
printf("\n");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//按照作者名字搜索書籍信息
//可以搜索同一作者的所有書籍
int search_book_2(struct book *head)
{
int i=0,xunhuan;
char writername[50];
struct book *tail;
tail=head->next;
system("cls");
printf("請輸入作者姓名:");
scanf("%s",writername);
for(xunhuan=0;tail!=0;xunhuan++)
{
for(;tail!=0;i++)
{
if(strcmp(tail->book_writer,writername)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("書籍庫爲空\n");
return 0;
}
else
{
if((tail==0)&&(xunhuan==0))
{
printf("書庫中沒有該作者的書\n");
return 0;
}
}
if(xunhuan==0)
{
system("cls");
}
printf("作者:%s 書號:%s 書名:%s 價格:%d元 狀態:",tail->book_writer,tail->book_num,tail->book_name,tail->book_price);
if(tail->here==0)
{
printf("不在\n");
}
else
{
printf("在架\n");
}
tail=tail->next;
}
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//按照書的書號搜書
int search_book_3(struct book *head)
{
int i;
char booknum[10];
struct book *tail;
tail=head->next;
system("cls");
printf("請輸入要找的書號:");
scanf("%s",booknum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->book_num,booknum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("書籍庫爲空");
return 0;
}
else
{
if(tail==0)
{
printf("書庫中沒有該書");
return 0;
}
}
system("cls");
printf("書號:%s 書名:%s 價格:%d元 狀態:",tail->book_num,tail->book_name,tail->book_price);
if(tail->here==0)
{
printf("不在,借書人:%s \n",tail->borrow_book_man);
}
else
printf("在架\n");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//根據借書證號找讀者信息
int search_ka_1(struct student *head)
{
int i;
char kanum[10];
struct student *tail;
tail=head->next;
system("cls");
printf("請輸入要找的書證號:");
scanf("%s",kanum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->stu_num,kanum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系統中借書證信息爲空\n");
return 0;
}
else
{
if(tail==0)
{
printf("系統中沒有該借書證\n");
return 0;
}
}
system("cls");
printf("讀者借書證號:%s 讀者姓名:%s 所借書籍:%s",tail->stu_num,tail->stu_name,tail->borrow_book);
printf("\n***************************[操作成功]*****************************\n\n");
return 0;
}
//根據讀者姓名找讀者信息
int search_ka_2(struct student *head)
{
int i;
char studentname[50];
struct student *tail;
tail=head->next;
system("cls");
printf("請輸入讀者姓名:");
scanf("%s",studentname);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->stu_name,studentname)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系統中借書證信息爲空\n");
return 0;
}
else
{
if(tail==0)
{
printf("系統中沒有該借書證\n");
return 0;
}
}
system("cls");
printf("讀者借書證號:%s 讀者姓名:%s 所借書籍:%s",tail->stu_num,tail->stu_name,tail->borrow_book);
printf("\n***************************[操作成功]*****************************\n\n");
return 0;
}
//根據所借書籍找讀者信息
int search_ka_3(struct student *head)
{
int i;
char borrow_book_name[50];
struct student *tail;
tail=head->next;
system("cls");
printf("請輸入書籍名:");
scanf("%s",borrow_book_name);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->borrow_book,borrow_book_name)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系統中借書證信息爲空\n");
return 0;
}
else
{
if(tail==0)
{
printf("系統中沒有該借書證\n");
return 0;
}
}
system("cls");
printf("讀者借書證號:%s 讀者姓名:%s 所借書籍:%s",tail->stu_num,tail->stu_name,tail->borrow_book);
printf("\n***************************[操作成功]*****************************\n\n");
return 0;
}
//搜借書證的開始的程序
int search_ka(struct student *head)
{
int k;
system("cls");
printf("1 按借書證號查找\n2 按讀者名字查找\n3 按所借書籍查找\n0 返回上一層\n");
shuru_2:
printf("請選擇功能0-3:");
scanf("%d",&k);
printf("**********************************\n");
switch(k)
{
case 1:search_ka_1(head);break;
case 2:search_ka_2(head);break;
case 3:search_ka_3(head);break;
case 0:
system("cls");
printf("****************[操作成功]********************\n\n");
return 0;
default:
printf("輸入錯誤,");goto shuru_2;
}
return 0;
}
//搜書的開始程序
int search_book(struct book *head)
{
int k;
system("cls");
printf("1 按書名查找\n2 按作者查找\n3 按書號查找\n0 返回上一層\n");
shuru_2:
printf("請選擇功能0-3:");
scanf("%d",&k);
printf("**********************************\n");
switch(k)
{
case 1:search_book_1(head);break;
case 2:search_book_2(head);break;
case 3:search_book_3(head);break;
case 0:
system("cls");
printf("****************[操作成功]********************\n\n");
return 0;
default:
printf("輸入錯誤,");goto shuru_2;
}
return 0;
}
//借書
int borrow_book(struct student *head1,struct book *head2)
{
FILE *fp,*fp2;
struct student *tail,*pnew;
struct book *tail_2,*pnew_2;
int i,j;//biaozhi_4;
char student_num[10];
char borrow_num[10];
system("cls");
tail=head1->next;
tail_2=head2->next;
pnew=head1->next;
pnew_2=head2->next;
printf("請輸入借書證號:");
scanf("%s",student_num);
for(i=0;tail!=0;i++)//根據借書證號找到該學生
{
if(strcmp(tail->stu_num,student_num)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系統中沒有該借書證號。\n");
return 0;
}
if(strcmp(tail->borrow_book,"無"))//如果借了書
{
printf("該讀者仍未還書,無法借書。\n");
return 0;
}
printf("請輸入書號:");
scanf("%s",borrow_num);
for(j=0;tail_2!=0;j++)//根據書號找到該書籍
{
if(strcmp(tail_2->book_num,borrow_num)==0)
{
j++;
break;
}
tail_2=tail_2->next;
}
if(j==0)
{
printf("書庫中不存在該書。\n");
return 0;
}
if(tail_2->here==0)
{
printf("該書不在架,已經外借\n");
return 0;
}
strcpy(tail->borrow_book,tail_2->book_name);
tail_2->here=0;
strcpy(tail_2->borrow_book_man,tail->stu_name);
fp=fopen("ka.txt","w");
while(pnew->next)
{
fwrite(pnew,sizeof(struct student),1,fp);
pnew=pnew->next;
}
fclose(fp);
fp2=fopen("book.txt","w");
while(pnew_2->next)
{
fwrite(pnew_2,sizeof(struct book),1,fp2);
pnew_2=pnew_2->next;
}
fclose(fp2);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//還書
int back_book(struct student *head,struct book *head2)
{
FILE *fp,*fp2;
struct student *tail,*pnew;
struct book *tail_2,*pnew_2;
int i;
int chose;
char kanum[10];
system("cls");
tail=head->next;
tail_2=head2->next;
pnew=head->next;
pnew_2=head2->next;
printf("請輸入卡號:");
scanf("%s",kanum);
for(i=0;tail!=0;i++)//根據卡號找到該學生
{
if(strcmp(tail->stu_num,kanum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系統中沒有該借書證號。\n");
return 0;
}
if(strcmp(tail->borrow_book,"無"))//如果借了書
{
fanhui_1:
printf("借的書是:%s\n",tail->borrow_book);
printf("確認歸還該書?\n(1 確認 0 取消)");
scanf("%d",&chose);
switch(chose)
{
case 1:
while(strcmp(tail->borrow_book,tail_2->book_name))//根據學生借的書找到該書
{
tail_2=tail_2->next;
}
tail_2->here=1;//更改書的狀態
strcpy(tail_2->borrow_book_man,"");
strcpy(tail->borrow_book,"");
break;
case 0:return 0;
default:printf("請輸入1或0\n");goto fanhui_1;
}
}
else
{
printf("該讀者還沒借書\n");
return 0;
}
fp=fopen("ka.txt","w");
fp2=fopen("book.txt","w");
while(pnew->next)
{
fwrite(pnew,sizeof(struct student),1,fp);
pnew=pnew->next;
}
while(pnew_2)
{
fwrite(pnew_2,sizeof(struct book),1,fp2);
pnew_2=pnew_2->next;
}
fclose(fp);
fclose(fp2);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//顯示書庫中的所有書籍
void printf_book(struct book *head)
{
struct book *tail;//*pnew;
int i;
tail=head->next;
system("cls");
for(i=1;tail!=0;i++)
{
printf("第%d本書是:\n",i);
printf("書號:%s 書名:%s 作者:%s 單價:%d元 狀態:",tail->book_num,tail->book_name,tail->book_writer,tail->book_price) ;
if(tail->here==0)
printf("不在,借書人爲:%s",tail->borrow_book_man);
else
printf("在");
printf("\n");
tail=tail->next;
}
if(i==1)
{
printf("*****************書庫爲空*******************\n");
}
printf("***************************[操作成功]*****************************\n\n");
}
//顯示所有讀者信息
void printf_ka(struct student *head)
{
struct student *tail;//*pnew;
int i;
tail=head->next;
system("cls");
for(i=1;tail!=0;i++)
{
printf("第%d個讀者是:\n",i);
printf("借書證號:%s 姓名:%s 所借書籍:%s\n",tail->stu_num,tail->stu_name,tail->borrow_book);
tail=tail->next;
}
if(i==1)
{
printf("*****************借書證存儲系統爲空*******************\n");
}
printf("***************************[操作成功]*****************************\n\n");
}
//修改書籍信息的程序
int change_1(struct book *head)
{
FILE *fp;
struct book *tail,*pnew;
struct book *new_change;
char booknum[20];
int i,chose;
system("cls");
new_change=(struct book*)malloc(sizeof(struct book));
tail=head->next;
pnew=head->next;
printf("請輸入要修改的書的書號:");
scanf("%s",booknum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->book_num,booknum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("書庫爲空\n");
return 0;
}
if(tail==0)
{
printf("書庫中不存在該書\n");
return 0;
}
printf("書籍原信息爲:\n");
printf("書號:%s 書名:%s 作者:%s 單價:%d元 狀態:",tail->book_num,tail->book_name,tail->book_writer,tail->book_price) ;
if(tail->here==0)
{
printf("不在,借書人爲:%s",tail->borrow_book_man);
}
else
{
printf("在");
}
shuru_9:
printf("\n1 修改書號\n2 修改書名\n3 修改作者\n4 修改單價\n5 修改狀態\n6 修改借書人\n0 取消,返回上一級\n");
scanf("%d",&chose);
switch(chose)
{
case 1:
printf("請輸入新書號:");
scanf("%s",new_change->book_num);
strcpy(tail->book_num,new_change->book_num);
break;
case 2:
printf("請輸入新書名:");
scanf("%s",new_change->book_name);
strcpy(tail->book_name,new_change->book_name);
break;
case 3:
printf("請輸入新作者:");
scanf("%s",new_change->book_writer);
strcpy(tail->book_writer,new_change->book_writer);
break;
case 4:
printf("請輸入新單價:");
scanf("%d",&new_change->book_price);
tail->book_price=new_change->book_price;
break;
case 5:
printf("請輸入新狀態(1 在架;2 不在):");
scanf("%d",&new_change->here);
tail->here=new_change->here;
break;
case 6:
printf("請輸入新的借書人:");
scanf("%s",new_change->borrow_book_man);
strcpy(tail->borrow_book_man,new_change->borrow_book_man);
break;
case 0:return 0;
default:printf("輸入錯誤,請重新輸入。\n");goto shuru_9;
}
free(new_change);
fp=fopen("book.txt","w");
while(pnew)
{
fwrite(pnew,sizeof(struct book),1,fp);
pnew=pnew->next;
}
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//修改借書證信息的程序
int change_2(struct student *head)
{
FILE *fp;
struct student *tail,*pnew;
struct student *new_change;
char studentnum[10];
int i,chose;
system("cls");
tail=head->next;
pnew=head->next;
printf("請輸入要修改的借書證的證號:");
scanf("%s",studentnum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->stu_num,studentnum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("借書證信息爲空\n");
return 0;
}
if(tail->next==0)
{
printf("系統中不存在該借書證\n");
return 0;
}
printf("讀者原信息爲:\n");
printf("借書證號:%s 姓名:%s 所借書籍:%s",tail->stu_num,tail->stu_name,tail->borrow_book);
shuru_8:
printf("\n1 修改借書證號\n2 修改讀者姓名\n3 修改所借書籍\n0 取消\n");
scanf("%d",&chose);
new_change=(struct student *)malloc(sizeof(struct student));
switch(chose)
{
case 1:printf("請輸入新借書證號:");scanf("%s",new_change->stu_num);strcpy(tail->stu_num,new_change->stu_num);break;
case 2:printf("請輸入新讀者姓名:");scanf("%s",new_change->stu_name);strcpy(tail->stu_num,new_change->stu_num);break;
case 3:printf("請輸入新所借書籍:");scanf("%s",new_change->borrow_book);strcpy(tail->borrow_book,new_change->borrow_book);break;
case 0:return 0;
default:printf("輸入錯誤,請重新輸入。\n");goto shuru_8;
}
free(new_change);
fp=fopen("ka.txt","w");
while(pnew)
{
fwrite(pnew,sizeof(struct student),1,fp);
}
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//主程序在此
int main()
{
int a[5],i=0,k=0;
struct student *s1;
struct book *b1;
FILE *fp1,*fp2;
s1=(struct student *)malloc(sizeof (struct student));
b1=(struct book *)malloc(sizeof (struct book));
s1->next=NULL;
b1->next=NULL;
stu1=s1;
book1=b1;
stu2=(struct student *)malloc(sizeof(struct student));
book2=(struct book *)malloc(sizeof(struct book));
fp1=fopen("ka.txt","a+");
fp2=fopen("book.txt","a+");
while(fread(stu2,sizeof(struct student),1,fp1)!=0)
{
stu2->next=NULL;
stu1->next=stu2;
stu1=stu2;
stu2=(struct student *)malloc(sizeof(struct student));
i++;
}
fclose(fp1);
while(fread(book2,sizeof(struct book),1,fp2)!=0)
{
book1->next=book2;
book1=book2;
book2=(struct book *)malloc(sizeof(struct book));
k++;
}
fclose(fp2);
printf("\t\t通信工程1154班\t【陳恆釗同學】獨立創作\n");
printf("\t\t目前系統內已存儲了:%d本書%d個借書證\n",k,i);
shuru_3:
printf("\t********************第1級頁面_超級圖書管理系統******************\n");
printf("【1】 借書\n【2】 還書\n【3】 圖書查詢\n【4】 借書證信息查詢\n【5】 系統管理\n【0】 關閉系統\n");
printf("請選擇功能0-5:");
scanf("%d",&a[0]);
switch(a[0])
{
case 0:printf("\t**************[成功關閉系統]*****************");return 0;
case 1:
printf("\t***************第2級頁面_借書系統**************\n");
borrow_book(s1,b1);goto shuru_3;
case 2:
printf("\t***************第2級頁面_還書系統**************\n");
back_book(s1,b1);goto shuru_3;
case 3:
printf("\t***************第2級頁面_圖書查詢**************\n");
search_book(b1);
goto shuru_3;
case 4:
printf("\t***************第2級頁面_借書證信息查詢**************\n");
search_ka(s1);goto shuru_3;
case 5:
second_1:
system("cls");
second_11:
printf("\t***************第2級頁面_系統管理**************\n");
printf("【1】 新辦借書證\n【2】 註銷借書證\n【3】 新增圖書\n【4】 刪除圖書\n【5】 修改舊書信息\n【6】 顯示書庫中所有書籍\n【7】 顯示所有借書證信息\n【0】 返回上一級\n");
printf("請選擇功能0-5:");
scanf("%d",&a[1]);
switch(a[1])
{
case 1:i=creat_ka(s1,i);goto second_11;
case 2:i=del_ka(s1,i);goto second_11;
case 3:k=add_book(b1,k);goto second_11;
case 4:k=del_book(b1,k);goto second_11;
case 5:change_1(b1);goto second_11;
case 6:printf_book(b1);goto second_11;
case 7:printf_ka(s1);goto second_11;
case 8:change_2(s1);goto second_1;
case 0:system("cls");goto shuru_3;
default:goto second_1;
}
default:printf("***********輸入錯誤***************\n");goto shuru_3;
}
}