1. 程式人生 > >C語言——圖書管理系統

C語言——圖書管理系統

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<malloc.h>
#include<windows.h>
#include<ctype.h>

struct time{
	int year;
	int month;
	int day;
};
typedef struct library{
	long booknum;
	char bookname[20];
	char author[20];
	int speciesnumber;
	char publishers[30];
	struct time publish_time;
	float price;
	struct library *next;
}*Booklist,BOOK;

int count;
Booklist lHead;
/******************************************************/
void deletebook();
void changebook();
void searchbook_num();
void searchbook_name();
void searchbook_author();
void searchbook_speciesnumber();
void searchbook_publisher();
void searchbook();
void addbooks();
void readfile();
void savefile();
Booklist inputbooks();
void outputbooks();
void printscreen2();
void printscreen1();
void printscreen0();
/******************************************************/

/* 主函式 */
int main()
{    
    printscreen0();
	return 0;
}
/* 主介面 */
void printscreen0()
{
	char choice='1';
	while(choice!='0'){
	    system("cls");
	    printf("\t********************************************\n\n");
		printf("\t            歡迎進入圖書管理系統\n\n");
	    printf("\t       ====     1.  管理圖書     ====\n\n");
		printf("\t       ====     2.  讀取檔案     ====\n\n");
		printf("\t       ====     3.  儲存檔案     ====\n\n");
	    printf("\t       ====     0.    退出       ====\n\n");
	    printf("\t********************************************\n\n");
        printf("\t         (請用數字選擇要進行的操作!)\n");
    	if( (choice=getche()) != '0' )
	    switch(choice){
            case '1':    printscreen1();    break;
	        case '2':    readfile();    break;
			case '3':    savefile();    break;
	        default:     break;
		}
	}
}
/* 圖書資訊管理介面 */
void printscreen1()   
{
	char choice='1';
	while(choice!='0'){
	   system("cls");
	    printf("\t********************************************\n\n");
	    printf("\t          歡迎進入圖書資訊管理系統\n\n");
        printf("\t      ====     1.  檢視圖書       ====\n\n");
        printf("\t      ====     2.    搜尋         ====\n\n");
        printf("\t      ====     3.新增圖書資訊     ====\n\n");
		printf("\t      ====     4.修改圖書資訊     ====\n\n");
	    printf("\t      ====     5.刪除圖書資訊     ====\n\n");
        printf("\t      ====     6.錄入圖書資訊     ====\n\n");
	    printf("\t      ====     0.    退出         ====\n\n");
     	printf("\t*******************************************\n\n");
        printf("\t         (請用數字選擇要進行的操作!)\n");
	    if( (choice=getche()) != '0' )
        switch(choice){
	        case '1':    outputbooks();    break;
	        case '2':    searchbook();     break;
	        case '3':    addbooks();    break;
	        case '4':    changebook();    break;
			case '5':    deletebook();    break;
			case '6':    lHead=inputbooks();    break;
	        default:     break;
		}
	}
}
/* 檢視圖書 */
void outputbooks()
{
     Booklist pTemp;
	 pTemp=lHead;
	 system("cls");
	 printf("\t************共有%d本書************\n\n",count);
	 int index=1;
	 while(pTemp!=NULL){
		printf("\t------------第%d本書-----------\n\n",index);
        printf("\t圖書號:  %08ld",pTemp->booknum);
	    printf("\n\t圖書名:  %s",pTemp->bookname);
	    printf("\n\t作者:  %s",pTemp->author);
	    printf("\n\t圖書類別號碼:  %03d",pTemp->speciesnumber);
	    printf("\n\t出版社:  %s",pTemp->publishers);
	    printf("\n\t出版時間:  ");
	    printf("%04d-%02d-%02d",pTemp->publish_time.year,
		                 pTemp->publish_time.month,
			    		 pTemp->publish_time.day);
        printf("\n\t價格:  %.2f",pTemp->price);
        printf("\n\n\t-------------------------------\n\n");
		pTemp=pTemp->next;
		index++;
	 }
	 printf("按任意鍵繼續!\n"); 
	 getch();
}
/* 搜尋 */
void searchbook()
{
	while(1){
	    system("cls");
	    printf("\t\t請選擇搜尋方式:\n\n");
	    printf("\t\t  *********  1-圖書號\n\n"); 
	    printf("\t\t  *********  2-圖書名\n\n"); 
	    printf("\t\t  *********  3-作者\n\n"); 
	    printf("\t\t  *********  4-出版社\n\n"); 
	    printf("\t\t  *********  5-圖書型別號\n\n");
	    printf("\t\t  *********  0-退出\n\n");
	    printf("\t\t  ----------------請選擇:");
	    int choice=1;
	    scanf("%d",&choice);
	    switch(choice){
		    case 1:  searchbook_num();    break;
		    case 2:  searchbook_name();   break;
		    case 3:  searchbook_author();    break;
		    case 4:  searchbook_publisher();    break;
		    case 5:  searchbook_speciesnumber();    break;
		    case 0:  return;
	        default:   break; 
	    }
	}
}
/* 按圖書號搜尋 */
void searchbook_num()
{
	int book_num;
	int index=0;
	printf("\n\t\t*********請輸入圖書號:");
	scanf("%d",&book_num);
	Booklist pTemp;
	pTemp=lHead;
	int flag=1;
	while(index<count){
		if(pTemp->booknum==book_num){
			printf("\n\t\t******************\n\n");
			printf("\t\t圖書號:  %08ld",pTemp->booknum);
	        printf("\n\t\t圖書名:  %s",pTemp->bookname);
	        printf("\n\t\t作者:  %s",pTemp->author);
	        printf("\n\t\t圖書類別號碼:  %03d",pTemp->speciesnumber);
	        printf("\n\t\t出版社:  %s",pTemp->publishers);
	        printf("\n\t\t出版時間:  ");
	        printf("%04d-%02d-%02d",pTemp->publish_time.year,
		                     pTemp->publish_time.month,
			    	    	 pTemp->publish_time.day);
            printf("\n\t\t價格:  %.2f",pTemp->price);
            printf("\n\n\t\t-------------------------------\n\n");
            flag=0;
		}   
		pTemp=pTemp->next;
		index++; 
	}
	if(flag)	printf("\n\t\t**********  未找到圖書!");
	printf("\t\t-----  按任意鍵繼續...");
    getch();
}
/* 按圖書名搜尋 */
void searchbook_name()
{
	char book_name[20];
	int index=0;
	printf("\n\t\t*********請輸入圖書名:");
	fflush(stdin);
	gets(book_name);
	Booklist pTemp;
	pTemp=lHead;
	int flag=1;
	while(index<count){
		if(strcmp(book_name,pTemp->bookname)==0){
			printf("\n\t\t******************\n\n");
			printf("\t\t圖書號:  %08ld",pTemp->booknum);
	        printf("\n\t\t圖書名:  %s",pTemp->bookname);
	        printf("\n\t\t作者:  %s",pTemp->author);
	        printf("\n\t\t圖書類別號碼:  %03d",pTemp->speciesnumber);
	        printf("\n\t\t出版社:  %s",pTemp->publishers);
	        printf("\n\t\t出版時間:  ");
	        printf("%04d-%02d-%02d",pTemp->publish_time.year,
		                     pTemp->publish_time.month,
			    	    	 pTemp->publish_time.day);
            printf("\n\t\t價格:  %.2f",pTemp->price);
            printf("\n\n\t\t-------------------------------\n\n");
            flag=0;
		}   
		pTemp=pTemp->next;
		index++; 
	}
	if(flag)	printf("\n\t\t**********  未找到圖書!");
	printf("\t\t-----  按任意鍵繼續...");
	getch();
}
/* 按作者搜尋 */
void searchbook_author()
{
	char book_author[20];
	int index=0;
	printf("\n\t\t*********請輸入作者:");
	fflush(stdin);
	gets(book_author);
	Booklist pTemp;
	pTemp=lHead;
	int flag=1;
	while(index<count){
		if(strcmp(book_author,pTemp->author)==0){
			printf("\n\t\t******************\n\n");
			printf("\t\t圖書號:  %08ld",pTemp->booknum);
	        printf("\n\t\t圖書名:  %s",pTemp->bookname);
	        printf("\n\t\t作者:  %s",pTemp->author);
	        printf("\n\t\t圖書類別號碼:  %03d",pTemp->speciesnumber);
	        printf("\n\t\t出版社:  %s",pTemp->publishers);
	        printf("\n\t\t出版時間:  ");
	        printf("%04d-%02d-%02d",pTemp->publish_time.year,
		                     pTemp->publish_time.month,
			    	    	 pTemp->publish_time.day);
            printf("\n\t\t價格:  %.2f",pTemp->price);
            printf("\n\n\t\t-------------------------------\n\n");
            flag=0;
		}   
		pTemp=pTemp->next;
		index++; 
	}
	if(flag)	printf("\n\t\t**********  未找到圖書!");
	printf("\t\t-----  按任意鍵繼續...");
    getch();
}
/* 按圖書型別號搜尋 */
void searchbook_speciesnumber()
{
	int book_speciesnumber;
	int index=0;
	printf("\n\t\t*********請輸入圖書型別號:");
	scanf("%d",&book_speciesnumber);
	Booklist pTemp;
	pTemp=lHead;
	int flag=1;
	while(index<count){
		if(pTemp->speciesnumber==book_speciesnumber){
			printf("\n\t\t******************\n\n");
			printf("\t\t圖書號:  %08ld",pTemp->booknum);
	        printf("\n\t\t圖書名:  %s",pTemp->bookname);
	        printf("\n\t\t作者:  %s",pTemp->author);
	        printf("\n\t\t圖書類別號碼:  %03d",pTemp->speciesnumber);
	        printf("\n\t\t出版社:  %s",pTemp->publishers);
	        printf("\n\t\t出版時間:  ");
	        printf("%04d-%02d-%02d",pTemp->publish_time.year,
		                     pTemp->publish_time.month,
			    	    	 pTemp->publish_time.day);
            printf("\n\t\t價格:  %.2f",pTemp->price);
            printf("\n\n\t\t-------------------------------\n\n");
            flag=0;
		}   
		pTemp=pTemp->next;
		index++; 
	}
	if(flag)	printf("\n\t\t**********  未找到圖書!");
	printf("\t\t-----  按任意鍵繼續...");
    getch();
}
/* 按出版社搜尋 */
void searchbook_publisher()
{
	char book_publisher[30];
	int index=0;
	printf("\n\t\t*********請輸入出版社:");
	fflush(stdin);
	gets(book_publisher);
	Booklist pTemp;
	pTemp=lHead;
	int flag=1;
	while(index<count){
		if(strcmp(book_publisher,pTemp->publishers)==0){
			printf("\n\t\t******************\n\n");
			printf("\t\t圖書號:  %08ld",pTemp->booknum);
	        printf("\n\t\t圖書名:  %s",pTemp->bookname);
	        printf("\n\t\t作者:  %s",pTemp->author);
	        printf("\n\t\t圖書類別號碼:  %03d",pTemp->speciesnumber);
	        printf("\n\t\t出版社:  %s",pTemp->publishers);
	        printf("\n\t\t出版時間:  ");
	        printf("%04d-%02d-%02d",pTemp->publish_time.year,
		                     pTemp->publish_time.month,
			    	    	 pTemp->publish_time.day);
            printf("\n\t\t價格:  %.2f",pTemp->price);
            printf("\n\n\t\t-------------------------------\n\n");
            flag=0;
		}   
		pTemp=pTemp->next;
		index++; 
	}
	if(flag)	printf("\n\t\t**********  未找到圖書!");
	printf("\t\t-----  按任意鍵繼續...");
    getch();
}
/* 新增圖書資訊 */
void addbooks()
{
	system("cls");
	char ch='Y';
	Booklist pTemp,pPre;
	pTemp=lHead;
	for(int i=0;i<count-1;i++)
		pTemp=pTemp->next;
	pPre=pTemp;
	while(toupper(ch)=='Y'){
	    pTemp=(Booklist)malloc(sizeof(BOOK));
	    printf("\n\t------------第%d本書-----------\n\n",count+1);
        printf("\t圖書號:  ");
	    scanf("%ld",&pTemp->booknum);
		fflush(stdin);
	    printf("\n\t圖書名:  ");
	    gets(pTemp->bookname);
	    printf("\n\t作者:  ");
	    gets(pTemp->author);
	    printf("\n\t圖書類別號碼:  ");
	    scanf("%d",&pTemp->speciesnumber);
		fflush(stdin);
	    printf("\n\t出版社:  ");
	    gets(pTemp->publishers);
	    printf("\n\t出版時間:  ");
	    scanf("%d-%d-%d",&pTemp->publish_time.year,
		                 &pTemp->publish_time.month,
			    		 &pTemp->publish_time.day);
        printf("\n\t價格:  ");
	    scanf("%f",&pTemp->price);
        printf("\n\n\t-------------------------------\n\n");
        pTemp->next=NULL;
	    pPre->next=pTemp;
	    pPre=pTemp;
	    count++;
	    printf("\t是否繼續新增(Y/N):");
			ch=getche();
	}
}
/* 修改圖書資訊 */
void changebook()
{
	Booklist pTemp;
	pTemp=lHead;
	system("cls");
	printf("\n\t\t*********  修改操作\n");
	int book_num;
	printf("\n\t\t-----請輸入圖書號:");
	scanf("%d",&book_num);
	while(pTemp->booknum!=book_num){
		if(pTemp->next==NULL){
		    printf("\n\t\t-----  未找到圖書!按任意鍵退出...\n");
		    getch();
		    return;
    	}      
    	pTemp=pTemp->next;
	}
	printf("\n\t\t******************\n\n");
	printf("\t\t圖書號:  %08ld",pTemp->booknum);
    printf("\n\t\t圖書名:  %s",pTemp->bookname);
    printf("\n\t\t作者:  %s",pTemp->author);
	printf("\n\t\t圖書類別號碼:  %03d",pTemp->speciesnumber);
	printf("\n\t\t出版社:  %s",pTemp->publishers);
	printf("\n\t\t出版時間:  ");
	printf("%04d-%02d-%02d",pTemp->publish_time.year,
		                     pTemp->publish_time.month,
			    	    	 pTemp->publish_time.day);
    printf("\n\t\t價格:  %.2f",pTemp->price);
    printf("\n\n\t\t-------------------------------\n\n");
    printf("\t\t  *********  1-圖書號\n\n"); 
	printf("\t\t  *********  2-圖書名\n\n"); 
    printf("\t\t  *********  3-作者\n\n"); 
	printf("\t\t  *********  4-出版社\n\n"); 
	printf("\t\t  *********  5-圖書型別號\n\n");
	printf("\t\t  *********  6-出版時間\n\n");
	printf("\t\t  *********  7-價格\n\n");
	printf("\t\t  *********  0-退出\n\n");
	while(1){
	    printf("\t\t  ----------------請選擇要修改的專案:");
	    int choice;
        scanf("%d",&choice);
        switch(choice){
    	    case 1:  printf("\t\t圖書號:  ");
		             scanf("%d",&pTemp->booknum);    break;
    	    case 2:  printf("\t\t圖書名:  "); 
		             fflush(stdin);  gets(pTemp->bookname);    break;
    	    case 3:  printf("\t\t作者:  ");
		             fflush(stdin);  gets(pTemp->author);    break;
    	    case 4:  printf("\t\t圖書類別號碼:  ");
		             scanf("%d",&pTemp->speciesnumber);    break;
	        case 5:  printf("\t\t出版社:  ");
		             fflush(stdin);  gets(pTemp->publishers);    break;
		    case 6:  printf("\t\t出版時間:  ");
		             scanf("%d-%d-%d",&pTemp->publish_time.year,
					                  &pTemp->publish_time.month, 
									  &pTemp->publish_time.day);    break;
		    case 7:  printf("\t\t價格:  ");
		             scanf("%f",&pTemp->price);    break;
		    case 0:  return;
	        default:    break;
	    }
	}
	printf("\n\n\t\t修改完成!按任意鍵退出...\n");
	getch();
} 
/* 刪除圖書資訊 */
void deletebook()
{
	system("cls");
	int number;
	printf("\n\n\t\t*********刪除操作\n");
	printf("\n\t\t圖書號:");
	scanf("%d",&number); 
	Booklist pTemp,pPre;
	pTemp=lHead;
	pPre=pTemp;
	while(pTemp->booknum!=number){
		pPre=pTemp;
		pTemp=pTemp->next;
	}
	printf("\n\t\t******************\n\n");
	printf("\t\t圖書號:  %08ld",pTemp->booknum);
    printf("\n\t\t圖書名:  %s",pTemp->bookname);
    printf("\n\t\t作者:  %s",pTemp->author);
	printf("\n\t\t圖書類別號碼:  %03d",pTemp->speciesnumber);
	printf("\n\t\t出版社:  %s",pTemp->publishers);
	printf("\n\t\t出版時間:  ");
	printf("%04d-%02d-%02d",pTemp->publish_time.year,
		                     pTemp->publish_time.month,
			    	    	 pTemp->publish_time.day);
    printf("\n\t\t價格:  %.2f",pTemp->price);
    printf("\n\n\t\t-------------------------------\n\n");
	printf("\t\t*********  確認刪除?\n\n");
	printf("\n\t\t  *****  1-確認\n");
	printf("\n\t\t  *****  2-取消\n");
	int choice;
	scanf("%d",&choice);
	if(choice==2)    return;
	if(pTemp->booknum==lHead->booknum)   lHead=lHead->next;
	pPre->next=pTemp->next;
	free(pTemp);
	count--;
	printf("\t\t---------  刪除成功!按任意鍵繼續...\n");
	getch();
}
/* 圖書資訊錄入 */
Booklist inputbooks()
{
	Booklist pNew,pEnd,pHead;
	pEnd=NULL;
	pHead=NULL;
	system("cls");
	count=0;
	char ch='Y';
	while(toupper(ch)=='Y'){
		count++;
		pNew=(Booklist)malloc(sizeof(BOOK));
	    printf("\n\t------------第%d本書-----------\n\n",count);
        printf("\t圖書號:  ");
	    scanf("%ld",&pNew->booknum);
		fflush(stdin);
	    printf("\n\t圖書名:  ");
	    gets(pNew->bookname);
	    printf("\n\t作者:  ");
	    gets(pNew->author);
	    printf("\n\t圖書類別號碼:  ");
	    scanf("%d",&pNew->speciesnumber);
		fflush(stdin);
	    printf("\n\t出版社:  ");
	    gets(pNew->publishers);
	    printf("\n\t出版時間:  ");
	    scanf("%d-%d-%d",&pNew->publish_time.year,
		                 &pNew->publish_time.month,
			    		 &pNew->publish_time.day);
        printf("\n\t價格:  ");
	    scanf("%f",&pNew->price);
        printf("\n\n\t-------------------------------\n\n");
		if(count==1){
			pNew->next=pHead;
			pEnd=pNew;
			pHead=pNew;
		}
		else{
		    pNew->next=NULL;
		    pEnd->next=pNew;
		    pEnd=pNew;
		}
		printf("\t是否繼續錄入(Y/N):");
		//do{
			ch=getche();
		//}while(toupper(ch)!=('Y'||'N'));
	}    
	return pHead;
}
/* 圖書資訊讀取 */
void readfile()
{
	FILE *fp;
	fp=fopen("library_list.txt","r");
	if(fp==NULL){
		printf("\n\n\t*******庫存檔案不存在!請建立!\n");
		return;
	}
	fscanf(fp,"%d",&count);
	int index=0;
	Booklist pTemp,pPre;
	pPre=pTemp=(Booklist)malloc(sizeof(BOOK));
	while(index<count){	
	    index++;
	    fscanf(fp,"%ld %s %s %d %s %d-%d-%d %f\n",&pTemp->booknum,pTemp->bookname,pTemp->author,
		                                &pTemp->speciesnumber,pTemp->publishers,
		                                &pTemp->publish_time.year,&pTemp->publish_time.month,
								        &pTemp->publish_time.day,&pTemp->price);
		if(index==1){
			pTemp->next=NULL;
			pPre=pTemp;
			lHead=pPre;
		}
		else{
			pTemp->next=NULL;
			pPre->next=pTemp;
			pPre=pTemp;
		}
    	pTemp=(Booklist)malloc(sizeof(BOOK));
	}
	free(pTemp); 
	fclose(fp);
	printf("\t讀取圖書資訊成功!\n\n");
	printf("\t*************按任意鍵繼續...\n");
	getch();
}
/* 圖書資訊儲存 */
void savefile()
{
	int choice;
	printf("\n\t\t請確認是否儲存?\n");
	printf("\n\t\t  *****  1-確認\n");
	printf("\n\t\t  *****  2-取消\n");
	scanf("%d",&choice);
	if(choice==2)    return;
	FILE *fp;
	Booklist pTemp;
    pTemp=lHead;
	fp=fopen("library_list.txt","w");
	fprintf(fp,"%d\n",count);
	if(fp==NULL){
		printf("不能開啟該檔案! 按任意鍵退出!\n");
		getch();
		exit(1);
	}
    while(pTemp!=NULL){
		fprintf(fp,"%ld %s %s %d %s %d-%d-%d %f\n",pTemp->booknum,pTemp->bookname,pTemp->author,
		                                  pTemp->speciesnumber,pTemp->publishers,
		                                  pTemp->publish_time.year,pTemp->publish_time.month,
										  pTemp->publish_time.day,pTemp->price);
		pTemp=pTemp->next;
	}
	fclose(fp);
	printf("\t圖書資訊儲存成功!\n\n");
	printf("\t**********按任意鍵繼續...\n");
	getch();
}

功能不包括排序。此係統可以作為基礎程式碼進行修改,可以新增排序、密碼等功能,也可以通過新增管理員、學生兩個不同的介面設計成簡單的圖書館借閱管理系統。