发个这两天编的模拟银行开业程序
#include <stdio.h>#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include<windows.h>
unsigned char bank[]={
/*-- 调入了一幅图像:这是您新建的图像 --*/
/*-- 宽度x高度=32x16 --*/
0xF8,0xFF,0xE3,0xFF,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x92,0x24,0x48,0x91,
0x92,0x24,0x48,0x91,0x92,0x24,0x48,0x91,0x92,0x24,0x48,0x91,0x92,0x24,0x48,0x91,
0x92,0x24,0x48,0x91,0x92,0x24,0x48,0x91,0x92,0x24,0x48,0x91,0x92,0x24,0x48,0x91,
0x9E,0x3C,0x78,0xF1,0xF3,0xE7,0xCF,0x9F,0x80,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,
};
char pel[]=" ☆★●□■";
struct zb
{
int x;
int y;
}zuobiao[512]={0};
typedef struct event{
int OccurTime;
int NType;
struct event *next;
}Event;
typedef struct qType{
int ArrivalTime;
int Duration;
clock_t begin;
clock_t end;
struct zb _zuobiao;
struct qType *next;
}QType;
typedef struct{
QType *front;
QType *rear;
int length;
}LinkList;
typedef struct{
Event *front;
Event *rear;
}EventList;
LinkList q[5];
EventList ev;
Event en;
QType customer;
int TotalTime,CustomerNum,durtime,intertime;
float tt=0;
int cmp(Event a,Event b)
{
if(a.OccurTime<b.OccurTime)return -1;
else if(a.OccurTime==b.OccurTime)return 0;
else return 1;
}
bool InitQueue(LinkList &Q)
{
Q.front=Q.rear=(QType*)malloc(sizeof(QType));
if(!Q.front) exit(0);
Q.front->next=NULL;
Q.length=0;
Q.rear->_zuobiao.y=17;
return 1;
}
bool EmptyQueue(LinkList Q)
{
if(Q.front==Q.rear)return 1;
return 0;
}
bool InitEvent(EventList &Q)
{
Q.front=Q.rear=(Event*)malloc(sizeof(Event));
if(!Q.front) exit(0);
Q.front->next=NULL;
return 1;
}
bool EnQueue(LinkList &Q,QType e)
{
QType * p;
p=(QType*)malloc(sizeof(QType));
if(!p) exit(0);
p->ArrivalTime=e.ArrivalTime; p->Duration=e.ArrivalTime;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
Q.length++;
return 1;
}
bool EnEvent(EventList &Q,Event e)
{
Event *p;
p=(Event*)malloc(sizeof(Event));
if(!p) exit(0);
p->OccurTime=e.OccurTime; p->NType=e.NType;
p->next=NULL;
Q.rear=p;
return 1;
}
bool DeEvent(EventList &Q,Event &e)
{
Event *p;
if(Q.front==Q.rear) return 0;
p=Q.front->next;
Q.front->next=p->next;
e.OccurTime=p->OccurTime; e.NType=p->NType;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return 1;
}
bool DeQueue(LinkList &Q,QType &e)
{
QType *p;
if(Q.front==Q.rear) return 0;
p=Q.front->next;
Q.front->next=p->next;
e.ArrivalTime=p->ArrivalTime; e.Duration=p->ArrivalTime;
if(Q.rear==p) Q.rear=Q.front;
free(p);
Q.length--;
return 1;
}
void Random(int &a,int &b)
{
a=rand()%1000+500;
b=rand()%1000+200;
}
int Minimum(LinkList *q)
{
int temp=q[1].length,j=1;
for(int i=1;i<5;i++)
{
if(q[i].length<temp)
{
temp=q[i].length;
j=i;
}
}
return j;
}
void OpenForDay()
{
TotalTime=0;CustomerNum=0;
InitEvent(ev);
en.OccurTime=0; en.NType=0;
EnEvent(ev,en);
for(int i=0;i<=4;i++)InitQueue(q[i]);
}
void countxy(int &x,int i)
{
if(i==1)
{
x=13;
}
if(i==2)
{
x=27;
}
if(i==3)
{
x=41;
}
if(i==4)
{
x=55;
}
}
void color(int b) //颜色函数
{
HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
SetConsoleTextAttribute(hConsole,b) ;
}
void gotoxy(int x,int y)//设置字符显示位置
{
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
COORD loc={x,y};
SetConsoleCursorPosition(hOut,loc);
}
void drawmap()
{
gotoxy(5,5);
color(14);
unsigned char draw;
for(int i=0;i<64;i++)
{
if(i%4==0&&i!=0)
gotoxy(5,5+i/4);
draw=bank[i];
for(int j=0;j<8;j++)
{
zuobiao[i*8+j].x=5+(i%4)*16+j*2;
zuobiao[i*8+j].y=5+i/4;
if((draw&0x80)==0x80)
{
printf("%c%c",pel[10],pel[11]);
}
else
printf("%c%c",pel[8],pel[9]);
draw<<=1;
}
}
gotoxy(0,22);
}
void CustomerArrived(int CloseTime)
{
int i=0,x=17,y=5;
QType e;
CustomerNum++;
Random(durtime,intertime);
gotoxy(x,y);
printf("%c%c",pel[4],pel[5]);
i=Minimum(q);
en.OccurTime=ev.rear->OccurTime+intertime;
en.NType=0;
e.ArrivalTime=en.OccurTime;
e.Duration=durtime;
e._zuobiao.y=16-q[i].length;
e.begin=clock();
countxy(x,i);y=e._zuobiao.y;
countxy(e._zuobiao.x,i);
if(en.OccurTime<CloseTime&&q[i].length<=9)
{
EnEvent(ev,en);
EnQueue(q[i],e);
gotoxy(x,y);
printf("%c%c",pel[4],pel[5]);
}
gotoxy(0,22);
}
void CustomerDeparture()
{
int i=rand()%4+1,x;
countxy(x,i);
if(!EmptyQueue(q[i]))
{
gotoxy(x,17-q[i].length);
printf("%c%c",pel[8],pel[9]);
x+=2;
DeQueue(q[i],customer);
TotalTime+=customer.Duration;
for(int j=0;j<9;j++)
{
gotoxy(x,16-j);
printf("%c%c",pel[4],pel[5]);
Sleep(60);
gotoxy(x,16-j);
printf("%c%c",pel[8],pel[9]);
}
customer.end=clock();
tt+=(float)(customer.end-customer.begin)/CLOCKS_PER_SEC;
}
gotoxy(0,22);
}
void Bank_Simulation()
{
int CloseTime=60000,m,n;
clock_t begin,end;
OpenForDay();
begin=clock();
while(en.OccurTime<CloseTime)
{
m=rand()%3;
n=rand()%4;
for(int i=0;i<=n;i++)
{
CustomerArrived(CloseTime);
Sleep(100);
}
for(int i=0;i<=m;i++)
{
CustomerDeparture();
Sleep(100);
}
}
end=clock();
printf("银行模拟开业总共%.0f秒,顾客总共%d名,顾客的总服务时间为%.0f秒,平均每个顾客的服务时间为%.0f秒",(float)(end-begin)/CLOCKS_PER_SEC,CustomerNum,tt,tt/CustomerNum);
}
main()
{
system("mode con cols=80 lines=30");
srand(time(0));
drawmap();
Bank_Simulation();
}