博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
银行业务模拟
阅读量:4624 次
发布时间:2019-06-09

本文共 6725 字,大约阅读时间需要 22 分钟。

银行业务模拟是队列中非常经典的应用之一,各种版本的数据结构教材在队列这一章节援引这一应用。

本程序算法来源于《数据结构(C语言版)》(清华大学出版社 严蔚敏,吴伟民编著)第65页。

本程序在Ubuntu14.10平台上,利用g++编译通过。

#include 
#include
#include
#include
#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERLOW -2#define QNUMBER 5 //The bank has 4 windows //You can set the number of bank windows here!! //Note:the number of windows must lower than 4 !!#define OPENTIME 8 //Bank opens at 8:00#define CLOSETIME 18 //Bank closes at 18:00#define Q1_CUSTNUM 10000 //The first numberical order for first customer in queue1#define Q2_CUSTNUM 20000 //The first numberical order for first customer in queue2#define Q3_CUSTNUM 30000 //The first numberical order for first customer in queue3#define Q4_CUSTNUM 40000 //The first numberical order for first customer in queue4#define INTERTIME_LOWER 1 //You can set the range of intertime and durtime here!!#define INTERTIME_UPPER 10#define DURTIME_LOWER 5#define DURTIME_UPPER 20/*------------------------------------------------------- definition of the storage structure-------------------------------------------------------*/typedef struct { int OccurTime; //when event occur int NType; //0 re customer arrives,1-4 re customer at 1-4 window depart}Event,ElemType;typedef struct EventNode{ Event event; struct EventNode *next;}EventNode,*EventPtr;typedef struct { EventPtr head; EventPtr rear; int length;}EventList;typedef struct { int ArrivalTime; //the time customer arrives int Durtime; //the time customer in service int CustNum; //a code for every customer}QElemType;typedef struct QNode{ QElemType customer; struct QNode *next;}QNode,*QueuePtr;typedef struct { QueuePtr head; QueuePtr rear; int length;}LinkQueue;EventList ev;Event en; LinkQueue q[QNUMBER]; //four customer queuesQElemType customer; //custromer recordint TotalTime,CustomerNum,CloseTime;//record for the total time customers in bank, //and how many customers in bank one day.int q1,q2,q3,q4; //customer counter for matching queue.typedef int Status;/*----------------------------------------------------- function of element OP of LinkQueue-----------------------------------------------------*/Status InitQueue(LinkQueue &Q){ Q.head = Q.rear = (QueuePtr)malloc(sizeof(QNode)); if(!Q.head) exit(OVERLOW); Q.head->next = NULL; Q.length = 0; return OK;}Status EnQueue(LinkQueue &Q,QElemType e){ QueuePtr p; p = (QueuePtr)malloc(sizeof(QNode)); if(!p) exit(OVERLOW); p->customer = e; p->next = NULL; Q.rear->next = p; Q.rear = p; Q.length++; return OK;}Status IsQEmpty(LinkQueue Q){ if(Q.head == Q.rear) return TRUE; //Q.length ==0; else return FALSE;}Status GetQHead(LinkQueue Q,QElemType &e){ e = Q.head->next->customer; return OK;}Status DelQueue(LinkQueue &Q,QElemType &e){ QueuePtr p; if(IsQEmpty(Q)) return ERROR; p = Q.head->next; e = p->customer; Q.head->next = p->next; if(Q.rear == p) //Queue only has one elememt Q.rear = Q.head; free(p); Q.length--; return OK;}Status QueueLength(LinkQueue Q){ return Q.length;}Status TypeQueue(LinkQueue Q,int qnum){ QueuePtr p; p = Q.head->next; int atime; if(p == NULL){ printf("Empty ListQueue:%d\n",qnum); return OK; } printf("ListQueue:%-3dlength=%d\n",qnum,Q.length); printf("ArrivalTime\tCustomer_Num\tDurtime\n"); while(p!=NULL){ atime = p->customer.ArrivalTime; printf("%d:%d%d\t\t%d\t\t%d\n",OPENTIME+atime/60,atime%60/10,atime%10, p->customer.CustNum,p->customer.Durtime); p = p->next; } return OK;}/*----------------------------------------------------- function of element OP of EventList-----------------------------------------------------*/typedef int (*fp)(Event,Event);int compare(Event a,Event b){ if(a.OccurTime < b.OccurTime) return -1; if(a.OccurTime == b.OccurTime) return 0; if(a.OccurTime > b.OccurTime) return 1;}Status InitList(EventList &E){ E.head = E.rear = (EventPtr)malloc(sizeof(EventNode)); if(!E.head) exit(OVERLOW); E.head->next = NULL; //E.rear->next == NULL; E.length = 0; return OK;}Status ListEmpty(EventList E){ if(E.length == 0) return TRUE; else return FALSE;}Status OrderInsert(EventList &E,Event e,fp cmp){ //ascending order according the occurtime. Status TypeEventList(EventList E); EventPtr p,q,s; p = (EventPtr)malloc(sizeof(EventNode)); if(!p) exit(OVERLOW); p->event = e; p->next = NULL; if(E.head == E.rear){ E.rear->next = p; //E.head->next = p;(E.head = E.rear;) E.rear = p; E.length++; return OK; } if(E.head != E.rear){ q = E.head->next; if(cmp(e,q->event) < 0){ E.head->next = p; p->next = q; E.length++; return OK; } while(cmp(e,q->event) >= 0){ s = q; q = q->next; if(q == NULL){ E.rear = p; break; } } s->next = p; p->next = q; E.length++; } return OK;}Status GetEHead(EventList E,Event &e){ e = E.head->next->event; return OK;}Status DelFirst(EventList &E,Event &e){ Status TypeEventList(EventList E); GetEHead(E,e); EventPtr ep; ep = E.head->next; E.head->next = ep->next; if(E.rear == ep) E.rear = E.head; free(ep); E.length--; return OK;}Status TypeEventList(EventList E){ EventPtr p; int otime; p = E.head->next; if(p == NULL){ printf("Empty List\n"); return OK; } printf("EventList:length=%d\n",E.length); printf("OccurTime\tNType\n"); while(p!=NULL){ otime = p->event.OccurTime; printf("%d:%d%d\t\t%2d\n",OPENTIME+otime/60,otime%60/10, otime%10,p->event.NType); p = p->next; } return OK;}/*----------------------------------------------------- function of element OP Bank Business simulation-----------------------------------------------------*/fp cmp = compare;void Note_CustomerArrived(QElemType Q){ printf("\n%d:%d%d\tcustomer %d arrived:\n",OPENTIME+en.OccurTime/60, en.OccurTime%60/10,en.OccurTime%10,Q.CustNum); TypeEventList(ev); for(int i=1 ; i

 

转载于:https://www.cnblogs.com/JarningGau/p/5360792.html

你可能感兴趣的文章
Python基础学习笔记02之list
查看>>
jquery实现拖拽的效果
查看>>
JS 获取图片标签和所有的图片中的src的正则表达式
查看>>
jQuery:1.5.5.2,京东导航(当前默认设置)
查看>>
ASP.NET中 DetailsView(详细视图)的使用前台绑定
查看>>
我又情不自禁了——立方网的又一次加速度
查看>>
如何屏蔽国内IP访问我们的网站的一些方法!
查看>>
起与伏
查看>>
JWT(Json Web Token—)的定义及组成
查看>>
2.网络编程-udp
查看>>
显示页面链接(7.5.1)
查看>>
数据库们~MySQL~MongoDB~Redis
查看>>
SQL*Plus 系统变量之28 - LOBOF[FSET]
查看>>
【洛谷】P1247取火柴游戏
查看>>
学习过程中的三个小小程序
查看>>
StringBuffer的添加与删除功能
查看>>
[zt]如何有效地报告Bug
查看>>
手把手教你使用FineUI开发一个b/s结构的取送货管理信息系统(附源码+视频教程(1,2节))...
查看>>
linux磁盘分区fdisk分区和parted分区
查看>>
Linux系统清除缓存
查看>>