源碼如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define M 1000
- void choice_1_input_new(); /*輸入新進的圖書信息*/
- void choice_2_display_all(); /*顯示全部圖書信息*/
- void choice_3_search_according_name(); /*根據圖書名稱顯示圖書的信息*/
- void choice_4_delete_according_name(); /*根據圖書名稱對圖書信息進行刪除*/
- void choice_9_sort_according_list(); /*根據圖書庫存排序*/
- void choice_5_sort_according_name(); /*根據圖書價格排序*/
- void choice_6_display_high_price(); /*根據價格范圍列出圖書信息*/
- void choice_7_display_according_exitflag(); /*根據存在狀態列出圖書信息*/
- void choice_8_change_book(); /*修改圖書信息*/
- typedef struct bookinfo
- {
- char number[15]; /*產品編號*/
- char name[30]; /*產品名稱*/
- char auther[30]; /*作者*/
- char list[20]; /*類別*/
- float price; /*單價*/
- char boss[15]; /*出版社信息*/
- char time[10]; /*出版時間*/
- int isExit; /*庫存存在狀態*/
- }BOOKINFO;
- void choice_1_input_new() /*輸入新進的圖書信息*/
- {
- char choice;
- FILE *p;
- BOOKINFO newbook;
- system("cls") ;
- while(1)
- {
- printf("輸入圖書編號:");
- scanf("%s",&newbook.number);
- printf("輸入圖書名稱:");
- scanf("%s",&newbook.name);
- printf("輸入圖書類別:");
- scanf("%s",&newbook.list);
- printf("輸入圖書單價:");
- scanf("%f",&newbook.price);
- printf("輸入其出版社:");
- scanf("%s",&newbook.boss);
- printf("輸入圖書作者:");
- scanf("%s",&newbook.auther);
- printf("輸入出版時間:");
- scanf("%s",&newbook.time);
- printf("輸入需要添加的圖書數目:");
- scanf("%d",&newbook.isExit);
- printf("是否保存該條數目?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- {
- p=fopen("e:\\bookinfo.txt","ab");
- fwrite(&newbook,sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n該條書目已添加到e:\\bookinfo.txt 文件中!\n");
- }
- else
- {
- printf("\n該條書目未保存!\n");
- }
- printf("\n是否繼續添加書目?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- void choice_2_display_all() /*顯示全部圖書信息*/
- {
- FILE *p;
- int n;
- BOOKINFO bookinfo[M];
- int booknumber=0;
- system("cls");
- p=fopen("e:\\bookinfo.txt","rb");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- fclose(p);
- booknumber--;
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- n=0;
- printf("圖書信息如下:\n\n\n");
- printf("\t\t\t\t 圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- while(n<booknumber)
- {
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,bookinfo[n].price,
- bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- n++;
- }
- }
- printf("\n\n\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_3_search_according_name() /*根據圖書名稱顯示圖書的信息*/
- {
- char search[20];
- int n;
- FILE *p;
- char choice;
- BOOKINFO bookinfo[M];
- int booknumber=0;
- system("cls");
- p=fopen("e:\\bookinfo.txt","rb");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- while(1)
- {
- printf("輸入要查詢的書本名稱:");
- scanf("%s",&search);
- if(booknumber==0)
- {
- printf("\t\t\t 書庫中沒有任何信息!\n\n\n");
- printf("\t\t\t 按任意鍵返回主菜單!");
- getch();
- break;
- }
- else
- {
- for(n=0;n<booknumber;n++)
- if(strcmp(bookinfo[n].name,search)==0)
- {
- printf("\n\n\t\t\t該書的詳細信息如下:\n\n\n");
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- break;
- }
- if(n>=booknumber)
- printf("\n\n\t\t\t沒有查到該書的任何信息!\n");
- printf("\n\n是否繼續查詢?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- }
- void choice_4_delete_according_name() /*根據圖書名稱對圖書信息進行刪除*/
- {
- char search[20];
- int n,i;
- FILE *p;
- char choice;
- BOOKINFO bookinfo[M];
- int booknumber;
- system("cls");
- while(1)
- {
- printf("輸入要刪除的書本名稱:");
- scanf("%s",&search);
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\t\t\t 書庫中沒有任何信息!\n\n\n");
- printf("\t\t\t 按任意鍵返回主菜單!");
- getch();
- break;
- }
- else
- {
- for(n=0;n<booknumber;n++)
- if(strcmp(bookinfo[n].name,search)==0)
- {
- break;
- }
- if(n>=booknumber)
- printf("\t\t\t 沒有查到該書的任何信息!\n\n");
- else
- {
- printf("是否確認需要刪除該條書目?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- {
- for(i=n;i<booknumber-1;i++)
- bookinfo[i]=bookinfo[i+1];
- booknumber--;
- p=fopen("e:\\bookinfo.txt","wb");
- for(n=0;n<booknumber;n++)
- fwrite(&bookinfo[n],sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n\n\n\t\t\t\t 刪除成功!\n");
- }
- else
- printf("\n\n該條書目沒有被刪除!");
- }
- printf("\n\n是否繼續進行刪除操作?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- }
- void choice_9_sort_according_list() /*根據圖書庫存排序*/
- {
- FILE *p;
- int m,n;
- BOOKINFO temp;
- BOOKINFO bookinfo[M];
- int booknumber;
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- system("cls");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- for(m=0;m<booknumber-1;m++)
- for(n=m+1;n<booknumber;n++)
- if(bookinfo[m].isExit<bookinfo[n].isExit)
- {
- temp=bookinfo[m];
- bookinfo[m]=bookinfo[n];
- bookinfo[n]=temp;
- }
- p=fopen("e:\\bookinfo.txt","wb");
- for(m=0;m<booknumber;m++)
- fwrite(&bookinfo[m],sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n\n完成排序!\n\n");
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- n=0;
- printf("圖書信息如下:\n\n");
- printf("\t\t\t\t 圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- while(n<booknumber)
- {
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- n++;
- }
- }
- }
- fclose(p);
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_5_sort_according_name() /*根據圖書價格排序*/
- {
- FILE *p;
- int m,n;
- BOOKINFO temp;
- BOOKINFO bookinfo[M];
- int booknumber;
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- system("cls");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- for(m=0;m<booknumber-1;m++)
- for(n=m+1;n<booknumber;n++)
- if(bookinfo[m].price>bookinfo[n].price)
- {
- temp=bookinfo[m];
- bookinfo[m]=bookinfo[n];
- bookinfo[n]=temp;
- }
- p=fopen("e:\\bookinfo.txt","wb");
- for(m=0;m<booknumber;m++)
- fwrite(&bookinfo[m],sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n\n完成排序!\n\n");
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- n=0;
- printf("圖書信息如下:\n\n");
- printf("\t\t\t\t 圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- while(n<booknumber)
- {
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- n++;
- }
- }
- }
- fclose(p);
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_6_display_high_price() /*根據價格范圍列出圖書信息*/
- {
- float price;
- FILE *p;
- int n;
- int count=0;
- BOOKINFO bookinfo[M];
- int booknumber;
- system("cls");
- printf("請輸入價格:");
- scanf("%f",&price);
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- for(n=0;n<booknumber;n++)
- {
- if(bookinfo[n].price>=price)
- count++;
- }
- if(count==0)
- printf("書庫中沒有比%.1f$價格高的圖書!",price);
- else
- {
- printf("價格高于¥%.1f的圖書信息如下:\n\n",price);
- printf("\t\t\t\t圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- for(n=0;n<booknumber;n++)
- {
- if(bookinfo[n].price>=price)
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- }
- }
- }
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_7_display_according_exitflag() /*根據存在狀態列出圖書信息*/
- {
- FILE *p;
- int n;
- int count=0;
- BOOKINFO bookinfo[M];
- int booknumber;
- system("cls");
- booknumber=0;
- p=fopen("e:\\bookinfo.txt","rb");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\n\n\n\t\t\t 書庫中沒有任何書目存在!\n\n");
- }
- else
- {
- for(n=0;n<booknumber;n++)
- if(bookinfo[n].isExit!=0)
- count++;
- if(count==0)
- printf("\n\n\n\t\t\t 書庫中的書籍全部借出!\n\n");
- else
- {
- printf("書庫中的圖書信息如下:\n\n");
- printf("\t\t\t\t 圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- for(n=0;n<booknumber;n++)
- if(bookinfo[n].isExit!=0)
- {
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- }
- }
- }
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_8_change_book() /*修改圖書信息*/
- {
- char search[20];
- int n,i;
- FILE *p;
- char choice;
- BOOKINFO newbook;
- BOOKINFO bookinfo[M];
- int booknumber;
- system("cls");
- while(1)
- {
- printf("注意!請謹慎修改,未完成修改的圖書將被視為不合法操作!\a\n\n");
- printf("輸入要修改的書本名稱:");
- scanf("%s",&search);
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\t\t\t 書庫中沒有任何信息!\n\n");
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- break;
- }
- else
- {
- for(n=0;n<booknumber;n++)
- if(strcmp(bookinfo[n].name,search)==0)
- {
- break;
- }
- if(n>=booknumber)
- printf("\n\n\n\t\t\t 沒有查到該書的任何信息!\n");
- else
- {
- printf("是否確認需要修改該條書目?(Y/N):");
- choice=getch();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getch();
- if(choice == 'Y'||choice == 'y')
- {
- for(i=n;i<booknumber-1;i++)
- bookinfo[i]=bookinfo[i+1];
- booknumber--;
- p=fopen("e:\\bookinfo.txt","wb");
- for(n=0;n<booknumber;n++)
- fwrite(&bookinfo[n],sizeof(BOOKINFO),1,p);
- fclose(p);
- {
- printf("\n");
- printf("輸入圖書編號:");
- scanf("%s",newbook.number);
- printf("輸入圖書名稱:");
- scanf("%s",newbook.name);
- printf("輸入圖書類別:");
- scanf("%s",newbook.list);
- printf("輸入圖書單價:");
- scanf("%f",newbook.price);
- printf("輸入其出版社:");
- scanf("%s",newbook.boss);
- printf("輸入圖書作者:");
- scanf("%s",newbook.auther);
- printf("輸入出版時間:");
- scanf("%s",newbook.time);
- printf("\n\n是否保存該條書目信息?(Y/N): ");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- {
- p=fopen("e:\\bookinfo.txt","ab");
- fwrite(&newbook,sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n\n該條書目已添加到 e:\\bookinfo.txt 文件中!\n\n");
- }
- else
- {
- printf("\n該條書目未保存!\n");
- }
- printf("\n\n是否繼續修改書目?(Y/N): ");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- else
- printf("\n\n該條書目沒有被修改!");
- }
- printf("\n\n是否繼續進行修改操作?(Y/N): ");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- }
- void choice_() /*排序*/
- {
- int choice;
- system("cls");
- printf("1: 按價格排序 2:按圖書庫存排序\n");
- printf("請輸入您的選擇:");
- scanf("%d",&choice);
- if(choice==1)
- {
- choice_5_sort_according_name(); /*根據圖書價格排序*/
- }
- else if(choice==2)
- {
- choice_9_sort_according_list(); /*根據圖書庫存排序*/
- }
- else
- {
- printf("您輸入的命令錯誤!請核對后再輸入!\n\a");
- }
- }
- int main()
- {
- system("color 3F");
- system("title 許志強");
- char choice;
- int pass;
- int i=3;
- FILE *p;
- if((p=fopen("e:\\bookinfo.txt","rb"))==NULL)
- {
- p=fopen("e:\\bookinfo.txt","wb");
- fclose(p);
- }
- printf("\n\n\n\n\n");
- printf("\t\t\t歡迎使用呼倫貝爾學院圖書信息管理系統\n\n\n\n\n");
- printf("\t\t\t 請輸入您的學號(0~9): ");
- while(pass!=123456)
- {
- scanf("%d",&pass);
- if(pass!=123456)
- printf("\t\t\t對不起!你輸入的學號有誤,您還有%d次機會......\n\a",--i);
- if(i==0)
- {
- printf("\n\n\n");
- printf("\t\t\t您的學號不存在!請聯系圖書館管理員!\n\a");
- exit(0);
- }
- }
- if(pass==123456)
- {
- printf("\n\n\n\n");
- printf("\t\t\t 學號輸入正確,進入系統中......\n\n\n");
- Sleep(2600);
- }
- getchar();
- system("cls");
- printf("\n\n\n\n\n");
- printf("\t\t\t歡迎進入呼倫貝爾學院圖書信息管理系統\n\n\n\n\n");
- printf("\t\t\t\t 按Enter進入系統\n\n");
- time_t tim; //獲取電腦時間
- struct tm *at;
- char now[80];
- time(&tim);
- at=localtime(&tim); //轉為時間結構。
- strftime(now,79,"\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t 星期%w\n\t\t\t\t\t\t\t\t %H:%M:%S\n\t\t\t\t\t\t\t\t %Y-%m-%d\n",at); //輸出格式
- printf(now);
- getchar();
- system("cls");
- while(1)
- {
- system("cls");
- system("color 3f");
- printf("\n\n");
- printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
- printf("\t※ ※\n");
- printf("\t※ 呼倫貝爾學院歡迎您\t\t ※\n");
- printf("\t※ ※\n");
- printf("\t※ 圖書信息管理系統\t\t\t ※\n");
- printf("\t※ ※\n");
- printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
- printf("\t\t\t * * * * * * * * * * *\n");
- printf("\t\t\t * 1 輸入圖書信息 *\n");
- printf("\t\t\t * 2 顯示全部信息 *\n");
- printf("\t\t\t * 3 根據名稱查詢 *\n");
- printf("\t\t\t * 4 根據名稱刪除 *\n");
- printf("\t\t\t * 5 圖書進行排序 *\n");
- printf("\t\t\t * 6 統計圖書數量 *\n");
- printf("\t\t\t * 7 圖書狀態信息 *\n");
- printf("\t\t\t * 8 修改圖書信息 *\n");
- printf("\t\t\t * 9 退出圖書系統 *\n");
- printf("\t\t\t * * * * * * * * * * *\n\n");
- printf("\t\t\t 請輸入您的選擇:");
- scanf("%d",&choice);
-
- switch(choice)
- {
- case 1: choice_1_input_new(); break; /*輸入新進的圖書信息*/
- case 2: choice_2_display_all(); break; /*顯示全部圖書信息*/
- case 3: choice_3_search_according_name(); break; /*根據圖書名稱顯示圖書的信息*/
- case 4: choice_4_delete_according_name(); break; /*根據圖書名稱對圖書信息進行刪除*/
- case 5: choice_(); break; /*排序*/
- case 6: choice_6_display_high_price(); break; /*根據價格范圍列出圖書信息*/
- case 7: choice_7_display_according_exitflag(); break; /*根據存在狀態列出圖書信息*/
- case 8: choice_8_change_book(); break; /*修改圖書信息*/
- case 9: system("cls"); printf("\n\t\t\t * * * * * * * *\n");
- printf("\t\t\t * *\n");
- printf("\t\t\t * 謝謝使用! *\n");
- printf("\t\t\t * *\n");
- printf("\t\t\t * * * * * * * *\n");
- printf("\n\n\t\t\t 按任意鍵退出!\n");exit(0);
- break; /*退出*/
- default : printf("輸入錯誤\n"); break;
- }
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define M 1000
- void choice_1_input_new(); /*輸入新進的圖書信息*/
- void choice_2_display_all(); /*顯示全部圖書信息*/
- void choice_3_search_according_name(); /*根據圖書名稱顯示圖書的信息*/
- void choice_4_delete_according_name(); /*根據圖書名稱對圖書信息進行刪除*/
- void choice_9_sort_according_list(); /*根據圖書庫存排序*/
- void choice_5_sort_according_name(); /*根據圖書價格排序*/
- void choice_6_display_high_price(); /*根據價格范圍列出圖書信息*/
- void choice_7_display_according_exitflag(); /*根據存在狀態列出圖書信息*/
- void choice_8_change_book(); /*修改圖書信息*/
- typedef struct bookinfo
- {
- char number[15]; /*產品編號*/
- char name[30]; /*產品名稱*/
- char auther[30]; /*作者*/
- char list[20]; /*類別*/
- float price; /*單價*/
- char boss[15]; /*出版社信息*/
- char time[10]; /*出版時間*/
- int isExit; /*庫存存在狀態*/
- }BOOKINFO;
- void choice_1_input_new() /*輸入新進的圖書信息*/
- {
- char choice;
- FILE *p;
- BOOKINFO newbook;
- system("cls") ;
- while(1)
- {
- printf("輸入圖書編號:");
- scanf("%s",&newbook.number);
- printf("輸入圖書名稱:");
- scanf("%s",&newbook.name);
- printf("輸入圖書類別:");
- scanf("%s",&newbook.list);
- printf("輸入圖書單價:");
- scanf("%f",&newbook.price);
- printf("輸入其出版社:");
- scanf("%s",&newbook.boss);
- printf("輸入圖書作者:");
- scanf("%s",&newbook.auther);
- printf("輸入出版時間:");
- scanf("%s",&newbook.time);
- printf("輸入需要添加的圖書數目:");
- scanf("%d",&newbook.isExit);
- printf("是否保存該條數目?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- {
- p=fopen("e:\\bookinfo.txt","ab");
- fwrite(&newbook,sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n該條書目已添加到e:\\bookinfo.txt 文件中!\n");
- }
- else
- {
- printf("\n該條書目未保存!\n");
- }
- printf("\n是否繼續添加書目?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- void choice_2_display_all() /*顯示全部圖書信息*/
- {
- FILE *p;
- int n;
- BOOKINFO bookinfo[M];
- int booknumber=0;
- system("cls");
- p=fopen("e:\\bookinfo.txt","rb");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- fclose(p);
- booknumber--;
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- n=0;
- printf("圖書信息如下:\n\n\n");
- printf("\t\t\t\t 圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- while(n<booknumber)
- {
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,bookinfo[n].price,
- bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- n++;
- }
- }
- printf("\n\n\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_3_search_according_name() /*根據圖書名稱顯示圖書的信息*/
- {
- char search[20];
- int n;
- FILE *p;
- char choice;
- BOOKINFO bookinfo[M];
- int booknumber=0;
- system("cls");
- p=fopen("e:\\bookinfo.txt","rb");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- while(1)
- {
- printf("輸入要查詢的書本名稱:");
- scanf("%s",&search);
- if(booknumber==0)
- {
- printf("\t\t\t 書庫中沒有任何信息!\n\n\n");
- printf("\t\t\t 按任意鍵返回主菜單!");
- getch();
- break;
- }
- else
- {
- for(n=0;n<booknumber;n++)
- if(strcmp(bookinfo[n].name,search)==0)
- {
- printf("\n\n\t\t\t該書的詳細信息如下:\n\n\n");
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- break;
- }
- if(n>=booknumber)
- printf("\n\n\t\t\t沒有查到該書的任何信息!\n");
- printf("\n\n是否繼續查詢?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- }
- void choice_4_delete_according_name() /*根據圖書名稱對圖書信息進行刪除*/
- {
- char search[20];
- int n,i;
- FILE *p;
- char choice;
- BOOKINFO bookinfo[M];
- int booknumber;
- system("cls");
- while(1)
- {
- printf("輸入要刪除的書本名稱:");
- scanf("%s",&search);
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\t\t\t 書庫中沒有任何信息!\n\n\n");
- printf("\t\t\t 按任意鍵返回主菜單!");
- getch();
- break;
- }
- else
- {
- for(n=0;n<booknumber;n++)
- if(strcmp(bookinfo[n].name,search)==0)
- {
- break;
- }
- if(n>=booknumber)
- printf("\t\t\t 沒有查到該書的任何信息!\n\n");
- else
- {
- printf("是否確認需要刪除該條書目?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- {
- for(i=n;i<booknumber-1;i++)
- bookinfo[i]=bookinfo[i+1];
- booknumber--;
- p=fopen("e:\\bookinfo.txt","wb");
- for(n=0;n<booknumber;n++)
- fwrite(&bookinfo[n],sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n\n\n\t\t\t\t 刪除成功!\n");
- }
- else
- printf("\n\n該條書目沒有被刪除!");
- }
- printf("\n\n是否繼續進行刪除操作?(Y/N):");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- }
- void choice_9_sort_according_list() /*根據圖書庫存排序*/
- {
- FILE *p;
- int m,n;
- BOOKINFO temp;
- BOOKINFO bookinfo[M];
- int booknumber;
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- system("cls");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- for(m=0;m<booknumber-1;m++)
- for(n=m+1;n<booknumber;n++)
- if(bookinfo[m].isExit<bookinfo[n].isExit)
- {
- temp=bookinfo[m];
- bookinfo[m]=bookinfo[n];
- bookinfo[n]=temp;
- }
- p=fopen("e:\\bookinfo.txt","wb");
- for(m=0;m<booknumber;m++)
- fwrite(&bookinfo[m],sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n\n完成排序!\n\n");
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- n=0;
- printf("圖書信息如下:\n\n");
- printf("\t\t\t\t 圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- while(n<booknumber)
- {
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- n++;
- }
- }
- }
- fclose(p);
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_5_sort_according_name() /*根據圖書價格排序*/
- {
- FILE *p;
- int m,n;
- BOOKINFO temp;
- BOOKINFO bookinfo[M];
- int booknumber;
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- system("cls");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- for(m=0;m<booknumber-1;m++)
- for(n=m+1;n<booknumber;n++)
- if(bookinfo[m].price>bookinfo[n].price)
- {
- temp=bookinfo[m];
- bookinfo[m]=bookinfo[n];
- bookinfo[n]=temp;
- }
- p=fopen("e:\\bookinfo.txt","wb");
- for(m=0;m<booknumber;m++)
- fwrite(&bookinfo[m],sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n\n完成排序!\n\n");
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- n=0;
- printf("圖書信息如下:\n\n");
- printf("\t\t\t\t 圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- while(n<booknumber)
- {
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- n++;
- }
- }
- }
- fclose(p);
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_6_display_high_price() /*根據價格范圍列出圖書信息*/
- {
- float price;
- FILE *p;
- int n;
- int count=0;
- BOOKINFO bookinfo[M];
- int booknumber;
- system("cls");
- printf("請輸入價格:");
- scanf("%f",&price);
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\n\n\t\t\t 沒有任何圖書信息!\n\n");
- }
- else
- {
- for(n=0;n<booknumber;n++)
- {
- if(bookinfo[n].price>=price)
- count++;
- }
- if(count==0)
- printf("書庫中沒有比%.1f$價格高的圖書!",price);
- else
- {
- printf("價格高于¥%.1f的圖書信息如下:\n\n",price);
- printf("\t\t\t\t圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- for(n=0;n<booknumber;n++)
- {
- if(bookinfo[n].price>=price)
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- }
- }
- }
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_7_display_according_exitflag() /*根據存在狀態列出圖書信息*/
- {
- FILE *p;
- int n;
- int count=0;
- BOOKINFO bookinfo[M];
- int booknumber;
- system("cls");
- booknumber=0;
- p=fopen("e:\\bookinfo.txt","rb");
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\n\n\n\t\t\t 書庫中沒有任何書目存在!\n\n");
- }
- else
- {
- for(n=0;n<booknumber;n++)
- if(bookinfo[n].isExit!=0)
- count++;
- if(count==0)
- printf("\n\n\n\t\t\t 書庫中的書籍全部借出!\n\n");
- else
- {
- printf("書庫中的圖書信息如下:\n\n");
- printf("\t\t\t\t 圖書信息\n\n\n");
- printf("編號 名稱 單價 作者 圖書庫存 出版社 出版時間 圖書類別\n\n");
- for(n=0;n<booknumber;n++)
- if(bookinfo[n].isExit!=0)
- {
- printf("%-6s%-12s%-8.1f%s %-2d %s %s %s\n",bookinfo[n].number,bookinfo[n].name,
- bookinfo[n].price,bookinfo[n].auther,bookinfo[n].isExit,bookinfo[n].boss,bookinfo[n].time,bookinfo[n].list);
- }
- }
- }
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- }
- void choice_8_change_book() /*修改圖書信息*/
- {
- char search[20];
- int n,i;
- FILE *p;
- char choice;
- BOOKINFO newbook;
- BOOKINFO bookinfo[M];
- int booknumber;
- system("cls");
- while(1)
- {
- printf("注意!請謹慎修改,未完成修改的圖書將被視為不合法操作!\a\n\n");
- printf("輸入要修改的書本名稱:");
- scanf("%s",&search);
- p=fopen("e:\\bookinfo.txt","rb");
- booknumber=0;
- while(!feof(p))
- {
- fread(&bookinfo[booknumber],sizeof(BOOKINFO),1,p);
- booknumber++;
- }
- booknumber--;
- fclose(p);
- if(booknumber==0)
- {
- printf("\t\t\t 書庫中沒有任何信息!\n\n");
- printf("\n\t\t\t 按任意鍵返回主菜單!");
- getch();
- break;
- }
- else
- {
- for(n=0;n<booknumber;n++)
- if(strcmp(bookinfo[n].name,search)==0)
- {
- break;
- }
- if(n>=booknumber)
- printf("\n\n\n\t\t\t 沒有查到該書的任何信息!\n");
- else
- {
- printf("是否確認需要修改該條書目?(Y/N):");
- choice=getch();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getch();
- if(choice == 'Y'||choice == 'y')
- {
- for(i=n;i<booknumber-1;i++)
- bookinfo[i]=bookinfo[i+1];
- booknumber--;
- p=fopen("e:\\bookinfo.txt","wb");
- for(n=0;n<booknumber;n++)
- fwrite(&bookinfo[n],sizeof(BOOKINFO),1,p);
- fclose(p);
- {
- printf("\n");
- printf("輸入圖書編號:");
- scanf("%s",newbook.number);
- printf("輸入圖書名稱:");
- scanf("%s",newbook.name);
- printf("輸入圖書類別:");
- scanf("%s",newbook.list);
- printf("輸入圖書單價:");
- scanf("%f",newbook.price);
- printf("輸入其出版社:");
- scanf("%s",newbook.boss);
- printf("輸入圖書作者:");
- scanf("%s",newbook.auther);
- printf("輸入出版時間:");
- scanf("%s",newbook.time);
- printf("\n\n是否保存該條書目信息?(Y/N): ");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- {
- p=fopen("e:\\bookinfo.txt","ab");
- fwrite(&newbook,sizeof(BOOKINFO),1,p);
- fclose(p);
- printf("\n\n該條書目已添加到 e:\\bookinfo.txt 文件中!\n\n");
- }
- else
- {
- printf("\n該條書目未保存!\n");
- }
- printf("\n\n是否繼續修改書目?(Y/N): ");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- else
- printf("\n\n該條書目沒有被修改!");
- }
- printf("\n\n是否繼續進行修改操作?(Y/N): ");
- choice=getchar();
- while(choice != 'Y'&&choice != 'y'&&choice != 'N'&&choice != 'n')
- choice=getchar();
- if(choice == 'Y'||choice == 'y')
- continue;
- else
- break;
- }
- }
- }
- void choice_() /*排序*/
- {
- int choice;
- system("cls");
- printf("1: 按價格排序 2:按圖書庫存排序\n");
- printf("請輸入您的選擇:");
- scanf("%d",&choice);
- if(choice==1)
- {
- choice_5_sort_according_name(); /*根據圖書價格排序*/
- }
- else if(choice==2)
- {
- choice_9_sort_according_list(); /*根據圖書庫存排序*/
- }
- else
- {
- printf("您輸入的命令錯誤!請核對后再輸入!\n\a");
- }
- }
- int main()
- {
- system("color 3F");
- system("title ***");
- char choice;
- int pass;
- int i=3;
- FILE *p;
- if((p=fopen("e:\\bookinfo.txt","rb"))==NULL)
- {
- p=fopen("e:\\bookinfo.txt","wb");
- fclose(p);
- }
- printf("\n\n\n\n\n");
- printf("\t\t\t歡迎使用呼倫貝爾學院圖書信息管理系統\n\n\n\n\n");
- printf("\t\t\t 請輸入您的學號(0~9): ");
- while(pass!=123456)
- {
- scanf("%d",&pass);
- if(pass!=123456)
- printf("\t\t\t對不起!你輸入的學號有誤,您還有%d次機會......\n\a",--i);
- if(i==0)
- {
- printf("\n\n\n");
- printf("\t\t\t您的學號不存在!請聯系圖書館管理員!\n\a");
- exit(0);
- }
- }
- if(pass==123456)
- {
- printf("\n\n\n\n");
- printf("\t\t\t 學號輸入正確,進入系統中......\n\n\n");
- Sleep(2600);
- }
- getchar();
- system("cls");
- printf("\n\n\n\n\n");
- printf("\t\t\t歡迎進入呼倫貝爾學院圖書信息管理系統\n\n\n\n\n");
- printf("\t\t\t\t 按Enter進入系統\n\n");
- time_t tim; //獲取電腦時間
- struct tm *at;
- char now[80];
- time(&tim);
- at=localtime(&tim); //轉為時間結構。
- strftime(now,79,"\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t 星期%w\n\t\t\t\t\t\t\t\t %H:%M:%S\n\t\t\t\t\t\t\t\t %Y-%m-%d\n",at); //輸出格式
- printf(now);
- getchar();
- system("cls");
- while(1)
- {
- system("cls");
- system("color 3f");
- printf("\n\n");
- printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
- printf("\t※ ※\n");
- printf("\t※ 呼倫貝爾學院歡迎您\t\t ※\n");
- printf("\t※ ※\n");
- printf("\t※ 圖書信息管理系統\t\t\t ※\n");
- printf("\t※ ※\n");
- printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
- printf("\t\t\t * * * * * * * * * * *\n");
- printf("\t\t\t * 1 輸入圖書信息 *\n");
- printf("\t\t\t * 2 顯示全部信息 *\n");
- printf("\t\t\t * 3 根據名稱查詢 *\n");
- printf("\t\t\t * 4 根據名稱刪除 *\n");
- printf("\t\t\t * 5 圖書進行排序 *\n");
- printf("\t\t\t * 6 統計圖書數量 *\n");
- printf("\t\t\t * 7 圖書狀態信息 *\n");
- printf("\t\t\t * 8 修改圖書信息 *\n");
- printf("\t\t\t * 9 退出圖書系統 *\n");
- printf("\t\t\t * * * * * * * * * * *\n\n");
- printf("\t\t\t 請輸入您的選擇:");
- scanf("%d",&choice);
-
- switch(choice)
- {
- case 1: choice_1_input_new(); break; /*輸入新進的圖書信息*/
- case 2: choice_2_display_all(); break; /*顯示全部圖書信息*/
- case 3: choice_3_search_according_name(); break; /*根據圖書名稱顯示圖書的信息*/
- case 4: choice_4_delete_according_name(); break; /*根據圖書名稱對圖書信息進行刪除*/
- case 5: choice_(); break; /*排序*/
- case 6: choice_6_display_high_price(); break; /*根據價格范圍列出圖書信息*/
- case 7: choice_7_display_according_exitflag(); break; /*根據存在狀態列出圖書信息*/
- case 8: choice_8_change_book(); break; /*修改圖書信息*/
- case 9: system("cls"); printf("\n\t\t\t * * * * * * * *\n");
- printf("\t\t\t * *\n");
- printf("\t\t\t * 謝謝使用! *\n");
- printf("\t\t\t * *\n");
- printf("\t\t\t * * * * * * * *\n");
- printf("\n\n\t\t\t 按任意鍵退出!\n");exit(0);
- break; /*退出*/
- default : printf("輸入錯誤\n"); break;
- }
- }
- return 0;
- }
復制代碼 |