"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()
}
如下,实际过程中还需要加个菜单选择,另外要对用户输入进行合法性检查。#include<stdio.h>
int main()
{
int n
char ch
printf("请输入您要取款的金额:")
scanf("%d"散行,&n)
printf("请确认您的取款金额(y 或者n):")
ch = getchar()
if(ch == 'y' &&ch =='Y') //这个是你要的算法
printf("取款%d成功\n",n)
//同样的这里可以凯州加入其它的判断,手机打字不便,自己根据盯掘蔽需要加。
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)