加入收藏 | 设为首页 | 会员中心 | 我要投稿 鞍山站长网 (https://www.0412zz.com/)- 应用安全、运维、云计算、5G、云通信!
当前位置: 首页 > 创业 > 经验 > 正文

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

发布时间:2020-12-24 10:37:41 所属栏目:经验 来源:网络整理
导读:C语言数据结构实现银行模拟 实现代码: #include stdio.h #include stdlib.h #include windows.h #define MAX_WIN 20 #define MAX_STAY 100 typedef struct customer *link; struct customer { int stay; link next; }; link GUY(int stay,link next) { link

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

实现代码:

#include <stdio.h> 
#include <stdlib.h> 
#include <windows.h> 
#define MAX_WIN 20 
#define MAX_STAY 100 
typedef struct customer *link; 
struct customer 
{ 
  int stay; 
  link next; 
}; 
 
link GUY(int stay,link next) 
{ 
  link c = malloc(sizeof *c); 
  c->stay = stay; 
  c->next = next; 
  return c; 
} 
 
 
link win[MAX_WIN]; 
void morning() 
{ 
  int i; 
  for(i = 0; i < MAX_WIN; i++) 
  { 
    win[i] = NULL; 
  } 
} 
 
 
void come(int w,int stay) 
{ 
  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); 
} 
 
void leave(int w) 
{ 
  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); 
  } 
       
} 
 
 
void guys() 
{ 
  int i; 
  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); 
}   
 
void later() 
{ 
  int i; 
  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); 
  } 
} 
 
 
 
int main() 
{ 
  srand(time(NULL)); 
  for(morning; ;later()) 
  { 
    come(rand()%MAX_WIN,rand()%MAX_STAY+1); 
  } 
  return 0; 
} 

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

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


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

以上就是C语言数据结构实现银行模拟的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(编辑:鞍山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读