#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*********图书信息结构体*********/
/***包括登录号,书名,作者名,分类号,出版单位,出版时间,价格等***/
struct bookinf
{
	int num; /*******************************************************登录号*/
    char bname[20];/*************************************** 书名*/
    char wname[10];/*************************************** 作者名*/
    char clanum[14];/*************************************** 分类号*/
    char pubcom[10];/*************************************** 出版单位*/
    char pubtime[10];/*************************************** 出版时间*/
    float price;/********************************************* 价格*/
}
	
void index()
{
	{ 
    char input;                                                          /*注释:运行时直接进入这个页面*/
    printf("\n\n\t\t\t*****************************\n");                         
    printf("\t\t\t    图书信息管理系统\n");                                     
    printf("\t\t\t*****************************\n\n\n");
    printf(" ------------------------------------------------------------------------------\n");
    printf("\t<按任意键进入>\n\t");
    input=getchar();    
	}
    FILE *fp;
    struct manage passsword;
	if ((fp=fopen("password.txt","r"))==NULL)  
	{ 
	 printf("\n\n\n\tCannot open file!\n\t");
	}	
	else                                                                         
	{
	 do 
	 {                                                  
	 printf("\n\n\t请输入管理员帐号:");                             
	 scanf("%s",name);                                          
	 printf("\n\n\t请输入管理员密码:");
	 scanf("%s",password);
	 fp=fopen("password.txt","r");   
	 fread(&pswd,sizeof(struct manage),1,fp);
	 fclose(fp);
	 w=strcmp(pswd.manage_name,name); 
	 k=strcmp(pswd.password,password);
	 adm_servelist();
	 }
	 while (w!=0||k!=0);
	 adm_servelist();     
	 }
}
void adm_servelist()
{
 void book_add();
 void book_view();
 void change();
 void del();
 void book_search();
 void login();
 void pwchange();
 int in;                                               /* 注释:菜单列出的服务种类从1,2,3,4,5,6所对应的功能进行选择*/
 char input;                                                 
 do                                                        
 {                                                  
  printf("\n\n\n\t--欢迎使用信息管理系统--");
  printf("\n\n\t1.图书信息录入\n");
  printf("\t2.图书信息浏览\n");
  printf("\t3.图书信息查询\n");
  printf("\t4.图书信息删除\n");
  printf("\t5.图书信息修改\n");
  printf("\t6.退出管理模式\n");
  printf("\n\t请选择服务种类:");
  scanf("%d",&in);
  if (in!=1&&in!=2&&in!=3&&in!=4&&in!=5&&in!=6)
  { 
   printf("\n\t没有这项服务~\n");
   printf("\t按任意键重新选择");
   input=getchar();
  }
 }
 while (in!=1&&in!=2&&in!=3&&in!=4&&in!=5&&in!=6);
 switch (in)
 {
 case 1: book_add();  break;                                    /*注释:选择自己所需要后摁下数字键则则进入此功能页面*/
 case 2: book_view(); break;                                         
 case 3: book_search(); break;                                      
 case 4: del(); break;
 case 5: change();break;
 case 6: login();  break;
 }
}
void book_add()
{
void adm_servelist()
 void book_add();
 int i=0,j;
 FILE *fp;
 char input;                                      /*注释:输入的图书信息是通过文件进行保存的*/
 struct bookinf books;                                   
 struct bookinf book_taxis[30];                            
  printf("\n\n\n\t---请输入图书信息---\n\n");*
  printf("\t登录号:");
  scanf("%d",&books.num);
  printf("\n\t书名:");
  scanf("%s",books.bname);
  printf("\n\t作者名:");
  scanf("%s",books.wname);
  printf("\n\t分类号:");
  scanf("%s",books.clanum);
  printf("\n\t出版单位:");
  scanf("%s",books.pubcom);
  printf("\n\t出版时间:");
  scanf("%s",books.pubtime);
  printf("\n\t价格:");
  scanf("%f",&books.price);
  if ((fp=fopen("book.txt","r"))!=NULL)   
  {
   do
   {
    fread(&book_taxis[i],sizeof(struct bookinf),1,fp);  
    i=i+1;
   }
   while(!feof(fp));
   fclose(fp);
   for (j=0;j<=i;j++)
    if (books.num==book_taxis[j].num)   
    {
     printf("\n\n\n\t对不起,这个编号已经使用过了~\n");
     printf("\t按任意键返回管理菜单~");
     input=getchar();
     adm_servelist();
     break;
    }
  }
 if ((fp=fopen("book.txt","r"))==NULL)    
 {
fp=fopen("book.txt","w");    
 }
 else
 {
  fclose(fp);
  fp=fopen("book.txt","a");
 }
 fwrite(&books,sizeof(struct bookinf),1,fp);   
 fclose(fp);
 printf("\n\n\n\t信息输入完毕~按任意键继续\n");
 input=getchar(); 
 adm_servelist();
}
void book_view()
{
 void adm_servelist();
 void book_view();
 int inp,i=0,n=0;
 FILE *fp;
 char input;
 struct bookinf bookview[20];
 if ((fp=fopen("book.txt","r"))==NULL)   
 {
  printf("\n\n\n\t数据尚未初始化~~\n\n\t请在管理菜单中增加图书信息~~~\n");
  printf("\n\t<按任意键返回管理菜单>\n\t");
  input=getchar();
  adm_servelist();
 }
 else    
 { 
  do
  {
   fread(&bookview[i],sizeof(struct bookinf),1,fp);
   i=i+1;
  }
  while (!feof(fp));
  fclose(fp);    
  do     
   { 
    printf("\n\t--------共有%d条记录--------",i-1);
    printf("\n\n\t登录号:   %d\n",bookview[n].num);
                                  6
printf("\n\t书名:   %s\n",bookview[n].bname);
    printf("\n\t作者名:   %s\n",bookview[n].wname);
    printf("\n\t分类号:   %s\n",bookview[n].clanum);
    printf("\n\t出版单位:   %s\n",bookview[n].pubcom);
    printf("\n\t出版时间:   %s\n",bookview[n].pubtime);
    printf("\n\t价格:   %f\n",bookview[n].price);
    printf("\n\n\t1.上一页\t2.下一页\t3.回目录\n");
    printf("\n\t请选择:");
    scanf("%d",&inp);
    switch (inp)
    {
    case 1:    
     {
      if (n==0)
      { 
       printf("\n\t已到达首页\n\t按任意键重新选择");
       input=getchar();
      }
      else  n=n-1;   break;
     }
    case 2:
     {
      if (n==i-2)
      {
       printf("\n\t已到达最末页\n\t按任意键重新选择");
       input=getchar();
      }
      else n=n+1;   break;
     }
    case 3: 
     {
      adm_servelist();
      break;
     }
    default:	
     {
      printf("\n\t没有这项服务\n\t按任意键重新选择");
      input=getchar();
     }
    }
    
   }
   while (inp!=3);
}
}
void change(int x)
{
 int inp,i=0,j;
 void book_search();
FILE *fp,*fp1;
 char input;
 struct bookinf books,book_taxis[20];
 do
 {    
  printf("\n\n\n\t注意:您确定要修改该记录吗?\n");
  printf("\n\t1.是\t2.否\n");
  printf("\n\t请输入:");
  scanf("%d",&inp);
  if (inp!=1&&inp!=2)
  {
   printf("\n\t对不起~没有这项服务~\n");
   printf("\t按任意键重新选择\n\t");
   input=getchar();
  }
 }
 while (inp!=1&&inp!=2);
 if (inp==2)
  book_search();
  if (inp==1)    
 {
   printf("\n\n\n\t---请输入图书信息---\n\n");
   printf("\t登录号:");
   scanf("%d",&books.num);
   printf("\n\t书名:");
   scanf("%s",books.bname);
   printf("\n\t作者名:");
   scanf("%s",books.wname);
   printf("\n\t分类号:");
   scanf("%s",books.clanum);
   printf("\n\t出版单位:");
   scanf("%s",books.pubcom);
   printf("\n\t出版时间:");
   scanf("%s",books.pubtime);
printf("\n\t价格:");
   scanf("%f",&books.price);
   
   if ((fp1=fopen("book.txt","r"))!=NULL)    
   { 
    do
    {
     fread(&book_taxis[i],sizeof(struct bookinf),1,fp1);  
     i=i+1;
    } 
    while(!feof(fp1));
    fclose(fp1);
   
    for (j=0;j<x;j++)
     if (books.num==book_taxis[j].num) 
     {
      printf("\n\n\n\t对不起,这个编号已经使用过了~\n");
      printf("\t按任意键返回管理菜单~");
       printf("0");
      input=getchar();
      book_search();   
      break;
     }

    for (j=x+1;j<i-2;j++)     
     if (books.num==book_taxis[j].num)
     {
      printf("\n\n\n\t对不起,这个编号已经使用过了~\n");
      printf("1");
      printf("\t按任意键返回管理菜单~");
      input=getchar();
      book_search(); 
     break;
     }
   }
   book_taxis[x]=books;   
   fp=fopen("book.txt","w");
   for (j=0;j<i-1;j++)       
   fwrite(&book_taxis[j],sizeof(struct bookinf),1,fp);
   fclose(fp);
   printf("\n\n\t信息修改完成!按任意键返回");
   input=getchar();
   book_search(); 
}
}
void del(int y)
{
 char input;
 FILE *fp;
 int inp,i=0,j;
 struct bookinf book_taxis[20];
 void book_search();
 void adm_servelist();
 do    
 {
  printf("\n\n\n\t注意:您确定要删除该记录吗?\n");
  printf("\n\t1.是\t2.否\n");
  printf("\n\t请输入:");
  scanf("%d",&inp);
  if (inp!=1&&inp!=2)
  {
   printf("\n\t对不起~没有这项服务~\n");
   printf("\t按任意键重新选择\n\t");
   input=getchar();
  }
 }
 while (inp!=1&&inp!=2);
 if (inp==2)
  book_search();
 if (inp==1)    
 {
   if ((fp=fopen("book.txt","r"))!=NULL) 
   {
    do
    {
     fread(&book_taxis[i],sizeof(struct bookinf),1,fp);
     i=i+1;
    }
    while(!feof(fp));
    fclose(fp);
   }

   if (i>2)   
   {
    fp=fopen("book.txt","w");
 for (j=0;j<y;j++)
      fwrite(&book_taxis[j],sizeof(struct bookinf),1,fp);
     fclose(fp);

    fp=fopen("book.txt","a");
     for (j=y+1;j<i-1;j++)
      fwrite(&book_taxis[j],sizeof(struct bookinf),1,fp);
     fclose(fp);
   }  
   else    
   { 
    remove("book.txt");
   }
  printf("\n\n\t记录已删除!\n\n\t按任意键返回\n\t");
  input=getchar();
  adm_servelist();   
 }
}