用C语言模拟ATM机工作流程编程
发布网友
发布时间:2022-04-23 09:46
我来回答
共1个回答
热心网友
时间:2023-10-10 00:31
#include
"stdio.h"对ATM机器的模拟就是一个对队列的模拟下面代码在VC6环境下调试已经通过了其中有个缺陷就是因为代码执行速度过快导致二次执行根据时间随机出来的数字都是一样的因此你可以自己加上一个延迟子程序部分功能已经注释了#include
"stdlib.h"#include
"time.h"#define
OK
1#define
ERROR
0
typedef
struct
node{
int
number;
struct
node*
next;}*Lnode;
typedef
struct
list{
node
*head,*rear;}*Plist;
//模拟
ATM开业bool
ListInit(Plist
list){
Lnode
p;
p
=
(Lnode)malloc(sizeof(Lnode));
list->head
=
p;
list->rear
=
list->head;
list->head->next
=
NULL;
if(list->head!=NULL)
return
ERROR;
else
return
OK;}
//模拟
有客户排队bool
ListInsert(Plist
list,int
number){
Lnode
p;
p
=
(Lnode)malloc(sizeof(Lnode));
if(p==NULL)
return
ERROR;
else
{
p->number
=
number;
p->next
=
NULL;
list->rear->next
=
p;
list->rear
=
p;
return
OK;
}}
//模拟
客户办完事离开bool
ListDelete(Plist
list){
Lnode
p;
if(list->head
==list->rear)
return
ERROR;
else
{
p
=
list->head->next;
list->head->next
=
p->next;
list->rear
=
list->head;
//
free(p);
return
OK;
}}
void
sand(int*
gettime,int*
needtime){
srand(time(NULL))
;
*gettime
=
rand()%100;
srand(time(NULL))
;
*needtime
=rand()%100;}
//模拟客户到达事件void
CustomerArrived(Plist
list,int
gettime,int
needtime,int
kehu,int
time){
int
nextgettime,nextneedtime;
sand(&nextgettime,&nextneedtime);
while(needtime>0
&&
nextgettime>0
&&
time>0)
{
needtime
--;
nextgettime
--;
time
--;
}
if(nextgettime
==
0
&&
needtime>0
&&time>0)
{
kehu++;
ListInsert(list,kehu);
while(needtime>0
&&
time>0)
{
needtime--;
time
--;
}
ListDelete(list);
CustomerArrived(list,nextgettime,nextneedtime,kehu,time);
}
if(needtime
==0
&&
nextgettime>0
&&
time>0)
{
ListDelete(list);
while(nextgettime>0
&&
time>0)
{
nextgettime--;
time
--;
}
kehu++;
ListInsert(list,kehu);
//未删除
,list未传递进去
CustomerArrived(list,nextgettime,nextneedtime,kehu,time);
}
if(time
==0)
{
printf("ATM关门,请明天在来!\n");
return;
}}
main(){
list
list;
int
i
=
10000;
//ATM机器每天工作时间
int
kehu
=
0;
//客户标号
int
gettime,needtime;
ListInit(&list);
//ATM开业
sand(&gettime,&needtime);
ListInsert(&list,kehu);
CustomerArrived(&list,gettime,needtime,kehu,i);
getchar();
}