银行模拟 严蔚敏教材C语言
typedef struct
{
int type;
int time;
}Event;
typedef struct LNode
{
Event data;
LNode *next;
}LNode, *ListPtr;
typedef struct
{
ListPtr head;
}LinkList;
Status MakeNode( ListPtr &p, Event e )
{
p=( ListPtr )malloc( sizeof( LNode ) );
if( !p ) return ERROR;
p->data=e;
p->next=null;
return OK;
}
Status InitList( LinkList &L )
{
L.head=( ListPtr )malloc( sizeof( LNode ) );
if( !L.head ) return ERROR;
L.head->next=null;
return OK;
}
Status OrderInsert( LinkList &L, Event e )
{
MakeNode( p, e );
q=L.head->next;
while( q!=null && ( q.data ).time< ( p.data ).time )
{
pre=q;
q=q->next;
}
p->next=q;
pre->next=p;
return OK;
}
Status DelFirst( LinkList &L, Event &e )
{
p=L.head->next;
if( p==null ) return ERROR;
L.head->next=p->next;
e=p->data;
free( p );
return OK;
}
typedef struct
{
int ArriveTime;
int ServiceTime;
}Customer;
typedef struct QNode
{
Customer data;
QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
int length;
}LinkQueue;
Status MakeQNode( QueuePtr &p, Customer e )
{
p=( QueuePtr )malloc( sizeof( QNode ) );
if( !p ) return ERROR;
p->data=e;
p->next=null;
return OK;
}
Status InitQueue( LinkQueue &Q)
{
Q.front=Q.rear=( QueuePtr )malloc( sizeof( QNode ) );
if( !Q.front ) return ERROR;
Q.front->next=null;
Q.length=0;
return OK;
}
Stutas EnQueue( LinkQueue &Q, Customer e )
{
MakeQNode( p, e );
Q.rear->next=p;
Q.rear=p;
Q.length++;
return OK;
}
Status DeQueue( LinkQueue &Q, Customer &e )
{
if( Q.length==0 ) return ERROR;
p=Q.front->next;
Q.front->next=p->next;
Q.length--;
e=p->data;
if( p==Q.rear ) Q.rear=Q.front;
free( p );
return ok;
}
Status GetHead( LinkQueue &Q, Customer &e )
{
if( Q.lengeh==0 ) return ERROR;
p=Q.front->next;
e=p->data;
return OK;
}
int MinQueue( LinkQueue q )
{
min=q[1].length;
num=1;
for( i=2; i<=4; i++ )
{
size=q[ i ].length;
if( min >size )
{
min=size;
num=i;
}
}
return num;
}
Status OpenForDay()
{
TotalTime=0;
CustomerNum=0;
event.time=0;
event.type=0;
InitList( eventlist );
OrderInsert( eventlist, event );
for( i=1; i<=4; i++ )
{
InitQueue( q[i] );
}
return OK;
}
Status CustomerArrived()
{
++CustomerNum;
Random( servicetime, intertime );
i=MinQueue( q );
EnQueue( q[i], ( event.time, servicetime ) );
if( q[i].length==1 )
{
OrderInsert( eventlist, ( event.time + servicetime, i ) );
}
t=event.time + intertime;
if( t< CloseTime )
{
OrderInsert( eventlist, ( t, 0 ) );
}
return OK;
}
Status CustomerDepature()
{
i=event.type;
DeQueue( q[i], customer );
TatolTime += event.time - customer.ArriveTime;
if( q[i].length !=0 )
{
GetHead( q[i], next_customer );
OrderInsert( eventlist, ( event.time + next_customer.ServiceTime, i ) );
}
return ERROR;
}
Event event;
LinkList evenlist;
Customer customer;
LinkQueue q[5];
int TatolTime, CustomerNum; /* 全局变量 */
int main()
{
OpenForDay();
while( eventlist.head->next != null )
{
DelFirst( eventlist, event );
if( event.type==0 )
{
CustomerArrived();
}
else
{
CustomerDepature();
}
}
printf( " Average time: %f ", ( float )TotalTime/CustomerNum );
return 0;
}