一聚教程网:一个值得你收藏的教程网站

热门教程

C语言数据结构实现银行模拟的教程

时间:2022-06-25 04:35:14 编辑:袖梨 来源:一聚教程网

实现代码:

#include 
#include 
#include 
#define MAX_WIN 20
#define MAX_STAY 100
typedefstructcustomer *link;
structcustomer
{
  intstay;
  link next;
};
  
link GUY(intstay, link next)
{
  link c =malloc(sizeof*c);
  c->stay = stay;
  c->next = next;
  returnc;
}
  
  
link win[MAX_WIN];
voidmorning()
{
  inti;
  for(i = 0; i < MAX_WIN; i++)
  {
    win[i] = NULL;
  }
}
  
  
voidcome(intw,intstay)
{
  if(win[w] == NULL)
  {
        win[w] = GUY(stay, NULL);
        win[w]->next = win[w];
  }
  else
      win[w] = win[w]->next = GUY(stay, win[w]->next);
}
  
voidleave(intw)
{
  if(win[w]->next == win[w])
  {
    free(win[w]);
    win[w] = NULL;
  }
  else
  {
    link t = win[w]->next;
    win[w]->next = t->next;
    free(t);
  }
        
}
  
  
voidguys()
{
  inti;
  link t;
  system("clear");
  for(i = 0; i < MAX_WIN; i++,puts(" "))
  {
      printf("WIN%3d:_", i);
      if((t = win[i]) == NULL)
          continue;
      for(; t->next != win[i]; t = t->next)
      {
        printf("%4d", t->next->stay);
      }
  }
  Sleep(1);
}  
  
voidlater()
{
  inti;
  for(guys(), i = 0; i < MAX_WIN; i++)
  {  
    if(win[i] == NULL)
        continue;
    if(win[i]->next->stay > 0)
        (win[i]->next->stay)--;
    else
        leave(i);
  }
}
  
  
  
intmain()
{
  srand(time(NULL));
  for(morning; ;later())
  {
    come(rand()%MAX_WIN,rand()%MAX_STAY+1);
  }
  return0;
}

由于这里是生成的随机数,所以程序会一直在变化。按住ctrl +c 终止程序

热门栏目