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

📄 publisher.c

📁 多线程的出版商和用户的过程的模拟
💻 C
字号:


#include"Source.h"

unsigned long PubID=1;
unsigned long BookID=1;









//----------------------function complete begin--------------------------------
int RePubNum(int n)
{
	if(n<=500)  
		return 500;
	else
	    return (n/1000+1)*1000;
}

TPublisher *PublisherCreate(char *name)
{
	TPublisher *temp=NULL;
	int i;
	temp=malloc(sizeof(TPublisher));
	if(temp==NULL)
	{
		printf("create publisher fail!\n");
		return NULL;
	}
	temp->PublisherID=PubID++;
	temp->OrderList=NULL;
	strcpy(temp->PublisherName,name);
	for(i=0; i<7; i++)
	 temp->BookList[i]=NULL;
	temp->registedshop=NULL;
	temp->bill=NULL;
	return temp;
}

int AddNewBook(TPublisher *publisher,char *path)
{
    TBook *temp=NULL;
	FILE *file;
	
	BookClass type;
	file=fopen(path,"r");
	
	if(file==NULL )
	{
		printf("open file error!\n");
		return 0;
	}
	while(!feof(file))
	{
     temp=malloc(sizeof(TBook));
     if(temp==NULL)
	{
		printf("create book fail!\n");
		return 0;
	}
	fscanf(file,"%s",temp->BookName);
	fscanf(file,"%d",&type);

   
    temp->BookID=BookID++;
	
	strcpy(temp->PublisherName,publisher->PublisherName);
	temp->Num=1000;
	temp->type=NewPub;
	
    temp->next=publisher->BookList[type];
    publisher->BookList[type]=temp;
	}
	fclose(file);
	
	return 1;
}

void RePublishBook(TPublisher *publisher)
{
   TBook *bk=NULL;
   TOrderFromPub *order=NULL;
   
   order=publisher->OrderList;
   
   
  if(order!=NULL)
   {
	 
     bk=publisher->BookList[order->type];
     while(bk!=NULL)
	 {
       if(strcmp(bk->BookName,order->BookName)==0)
	   {
          bk->type=RePub;
		  bk->Num+=RePubNum(order->number);
		  break;
	   }

	    bk=bk->next;
	 }
     publisher->OrderList=order->next;
     
     
	 fprintf(work,"%s republish book %-15s %d\n",publisher->PublisherName,order->BookName,RePubNum(order->number));
	  printf("%s republish book %-15s %d\n",publisher->PublisherName,order->BookName,RePubNum(order->number));
	 
	 
	 order->CallBack(publisher,order->type,order->number,order->bp,order->ptr);
     
	 free(order);
	 order=NULL;
     return ;
   }
  
}





int FreePublisher(TPublisher *p)
{
  TBook *book=NULL;
  TOrderFromPub *orderlist=NULL;
  TBookShopInFo *registedshop=NULL;
  TBill_Pub *bill=NULL;
  int i;

  for(i=0; i<7; i++)
  {
    book=p->BookList[i];
	while(book!=NULL)
	{
		p->BookList[i]=book->next;
		free(book);
		book=p->BookList[i];
	}
  }
  orderlist=p->OrderList;
  while(orderlist!=NULL)
  {
	  p->OrderList=orderlist->next;
	  free(orderlist);
	  orderlist=p->OrderList;
  }
  registedshop=p->registedshop;
  while(registedshop!=NULL)
  {
	  p->registedshop=registedshop->next;
	  free(registedshop);
	  registedshop=p->registedshop;
  }
  bill=p->bill;
  while(bill!=NULL)
  {
	  p->bill=bill->next;
	  free(bill);
	  bill=p->bill;
  }
  free(p);
  p=NULL;
  return 1;
}


int Register(TPublisher *p,TBookShop *s)
{
	TBookShopInFo *bkspinfo=NULL;
	
	int i;
	if(p==NULL || s==NULL) return 0;
	
    bkspinfo=malloc(sizeof(TBookShopInFo));
	if(bkspinfo==NULL)
	{
		printf("registe bookshop fail!\n");
			return 0;
	}
	for(i=0; i<7; i++)
	  bkspinfo->type[i]=0;
	bkspinfo->BookShopID=s->BookShopID;
	bkspinfo->callback=OrderFromPub;
	bkspinfo->ptr=s;
	/*if(strcmp(s->BookShopName,"C")==0)
	{
		bkspinfo->type[0]=500;
		bkspinfo->type[1]=200;
		bkspinfo->type[6]=200;
	}
	else
	if(strcmp(s->BookShopName,"D")==0)
	{
		bkspinfo->type[0]=200;
		bkspinfo->type[1]=500;
		bkspinfo->type[2]=200;
		bkspinfo->type[6]=200;
	}
    else
	{
		for(i=0; i<7; i++)
			bkspinfo->type[i]=200;
	}*/
    for(i=0; i<7; i++)
     bkspinfo->type[i]=s->typenum[i];
    bkspinfo->next=p->registedshop;
	p->registedshop=bkspinfo;
	fprintf(work,"bookshop %s register to publisher %s\n",s->BookShopName,p->PublisherName);
	
	return 1;
}







//----------------------function complete end----------------------------------

⌨️ 快捷键说明

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