?? wangtao.cpp
字號:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#define SIZE 10000
#define SIZE_stu 5000 /*學生數量*/
FILE *fp;
struct Data
{
int year;
int month;
int day;
}; /*定義日期的結構體*/
struct bookstype
{
int number; /*書的編號,從1開始編號*/
struct Data date; /*書的出版日期*/
char name[8]; /*書名*/
char writer[8]; /*作者名*/
char form[4]; /*書的類型,一般用大寫的英文字母(A-Z)表示*/
int lend[2]; /*租借情況*/
struct bookstye *next;
}books[SIZE];
struct reader
{
long stu_nub;
char name[10];
int sum; /*借閱統計*/
}student[SIZE_stu];
char path(); /*進入系統的路徑設置函數*/
void input(); /* 輸入書籍 */
int save(); /* 保存書籍 */
void output(); /* 輸出書籍 */
void seeknumber(); /* 按書號查詢 */
void seekdate(); /* 按日期查詢 */
void seekname(); /* 按書名查詢 */
void seekwriter(); /* 按作者名查詢 */
void seekform(); /* 按書的類型查詢 */
void ordernumber(); /* 按書的編號排序 */
void orderdate(); /* 按書的日期排序 */
void orderform(); /* 按書的類型排序 */
void delenumber(); /* 按書號刪除書籍 */
void delename(); /* 按書名刪除書籍 */
int book_borrowed(); /* 書籍借出 */
void returnbooks(); /* 還書函數 */
int book_borrowed()
{
char bookname[8];
int a;
FILE *fp;
struct reader student[SIZE_stu];
int i=1;
char choice='y';
if((fp=fopen("D:\\計035.txt","a+"))==NULL)
{
printf("Can't open the file!\n");
return(0);
} /*以讀或追加寫的方式打開文件*/
while(choice=='y'||choice=='Y')
{
printf("————————請輸入您的學號:————————\n",i);
scanf("%d",&student[i].stu_nub);
printf("————————請輸入您的姓名:————————\n",i);
scanf("%s",&student[i].name);
printf("——— 請輸入您要借閱的書籍名稱 ———\n",i);
scanf("%s",&bookname);
seekname();
if(bookname==books[i].name&&books[i].lend==1)
{
printf("此書可以借閱!\n");
printf("確定借閱請選擇1,取消請選擇0\n");
scanf("%d",&a);
delename();
fwrite(&student[i],sizeof(struct reader),1,fp);
} /*將student[i]追加入磁盤文件*/
else
printf("對不起,您預借閱的書籍庫存已空!\n");
printf("是否要輸入下一讀者Y/N\n");
scanf("%c",&choice);
i++;
}
fclose(fp);
}
void returnbooks() /*書籍歸還函數*/
{
}
char path()
{
char s[100];
char password[]="1";
/*字符數組password存放正確的口令1*/
int flag=0,n=3;
/*設置口令對錯標志flag,初值為0,n為允許輸入錯誤次數*/
char choice='y';
do
{
printf("請輸入口令:");
gets(s);
if(!(strcmp(s,password)))
{
printf("OK!\n");
flag=1;
break;
}
else
if(n>0)
{
printf("輸入錯誤,請再輸一次!\n");
n--;
}
}while(n>0);
if(!flag)
{
printf("對不起,你不能使用該系統,再見!\n");
exit(0);
}
return 1;
}
void input()
{
FILE *fp;
struct bookstype books[SIZE];
int i=1;
char choice='y';
fp=fopen("D:\\計035.txt","a+");
/*以讀或追加寫的方式打開文件*/
while(choice=='y'||choice=='Y')
{
printf("————請輸入第%d本書的書號,日期,書名,作者,類別以及租借情況:————\n",i);
printf("++++++++ 請輸入第%d本書的書號: ++++++++\n",i);
scanf("%d",&books[i].number);
printf("———— 請輸入第%d本書的日期: ++++++++\n",i);
scanf("%d.%d.%d",&books[i].date.year,&books[i].date.month,&books[i].date.day);
printf("請輸入書名:");
scanf("%s",books[i].name);
printf("請輸入書的作者:");
scanf("%s",books[i].writer);
printf("請輸入書的類別:");
scanf("%s",books[i].form);
printf("書的庫存狀態:");
scanf("%s",books[i].lend);
fwrite(&books[i],sizeof(struct bookstype),1,fp);
/*將books[i]追加入磁盤文件*/
printf("是否要輸入下一本書Y/N\n");
scanf("%c",&choice);
i++;
}
fclose(fp);
}
int save() /*將修改過的文件結果寫到文件里*/
{
FILE *fp;
int i ;
if ((fp=fopen("D:\\計035.txt","w"))==NULL)
{
printf("\n 不能打開輸出文件");
return 0;
}
for (i=0;i<SIZE;i++)
if (fwrite(&books[SIZE],sizeof(struct bookstype),1,fp)!=1)
{
printf("\n寫文件時出錯");
return 0;
}
fclose(fp);
return 1;
}
void output()
{
FILE *fp;
int i,n=0;
struct bookstype books[SIZE];
fp=fopen("D:\\計035.txt","r");
if((fp=fopen("D:\\計035.txt","r"))==NULL)
{
printf("打開文件出錯!\n");
return;
}
else
if(fread(&books[0],sizeof(struct bookstype),1,fp)==0)
{
printf("庫存已空!\n");
return;
}
else
{
printf("顯示所有書籍:\n");
printf("編號 日期 書名 作者名 類型 租借情況\n");
printf("********************************************************************\n");
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
printf("%d ",books[i].number);
printf(" %d.%d.%d\t",books[i].date.year,books[i].date.month,books[i].date.day);
printf("%s ",books[i].name);
printf("%s ",books[i].writer);
printf("%s ",books[i].form);
printf("%s \n",books[i].lend);
n++; /*統計書籍*/
}
printf("********************************************************************\n");
}
fclose(fp);
printf("\n總共有%d本書!\n",n);
}
/*以下為查詢部分*/
void seeknumber()
{
FILE *fp;
struct booksnumber;
int i,flag=0;
int num;
char choice='y';
fp=fopen("D:\\計035.txt","r");
/*以讀的方式打開文件*/
printf("\n請輸入你要查詢的書的書號:");
scanf("%d",&num);
fflush(stdin); /*緩沖*/
while(choice=='y'||choice=='Y')
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(num==books[i].number)
{
printf("找到所要查的書來,具體資料如下:\n");
printf("編號:%2d\n",books[i].number);
printf("日期:%d,%d,%d\n",books[i].date.year,books[i].date.month,books[i].date.day);
printf("書名:%s\n",books[i].name);
printf("作者名:%s\n",books[i].writer);
printf("類型:%s\n",books[i].form);
printf("租借情況:%s<*0為已借走,1為未借*>\n",books[i].lend);
flag=1;
}
}
if(flag==0)
printf("\n沒有發現書號為%d的書\n",num);
printf("是否要繼續查找Y/N");
scanf("%c",&choice);
};
fclose(fp);
}
void seekdate()
{
FILE *fp;
struct booksdate;
int i,flag=0;
int year1,month1,day1;
char choice='y';
fp=fopen("D:\\計035.txt","r");
printf("\n請輸入要查詢的書本的日期:");
scanf("%d.%d.%d",&year1,&month1,&day1);
fflush(stdin);
do
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(year1==books[i].date.year&&month1==books[i].date.month&&books[i].date.day)
{
printf("書本的編號是:%2d\n",books[i].number);
printf("書本的日期是:%d.%d.%d\n",books[i].date.year,books[i].date.month,books[i].date.day);
printf("書本的名字是:%s\n",books[i].name);
printf("書本的作者是:%s\n",books[i].writer);
printf("書本的類型是:%s\n",books[i].form);
printf("書本是否借了:0為借1為未借%-3s:\n",books[i].lend);
flag=1;
}
}
if(flag==0)
printf("\n沒有發現所要找的書!\n");
printf("\n是否要繼續查找Y/N\n");
scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
fclose(fp);
}
void seekname()
{
FILE *fp;
struct booksname;
int i,flag=0;
char choice='y';
char name[10];
fp=fopen("D:\\計035.txt","r");
printf("請輸入要查詢的書本的書名:");
scanf("%s",name);
fflush(stdin);
do
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(strcmp(name,books[i].name)==0)
{
printf("找到要查找的書,具體資料如下:");
printf("編號:%2d\n",books[i].number);
printf("日期:%d.%d.%d\n",books[i].date.year,books[i].date.month,books[i].date.day);
printf("書名:%s\n",books[i].name);
printf("作者名:%s\n",books[i].writer);
printf("類型:%s\n",books[i].form);
printf("租借情況(0為已借出,1為未借出):%s\n",books[i].lend);
fflush(stdin);
flag=1;
}
}
if(flag!=1)
{
printf("找不到書名為%s的書!",name);
}
printf("是否要繼續查找Y/N");
scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
fclose(fp);
}
void seekwriter()
{
FILE *fp;
struct bookswriter;
int i,flag=0;
char writer1[10];
char choice='y';
fp=fopen("D:\\計035.txt","r");
printf("\n請輸入要查詢的書本的作者名:");
scanf("%s",writer1);
fflush(stdin);
do
{
for(i=0;fread(&books[i],sizeof(struct bookstype),1,fp)!=0;i++)
{
if(strcmp(writer1,books[i].writer)==0)
{
printf("找到作者名為%s的書,具體資料如下:\n",writer1);
printf("編號:%2d\n",books[i].number);
printf("日期:%d.%d.%d\n",books[i].date.year,books[i].date.month,books[i].date.day);
printf("書名:%s\n",books[i].name);
printf("作者名:%s\n",books[i].writer);
printf("類型:%s\n",books[i].form);
printf("租借情況(0為已借出,1為未借出):%s\n",books[i].lend);
flag=1;
}
}
if(flag!=1)
{
printf("\n找不到作者名為%s的書!\n",writer1);
}
printf("是否要繼續查找Y/N:");
scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
fclose(fp);
}
void seekform()
{
FILE *fp;
struct bookform;
int i,flag=0;
char form1[4];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -