[数据结构-严蔚敏版]P65离散事件模拟(银行客户的离散事件驱动模拟程序)

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[数据结构-严蔚敏版]P65离散事件模拟(银行客户的离散事件驱动模拟程序)相关的知识,希望对你有一定的参考价值。

写这个简单玩意,居然花费了我6小时+,唉!!!,还是太菜了!
中间已经起了放弃的念头了,最后还是坚持下来了!

总结:
(1)漏了p = p->next

(2)队列删除元素的时候,删除的是最后一个忘记特判。


(3)写的时候太急了,忘记给pn赋值,导致一执行这个函数,我的表就断了。而且要注意判断删除的元素不存在怎么办(!e->next)


(4)这些特判最好写写,增强代码的健壮性。而且不写可能会报错。

代码如下:

#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

typedef struct
{
	int occurTime;
	int nType;
}Event, ElemType;

typedef struct
{
	int arrivalTime;
	int duration;
}QElemType;


typedef struct QNode
{
	QElemType data;
	QNode *next;
};

typedef struct
{
	QNode *front, *rear;
	int length;
}Queue;


typedef struct ENode
{
	Event data;
	ENode *next;
}ENode, *EventList, *Enode;

int totalTime = 0;
int customerNUM = 0;

bool initList(EventList &eL)
{
	eL = new ENode;
	if (!eL) return false;
	eL->next = nullptr;
	return true;
}

bool initQueue(Queue &q)
{
	q.front = q.rear = new QNode;
	if (!q.front) return false;
	q.front->next = nullptr;
	q.length = 0;
	return true;
}

int cmp(Event a, Event b) {
	if (a.occurTime < b.occurTime) return -1;
	else if (a.occurTime == b.occurTime) return 0;
	else if (a.occurTime > b.occurTime) return 1;
}

bool orderInsert(EventList &el, Event e, int(*compare)(Event, Event))
{
	ENode *p = el;
	if (!p->next)
	{
		//插入的元素为第一个元素的情况
		ENode *s = new ENode;
		if (!s) return false;
		s->data = e;
		s->next = p->next;
		p->next = s;
		return true;
	}
	p = p->next;
	while (p)
	{
		if (compare(p->data, e) <= 0)
		{
			ENode *s = new ENode;
			if (!s) return false;
			s->data = e;
			s->next = p->next;
			p->next = s;
			return true;
		}
	}
	return true;
}


bool listEmpty(EventList el)
{
	ENode *p = el->next;
	if (!p) return true;
	return false;
}

Event getCurElem(ENode *e)
{
	if (e)
		return e->data;
}

bool enQueue(Queue &q, QElemType e)
{
	QNode *s = new QNode;
	if (!s) return false;
	s->data = e;
	s->next = nullptr;
	q.rear->next = s;
	q.rear = s;
	q.length++;
	return true;
}


bool delQueue(Queue &q, QElemType &e)
{
	if (q.front == q.rear) return false;
	QNode *p = q.front->next;
	e = p->data;
	q.front->next = p->next;
	if (p == q.rear) q.rear = q.front;
	delete p;
	q.length--;
	return true;
}


int minimumQueue(Queue q[])
{
	int j = 0;
	int maxNum = 999999;
	for (int i = 1; i <= 4; i++)
	{
		int len = q[i].length;
		if (len < maxNum)
		{
			j = i;
			maxNum = len;
		}
	}
	return j;
}


int queueLength(Queue q)
{
	return q.length;
}

bool queueEmpty(Queue q)
{
	if (q.front == q.rear) return true;
	return false;
}

bool getHead(Queue q, QElemType &customer)
{
	if (!q.front->next) return false;
	customer = q.front->next->data;
	return true;
}

void Random(int &a, int &b)
{
	b = 1 + rand() % 10;
	a = b + 2;
	cout << a << " " << b << endl;
}

ENode* getHead(EventList &ev)
{
	return ev;
}

bool delFirst(Enode &e, Enode &pn)
{
	if (!e->next) return false;
	pn = e->next;
	e->next = pn->next;
	pn->next = nullptr;
	return true;
}


void openForDay(EventList &ev, Queue q[])
{
	totalTime = 0;
	customerNUM = 0;
	initList(ev);
	Event en;
	en.occurTime = 0;
	en.nType = 0;
	orderInsert(ev, en, cmp);
	for (int i = 1; i <= 4; i++)
	{
		initQueue(q[i]);
	}
}

void printEvent(EventList el);

void customerArrived(EventList &ev, Queue q[], Event en, int closeTime)
{
	++customerNUM;
	int durtime = 0;
	int intertime = 0;
	Random(durtime, intertime);
	int i = 0;
	int t = en.occurTime + intertime;
	if (t < closeTime)
	{
		orderInsert(ev, { t,0 }, cmp);
	}
	i = minimumQueue(q);
	enQueue(q[i], { en.occurTime,durtime });
	if (queueLength(q[i]) == 1)
	{
		orderInsert(ev, { en.occurTime + durtime,i }, cmp);
	}
}


void customerDeparture(EventList &ev, Queue q[], Event en)
{
	int i = en.nType;
	QElemType customer;
	delQueue(q[i], customer);
	cout << "duration time  = " << en.occurTime - customer.arrivalTime << endl;
	totalTime += en.occurTime - customer.arrivalTime;
	if (!queueEmpty(q[i]))
	{
		getHead(q[i], customer);
		orderInsert(ev, { en.occurTime + customer.duration,i }, cmp);
	}

}

void printEvent(EventList el)
{
	ENode *p = el->next;
	while (p)
	{
		cout << "event occurtime = " << p->data.occurTime << " " << "ntype = " << p->data.nType << " " << endl;
		p = p->next;
	}
}


void printQueue(Queue q)
{
	QNode *p = q.front->next;
	while (p)
	{
		cout << "queue arrivaltime = " << p->data.arrivalTime << " " << "queue duration = " << p->data.duration << endl;
		p = p->next;
	}
}


void bank_Simulation(EventList &ev, Queue q[], int closeTime)
{
	openForDay(ev, q);
	ENode *p = nullptr;
	Event en;
	while (!listEmpty(ev))
	{

		delFirst(ev, p);

		en = getCurElem(p);

		if (en.nType == 0)
		{

			customerArrived(ev, q, en, closeTime);
		}
		else
		{
			customerDeparture(ev, q, en);
		}


	}
	cout << "The average time is " << 1.0*totalTime / customerNUM << endl;
}

int main()
{
	EventList ev;
	Queue q[5];
	srand((unsigned)time(NULL));
	bank_Simulation(ev, q, 24);
	return 0;
}

以上是关于[数据结构-严蔚敏版]P65离散事件模拟(银行客户的离散事件驱动模拟程序)的主要内容,如果未能解决你的问题,请参考以下文章

数据结构严蔚敏版课后答案

数据结构严蔚敏版课后答案

[数据结构-严蔚敏版]P48栈的链式表示

[数据结构-严蔚敏版]P46栈的顺序存储表示

[数据结构-严蔚敏版]P64循环队列-队列的顺序存储结构

[数据结构-严蔚敏版]P71串的抽象数据类型的定义