清华大学数据结构,银行模拟程序
/*
银行业务模拟
*/
#include<opencv2/opencv.hpp>
#include<stdio.h>
#include <malloc.h>
#include <random>
#include <time.h>
#include <stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct {
int OccurTime;//事件发生时刻
int NType;//事件类型,0表示到达事件,1-4表示离开窗口时间;
}Event, ElemType;
typedef struct {
int ArrivalTime;//到达时间
int Duration;//办理业务所需时间
}QElemType;
typedef struct Qnode
{
QElemType data;
struct Qnode* next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front, rear;
}LinkQueue;
typedef struct Lnode
{
ElemType data;
struct Lnode *next;
}Lnode;
typedef struct Lnode* LinkList;
typedef LinkList EventList;
Lnode* ev;
Event en;
LinkQueue q[4];
QElemType customer;
int TotalTime, CustomerNum;
int CloseTime = 100;
int durtime, intertime;
int t;
int cmp(Event a, Event b);
void OpenForDay();
void CustomerArrived();
void CustomerDeparture();
void Blank_Simulation(int CloseTime);
Status InitQueue(LinkQueue &Q);
Status OrderInsert(EventList ev, Event en);
void Random(int &durtime, int &intertime);
Status Minimum(LinkQueue q[4]);
Status EnQueue(LinkQueue& Q, QElemType e);
Status DelQueue(LinkQueue& Q, QElemType& e);
Status QueueLength(LinkQueue Q);
Status QueueEmpty(LinkQueue Q);
Status GetHead(LinkQueue Q, QElemType& e);
int main(void)
{
Blank_Simulation(CloseTime);
return 0;
}
int cmp(Event a, Event b)
{
if (a.OccurTime > b.OccurTime)
return 1;
else if (a.OccurTime < b.OccurTime)
return -1;
else
return 0;
}
//银行业务模拟,统计一天内客户在银行逗留的平均时间
//初始化操作
void OpenForDay()
{
//初始化操作
TotalTime = 0; CustomerNum = 0;//初始化累计时间和客户数为0
ev = (Lnode*)malloc(sizeof(Lnode));
ev->next = NULL;
if (!ev)exit(OVERFLOW);//储存分配失败
en.OccurTime = 0; en.NType = 0;//设定第一个客户到达事件
OrderInsert(ev,en);//插入事件表
for (int i = 0; i < 4; ++i)
InitQueue(q[i]);//空置列表
}
//处理客户离开事件
void CustomerArrived()
{
int i=0;
++CustomerNum;
Random(durtime,intertime);
t = en.OccurTime + intertime;//下一个顾客到达的时间
if (t < CloseTime)
{
en.NType = 0;
en.OccurTime = t;
OrderInsert(ev, en);
}
//i = Minimum(q);
//en.NType = i;
//en.OccurTime = en.OccurTime + durtime;
//找q[i]中最短的点
int length[4] = { 0,0,0,0 };
for (int j = 0; j < 4; j++)
{
QueuePtr q1 = (QueuePtr)malloc(sizeof(QNode));
q1 = q[j].front;
while (q1 !=q[j].rear)
{
q1 = q1->next;
length[j]++;
}
}
int min = length[0];
for (int j = 1; j < 4; j++)
{
if (length[j] < min)
{
min = length[j];
i = j;
}
}
QElemType Qen;
Qen.ArrivalTime = en.OccurTime;
Qen.Duration = durtime;
EnQueue(q[i],Qen);
if (QueueLength(q[i]) == 1)
{
en.NType = i;
en.OccurTime = en.OccurTime + durtime;//离开时间
OrderInsert(ev, en);
}
}
void CustomerDeparture()
{
int i = en.NType;
DelQueue(q[i], customer);//删除第i队列的排头客户
TotalTime += en.OccurTime - customer.ArrivalTime;
if (!QueueEmpty(q[i]))
{
GetHead(q[i],customer);
en.OccurTime = en.OccurTime + customer.Duration;
en.NType = i;
OrderInsert(ev,en);
}
}
void Blank_Simulation(int CloseTime)
{
OpenForDay();
while(ev->next!=NULL)
{
ElemType elem;
Lnode* Lp = (Lnode*)malloc(sizeof(Lnode));
elem = ev->next->data;
Lp = ev->next;
ev->next = ev->next->next;
free(Lp);//删除ev的头元素
en = elem;
if (en.NType == 0)
CustomerArrived();//处理客户到达事件
else
CustomerDeparture();//处理客户离开事件
}
printf("%d\n", TotalTime / CustomerNum);
}
Status InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front)exit(OVERFLOW);
Q.front->next = NULL;
return OK;
}
Status OrderInsert(EventList ev, Event en)
{
Lnode* p1= (Lnode*)malloc(sizeof(Lnode));
Lnode* p2 = (Lnode*)malloc(sizeof(Lnode));
p2->data = en;
p1 = ev;
if (ev->next == NULL)
{
p2->next = ev->next;
ev->next =p2;
}
else if (p1->next->next == NULL)
{
p2->next = ev->next->next;
ev->next->next = p2;
}
else
{
while(!((en.OccurTime > p1->data.OccurTime)&&
(en.OccurTime<= p1->next->data.OccurTime|| p1->next==NULL)))
{
p1 = p1->next;
}
p2->next = p1->next;
p1->next = p2;
}
return 0;
}
//生成两个随机数
void Random(int &durtime,int &intertime)
{
intertime = rand() %CloseTime;
durtime = rand() % 10;
}
Status Minimum(LinkQueue q[4])
{
return 0;
}
Status EnQueue(LinkQueue& Q, QElemType e)
{
//插入元素e为Q的新的队尾元素
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p)
exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
//若队列不空则删除Q的队头元素,用e返回其值,并返回OK
//否则返回ERROR
Status DelQueue(LinkQueue &Q,QElemType &e)
{
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (Q.front == Q.rear)
return ERROR;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return OK;
}
Status QueueLength(LinkQueue Q)
{
int length = 1;
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
p = Q.rear->next;
while (p = NULL)
{
p = p->next;
length++;
}
return length;
}
Status QueueEmpty(LinkQueue Q)
{
if (Q.front->next==NULL)
return OK;
else
return ERROR;
}
Status GetHead(LinkQueue Q,QElemType &e)
{
e = Q.front->data;
return 0;
}
因为是初学者,能力还不足程序有很多不足的地方,需要改进。,希望能够给读者提供灵感,并欢迎大家的反馈。