C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)銀行模擬
更新時間:2017年08月20日 16:40:30 作者:楊鑫newlfe
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)銀行模擬的相關(guān)資料,通過此文希望大家能理解離散化的方法,希望能幫助到大家,需要的朋友可以參考下
C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)銀行模擬
實現(xiàn)代碼:
#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;
}
由于這里是生成的隨機數(shù),所以程序會一直在變化。按住ctrl +c 終止程序


以上就是C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)銀行模擬的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++:構(gòu)造函數(shù),析構(gòu)函數(shù)詳解
今天小編就為大家分享一篇關(guān)于C++構(gòu)造函數(shù)和析構(gòu)函數(shù)的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2021-09-09
C++ 中靜態(tài)成員函數(shù)與非靜態(tài)成員函數(shù)的區(qū)別
這篇文章主要介紹了C++ 中靜態(tài)成員函數(shù)與非靜態(tài)成員函數(shù)的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-05-05

