⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookbuyer.c

📁 书店管理系统
💻 C
字号:
/*
#include<stdio.h>
#include<math.h>
#include<string.h>
#include <alloc.h>
*/
/*
//以下变量定义在头文件中
extern press_store[3];
*/
//E书店注册F和G的信息 
void e_licence()//E for F and G
{
	 
         FILE *fp_f;
         FILE *fp_g;
         if ((fp_f=fopen("book_f","wb+"))==NULL)
         {
            printf("cannot build book_f in function e_licence\n");
         }
         else
         {
            printf("\nF buyer has open book_f to load his books!!\n ") ;
            fclose(fp_f);
         }

         if ((fp_g=fopen("book_g","wb+"))==NULL)
         {
            printf("cannot build book_g in function e_licence\n");
         }
         else
         {
            printf("G buyer has open book_g to load his books!!\n");
            fclose(fp_g);
         }
         
	 final_buyer[0].buyer_id='F';
	 final_buyer[0].kind[0]="science";
	 final_buyer[0].kind[1]="sports";
	 final_buyer[0].kind[2]="life";
	 	 
	 final_buyer[1].buyer_id='G';
         int i_g;
	 for (i_g=0;i_g<7;i_g++)
         {
	    final_buyer[1].kind[i_g]=press_store[2].kind[i_g];
         }
         printf("F and G  has licence in E store!!\n\n");

}

//购买者查询是否有新书
struct book * search_for_buyer(char buyer_id,struct book *a_book)
{
     struct book vector;
     FILE * fp;
    // FILE * work;  

     fp=fopen("book_e","rb+");
     if (fp==NULL)
     {
        printf("F buyer cannot open book_c in function search_for_buyer\n");
        pthread_exit(NULL);
         
     }
 
    
     if (buyer_id=='F')   
     {
        rewind(fp);
        while (!feof(fp))
        {      
           fread(&vector,sizeof(struct book),1,fp);
         
           if ((strcmp(vector.kind,"science")==0)||(strcmp(vector.kind,"sports")==0)||(strcmp(vector.kind,"life")==0))
           {
               
               //有新书则购买
               if (vector.f_y_n=='n')
               {
                  printf("F find new book\n");
                  a_book->press_id=vector.press_id;
                  a_book->kind=vector.kind;
                  a_book->cursor_name=vector.cursor_name;
                  a_book->name_id=vector.name_id;
                  a_book->lib_num=1;
                  a_book->f_y_n='y';
                  a_book->g_y_n=vector.g_y_n;

                  vector.lib_num-=1;
                  vector.f_y_n='y';
                  fseek(fp,-sizeof(struct book),1);
                  fwrite(&vector,sizeof(struct book),1,fp); 
                   printf("F has bought %s :%d\n",vector.kind,1);
                
                  return a_book;

               }
               else
               {
                
                    printf("F has not find new  %s book\n",vector.kind);
            
                 return NULL;
               }
           }
           else
           {
              
               printf("F didnot buy !!\n");
           }
         
                   
        }//end while
     }//end  if
     else if (buyer_id=='G')   
     { 
        rewind(fp);
        while (!feof(fp))
        {      
           fread(&vector,sizeof(struct book),1,fp);
    
           //有新书则购买
           if (vector.g_y_n='n')
           {
                 printf("G find new book!!!\n");
                 int num;
                 num=rand()%100/5;

                 a_book->press_id=vector.press_id;
                 a_book->kind=vector.kind;
                 a_book->cursor_name=vector.cursor_name;
                 a_book->name_id=vector.name_id;
                 a_book->lib_num=num;
                 a_book->g_y_n='y';
                 a_book->f_y_n=vector.f_y_n;
      
                 vector.lib_num-=num;
                 vector.g_y_n='y';
                 if (vector.lib_num==0)
                 {
                   printf("E store %s books sell over so  go to scrimber!!\n");
                   store_scrimbe('E', vector.press_id,vector.cursor_name,200);
                 }

                 fseek(fp,-sizeof(struct book),1);
                 fwrite(&vector,sizeof(struct book),1,fp); 
                 
                  printf("G has bought %s :%d\n",vector.kind,num);
                  
                 return a_book;

           }
           else
           {
             
                  printf("G has not find new  %s book\n",vector.kind);
                  return NULL;
           }
          
       }//end while             
       
    }//end else if 
     
     fclose(fp);
}//end function

//F和G从E书店购买图书
void buy_from_e_store(void * fg_buyer_id)
{
     char buyer_id=*((char *)fg_buyer_id); 
     struct book * a_book;
     static int cursor_f=0;
     static int cursor_g=0;
     FILE * fp;
    // FILE * work;
  
     if (buyer_id=='F')
     {
        if ((fp=fopen("book_f","rb+"))==NULL)
        {
           printf("F buyer cannot open book_f in function buy_from_e_store\n");
           pthread_exit(NULL);
         
        }
        do
        {
              a_book=(struct book *)malloc(sizeof(struct book));
              a_book=search_for_buyer(buyer_id,a_book);
              if (a_book!=NULL) 
              {
                printf("%c has bought %d a new %s book from E store!\n",buyer_id,1,a_book->kind);
                fwrite(a_book,sizeof(struct book),1,fp);
                cursor_f++;
              }
              else 
              {  
                // if ((work=fopen("work.txt","a+"))!=NULL)
                // { 
                   printf("%c has no new book! so %c didn't buy!\n",buyer_id,buyer_id);
                   //fclose(work);
                // }
             }
             free(a_book);
             sleep (1);
        }while(a_book!=NULL);
        fclose(fp);
     }
     else if (buyer_id=='G')
     {
        int seconds=3;

        if ((fp=fopen("book_g","rb+"))==NULL)
        {
          printf("F buyer cannot open book_f in function buy_from_e_store\n");
          pthread_exit(NULL);
         
        }
       /* if ((work=fopen("work.txt","a+"))==NULL)
        { 
            fprintf("cannot open work.txt in sell_book function\n");
            fclose(work);
        }*/
          
        do
        {
             a_book=(struct book *)malloc(sizeof(struct book));
             a_book=search_for_buyer(buyer_id,a_book);
             if (a_book!=NULL) 
             {
               // if (work!=NULL)
               // { 
                   printf("%c has bought %d a new %s book from E store!\n",buyer_id,a_book->lib_num,a_book->kind);
                 // fclose(work);
               // }
             fwrite(a_book,sizeof(struct book),1,fp);
             cursor_g++;
             }
             else 
             {
                 /*if (work!=NULL)
                 { 
                    fprintf(work,"%c has no new book! so %c didn't buy!\n",buyer_id,buyer_id);
                   // fclose(work);
                 }*/
                 printf("G has not find new book he didinot buy!!\n");
              }
             
             // fclose(work);
             
              sleep(seconds);
              free(a_book);
        }while(a_book!=NULL);
        fclose(fp);

     }
   // free(a_book);

}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -