📄 bookstore.c
字号:
/*
#include<stdio.h>
#include<math.h>
#include<string.h>
#include <alloc.h>
*/
/*
//以下变量的定义在头文件中
extern press_store[3];
extern final_buyer[2];
*/
//各个书店信息的初始化
void store_beginer()
{
FILE * fp_c;
FILE * fp_d;
FILE * fp_e;
//打开各个书店文件用于存放采购来的书籍
if ((fp_c=fopen("book_c","wb+"))!=NULL)
{
printf("book_c has been builded!\n");
fclose(fp_c);
}
else
{
printf("error: book_c has not been builded!\n");
}
if ((fp_d=fopen("book_d","wb+"))!=NULL)
{
printf("book_d has been builded!\n");
fclose(fp_d);
}
else
{
printf("error: book_d has not been builded!\n");
}
if ((fp_e=fopen("book_e","wb+"))!=NULL)
{
printf("book_e has been builded!\n");
fclose(fp_e);
}
else
{
printf("error: book_e has not been builded!\n");
}
//各个书店订购书目数量的初始化
int i_store;
int i_scri;
for (i_store=0;i_store<3;i_store++)
{
for (i_scri=0;i_scri<7;i_scri++)
{
press_store[i_store].scrimbe_num[i_scri]=200;
press_store[i_store].scrimbe_num[i_scri]=200;
}
}
//C 书店订购种类
press_store[0].kind[0]="science";
press_store[0].kind[1]="foreign";
press_store[0].kind[2]="education";
press_store[0].scrimbe_num[0]=500;
//D书店订购种类
press_store[1].kind[0]="foreign";
press_store[1].kind[1]="science";
press_store[1].kind[2]="society";
press_store[1].kind[3]="education";
press_store[1].scrimbe_num[0]=500;
//E 书店订购种类
press_store[2].kind[0]="science";
press_store[2].kind[1]="foreign";
press_store[2].kind[2]="society";
press_store[2].kind[3]="health";
press_store[2].kind[4]="life";
press_store[2].kind[5]="sports";
press_store[2].kind[6]="education";
}
//书店到出版社采购图书
void store_scrimbe(char store_id,char press_id,int cursor_name,int num)
{
static int cursor_c=0;
static int cursor_d=0;
static int cursor_e=0;
FILE * fp;
FILE * work;
struct book *a_book;
a_book=(struct book *)malloc(sizeof(struct book));
a_book=search_press(cursor_name,store_id,press_id, a_book);
//将采购到的图书放进书库
switch (store_id)
{
case 'C':
{
if ((fp=fopen ("book_c","ab"))!=NULL)
{
if (a_book!=NULL)
{
fwrite(a_book,sizeof(struct book),1,fp);
cursor_c++;
}
else
{
printf("store didnot scrimber a book!!\n");
}
fclose(fp);
}
else
{
printf("C store cannot open book_c in function store_scrimbe\n");
pthread_exit(NULL);
}
if ((work=fopen ("work.txt","a+"))!=NULL)
{
fprintf(work,"%c store has scrimbed %s book from %c press!\n",store_id,a_book->kind,press_id);
fclose(work);
}
break;
}
case 'D':
{
if ((fp=fopen("book_d","ab"))!=NULL)
{
if (a_book!=NULL)
{
fwrite(a_book,sizeof(struct book),1,fp);
cursor_d++;
}
else
{
printf("store didnot scrimber a book!!\n");
}
fclose(fp);
}
else
{
printf("D store cannot open book_d in function store_scrimbe\n");
pthread_exit(NULL);
}
if ((work=fopen ("work.txt","a+"))!=NULL)
{
fprintf(work,"%c store has scrimbed %s book from %c press!\n",store_id,a_book->kind,press_id);
fclose(work);
}
break;
}
case 'E':
{
if ((fp=fopen ("book_e","ab"))!=NULL)
{
if (a_book!=NULL)
{
fwrite(a_book,sizeof(struct book),1,fp);
cursor_e++;
}
else
{
printf("store didnot scrimber a book!!\n");
}
fclose(fp);
}
else
{
printf("E store cannot open book_e in function store_scrimbe\n");
pthread_exit(NULL);
}
if ((work=fopen ("work.txt","a+"))!=NULL)
{
fprintf(work,"%c store has scrimbed %s book from %c press!\n",store_id,a_book->kind,press_id);
fclose(work);
}
break;
}
default :
{
printf("error store_id!!\n");
}
}
free(a_book);
}//end function
//C D书店具体的买书行为
void sell_book(void * cd_store_id)
{
char store_id=*((char *)cd_store_id);
FILE * fp;
struct book vector;
if (store_id=='C')
{
if ((fp=fopen("book_c","rb+"))==NULL)
{
printf("C store cannot open book_c in function sell_book\n");
pthread_exit(NULL);
}
rewind(fp);
while (!feof(fp))
{
fread(&vector,sizeof(struct book),1,fp);
if (strcmp(vector.kind,"science")==0)
{
vector.lib_num-=100;
fseek(fp,-sizeof(struct book),1);
fwrite(&vector,sizeof(struct book),1,fp);
printf("C store has sold 100 science books!\n");
if (vector.lib_num==0)
{
printf("C store science book has been sold out !!\nsell end !go to scrimbe!sorry!!\n");
// store_scrimbe(store_id,vector.press_id,vector.cursor_name,500);
}
}
else
{
vector.lib_num-=20;
fseek(fp,-sizeof(struct book),1);
fwrite(&vector,sizeof(struct book),1,fp);
printf("C store has sold 20 %s books!\n",vector.kind);
if (vector.lib_num==0)
{
printf("C store %s book has been sold out !!!sell end sorry!!!\n",vector.kind);
// store_scrimbe(store_id,vector.press_id,vector.cursor_name,200);
}
}
if(feof(fp))
{
rewind(fp);
if(vector.lib_num==0)
{
return;
}
}
sleep(2);
}
}
else if (store_id=='D')
{
if ((fp=fopen("book_d","rb+"))==NULL)
{
printf("D store cannot open book_d in function sell_book\n");
pthread_exit(NULL);
}
rewind(fp);
while (!feof(fp))
{
fread(&vector,sizeof(struct book),1,fp);
if (strcmp(vector.kind,"foreign")==0)
{
vector.lib_num-=100;
fseek(fp,-sizeof(struct book),1);
fwrite(&vector,sizeof(struct book),1,fp);
printf("D store has sold 20 foreign books!\n");
if (vector.lib_num==0)
{
printf("D store foreign book has been sold out !\n");
// store_scrimbe(store_id,vector.press_id,vector.cursor_name,500);
}
}
else
{
vector.lib_num-=20;
fseek(fp,-sizeof(struct book),1);
fwrite(&vector,sizeof(struct book),1,fp);
printf("D store has sold 20 %s books!!\n",vector.kind);
if (vector.lib_num==0)
{
printf("D store %s book has been sold out !!!\n",vector.kind);
// store_scrimbe(store_id,vector.press_id,vector.cursor_name,200);
}
}
if(feof(fp))
{
rewind(fp);
if(vector.lib_num==0)
{
return;
}
}
sleep(2);
}
}
}
//press published a book then tell store goes to scrimbe!
void tell_store_buy(struct book temp,char store_id)
{
FILE * fp;
//FILE * work;
switch (store_id)
{
case 'C':
{
if((fp=fopen("book_c","ab"))!=NULL)
{
fwrite(&temp,sizeof(struct book),1,fp);
fclose(fp);
printf("C store has been told to scrimbe and it got them\n");
}
else
{
printf("cannot open book_c in function tell_store_buy\n");
}
break;
}
case 'D':
{
if((fp=fopen("book_d","ab"))!=NULL)
{
fwrite(&temp,sizeof(struct book),1,fp);
fclose(fp);
printf("D store has been told to scrimbe and it got them\n");
}
else
{
printf("cannot open book_d in function tell_store_buy\n");
}
break;
}
case 'E':
{
if((fp=fopen("book_e","ab"))!=NULL)
{
fwrite(&temp,sizeof(struct book),1,fp);
fclose(fp);
printf("E store has been told to scrimbe and it got them\n");
}
else
{
printf("cannot open book_e in function tell_store_buy\n");
}
break;
}
default :
{
printf("wrong store_id!!!\n");
}
}//end switch
}// end function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -