📄 press.c
字号:
/*
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>
*/
/*
//以下变量的定义均在头文件中
extern press_store[3];
extern press_a_book[6];
extern press_b_book[6];
extern cursor_a;
extern cursor_b;
*/
/*
//出版社出版书的延迟时间
struct timespec delay_2;
struct timespec delay_5;
delay_2.tv_sec=2;
delay_2.tv_nsec=0;
delay_5.tv_sec=5;
delay_5.tv_nsec=0;
*/
//A B 出版社初始化
void press_begin()
{
FILE *fp_a;
FILE *fp_b;
FILE * work;
if ((fp_a=fopen("book_a","wb+"))!=NULL)
{
printf("book_a has been builded!\n");
fclose(fp_a);
}
else
{
printf("error: book_a has not been builded!\n");
}
if ((fp_b=fopen("book_b","wb+"))!=NULL)
{
printf("book_b has been builded!\n");
fclose(fp_b);
}
else
{
printf("error: book_b has not been builded!\n");
}
if ((work=fopen("work.txt","w+"))!=NULL)
{
printf("work.txt has been builded!\n");
fclose(work);
}
else
{
printf("error:work.txt has not been builded!\n");
}
//A 出版社出版书的种类
press_a_book[0].kind="science";
press_a_book[1].kind="foreign";
press_a_book[2].kind="society";
press_a_book[3].kind="health";
press_a_book[4].kind="life";
press_a_book[5].kind="sports";
//B 出版社出版书的种类
press_b_book[0].kind="science";
press_b_book[1].kind="society";
press_b_book[2].kind="health";
press_b_book[3].kind="life";
press_b_book[4].kind="sports";
press_b_book[5].kind="education";
printf("press_begin over!!\n\n");
}
//出版社注册书店C D E
void license(char store_id)
{
static int k=0;
press_store[k].store_id=store_id;
press_store[k].store_id=store_id;
printf("%c licence success in A and B press!!\n",store_id);
k++;
}
//出版社出版新书
void press_new(void * ab_press_id)
{
char press_id=*((char *)ab_press_id);
FILE *fp;
FILE *work;
work=fopen("work.txt","a+");
struct book new_book;
if(press_id=='A')
{
fp=fopen("book_a","ab");
if (fp==NULL)
{
printf("A press cannot open book_a.txt in function press_new\n");
pthread_exit(NULL);
}
if (work==NULL)
{
printf("A press cannot open work.txt in function press_new\n");
}
while (cursor_a<100)
{
new_book.press_id=press_id;
new_book.kind=press_a_book[cursor_a%6].kind;
new_book.name_id=(unsigned long)rand();
new_book.cursor_name=cursor_a;
new_book.lib_num=1000;
new_book.f_y_n='n';
new_book.g_y_n='n';
fwrite(&new_book,sizeof(struct book),1,fp);
printf("A press published a book:\n");
printf("%c %s %d %d %d\n",new_book.press_id,new_book.kind,new_book.name_id,new_book.cursor_name,new_book.lib_num);
/*
if (work!=NULL)
{
fprintf(work,"%c press has published a book:\n",press_id);
fprintf(work,"%c----%s----%lu----%d\n",new_book.cursor_name, new_book.press_id,new_book.kind,new_book.name_id,new_book.lib_num);
// fclose(work);
}
*/
if (strcmp(new_book.kind,press_store[0].kind[0])==0||strcmp(new_book.kind,press_store[0].kind[1])==0||strcmp(new_book.kind,press_store[0].kind[2])==0)
{
tell_store_buy(new_book,'C');
}
if (strcmp(new_book.kind,press_store[1].kind[0])==0||strcmp(new_book.kind,press_store[1].kind[1])==0||strcmp(new_book.kind,press_store[1].kind[2])==0||strcmp(new_book.kind,press_store[1].kind[3])==0)
{
tell_store_buy(new_book,'D');
}
tell_store_buy(new_book,'E');
cursor_a++;
if (cursor_a<11)
{
// pthread_delay_np(&delay_2);
sleep(2);
}
else
{
// pthread_delay_np(&delay_5);
sleep(5);
}
}//end while
fclose(fp);
fclose(work);
}// end if A
else if (press_id=='B')
{
fp=fopen("book_b","ab");
if (fp==NULL)
{
printf("B press cannot open book_b.txt in function press_new\n");
pthread_exit(NULL);
}
if (work==NULL)
{
printf(" B press cannot open work.txt in function press_new\n");
}
while(cursor_b<100)
{
new_book.press_id=press_id;
new_book.kind=press_b_book[cursor_b%6].kind;
new_book.name_id=(unsigned long)rand();
new_book.cursor_name=cursor_b;
new_book.lib_num=1000;
new_book.f_y_n='n';
new_book.g_y_n='n';
fwrite(&new_book,sizeof(struct book),1,fp);
printf("B press published a book:\n");
printf("%c %s %d %d %d\n",new_book.press_id,new_book.kind,new_book.name_id,new_book.cursor_name,new_book.lib_num);
/*
if (work!=NULL)
{
fprintf(work,"%c press has published a book:\n",press_id);
fprintf(work,"%c----%s----%lu----%d\n",new_book.cursor_name, new_book.press_id,new_book.kind,new_book.name_id,new_book.lib_num);
// fclose(work);
}
*/
if (strcmp(new_book.kind,press_store[0].kind[0])==0||strcmp(new_book.kind,press_store[0].kind[1])==0||strcmp(new_book.kind,press_store[0].kind[2])==0)
{
tell_store_buy(new_book,'C');
}
if (strcmp(new_book.kind,press_store[1].kind[0])==0||strcmp(new_book.kind,press_store[1].kind[1])==0||strcmp(new_book.kind,press_store[1].kind[2])==0||strcmp(new_book.kind,press_store[1].kind[3])==0)
{
tell_store_buy(new_book,'D');
}
tell_store_buy(new_book,'E');
cursor_b++;
if (cursor_b<11)
{
//pthread_delay_np(&delay_2);
sleep(2);
}
else
{
// pthread_delay_np(&delay_5);
sleep(5);
}
}//end while
fclose(fp);
fclose(work);
}//end else B
}//end press_new
//出版社留给书店的查询接口用于书店的主动订购
struct book* search_press(int cursor_name,char store_id,char press_id,struct book* a_book)
{
long position=cursor_name * sizeof(struct book);
struct book temp;
int i=0;
FILE * fp;
FILE * work;
work=fopen("work.txt","a+");
if (work!=NULL)
{
fprintf(work,"%c store begin search in %c press before scrimber!\n",store_id,press_id);
}
else
{
printf("cannot open work.txt in function search_press\n");
}
if (press_id=='A')
{
fp=fopen("book_a","ab+");
if (fp==NULL)
{
printf("%c store cannot open book_a.txt in function search_press\n",store_id);
pthread_exit(NULL);
}
fseek(fp,position,0);
fread(&temp,sizeof(struct book),1,fp);
switch (store_id)
{
case 'C':
{
for (i=0;i<3;i++)
{
if (strcmp(temp.kind,press_store[0].kind[i])==0)
{
break;
}
}
if (i==3)
{
printf("error in C!\n");
return NULL;
}
printf("the %d name_id has %d lib_num\n",cursor_name,temp.lib_num);
//查询有书则购买
if (temp.lib_num>0)
{
a_book=scrimber_press(press_id, store_id,cursor_name, press_store[0].scrimbe_num[i],a_book);
}
else
{
if (work!=NULL)
{
fprintf(work,"%c did not scrimbe book in press %c\n",store_id,press_id);
fclose(work);
}
return NULL;
}
break;
}
case 'D':
{
for (i=0;i<4;i++)
{
if (strcmp(temp.kind,press_store[1].kind[i])==0)
{
break;
}
}
if (i==4)
{
printf("error in D!\n");
return NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -