这是一个模拟银行窗口排队叫号调度系统,参考了网上两篇文章,一篇java的和一篇linux c++的,然后我在windows下实现了它,开发工具是vs2008.在文章最后我会给出直接可编译可执行代码。

Java版参考:http://blog.csdn.net/zhangxiaoxiang/article/details/6294132
Linux c++版参考:http://blog.csdn.net/yanjiashang/article/details/6359652

模拟实现银行业务调度系统逻辑,具体需求如下:
• 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
• 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
• 异步随机生成各种类型的客户,生成各类型用户的概率比例为:
VIP客户:普通客户:快速客户 = 1 :6 :3。
• 客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
• 各类型客户在其对应窗口按顺序依次办理业务。
• 当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
• 随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
分析,注意一下几点:
1、 其实实现方法还是挺多的,可以用3个队列分别表示vip,fast和commer,也可以只用一个list,直接在里面找,我就只用了一个list。
2、 NumberMachine要设计为单例类。
3、 注意同步与互斥的问题,同时避免死锁。当表为空时,即每人来的的时候,线程应该在等待资源,而不是一直在循环。最后假设银行一天最多只能处理50个人最为终结条件。

代码关键的地方有加点注释,我觉得不需要解释还是可以看懂了,那就直接上代码吧,文章最后会把完整程序一并给出。同时,需要看到程序有什么不妥的地方请指出,互相探讨,共同进步。

Bank_queue.cpp

#include "stdafx.h"#include <iostream>
#include <process.h>#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "numMachine.h"
#include "serviceWin.h"
#include "client.h"
#include "constances.h"
using namespace std;HANDLE thread1[4];
HANDLE thread2;
HANDLE thread3;
map<int,HANDLE> thread_Map;
enum ID{THREAD1_0,THREAD1_1,THREAD1_2,THREAD1_3,THREAD2,THREAD3
};Client::ClientType createClientType() {  int type;  type = rand() % 10 + 1;  if (type <= 6)  return Client::CLIENT_COMMON;  else if (type >= 7 && type <= 9)  return Client::CLIENT_FAST;  return Client::CLIENT_VIP;
}
int createTasktime() {  return rand() % MAX_SERVICE_TIME + 1;
}
void createClient() {  Client::ClientType clientType;  int taskTime;  for (int i = 0; i < MAX_CLIENT_NUM; ++i) {  clientType = createClientType();  taskTime = createTasktime();  std::cout << "Come in a client : clientType = " << clientType  << " taskTime = " << taskTime << std::endl;  NumMachine::getInstance()->pressMachine(clientType, taskTime);  Sleep(1);}
}
unsigned  __stdcall serviceWin(void *win) {  ServiceWin *serWin = static_cast<ServiceWin*> (win);    serWin->execute(); TerminateThread(thread_Map[serWin->getCount()],0);return 0;
}
bool createServiceWin(HANDLE thread1[],HANDLE &thread2,HANDLE &thread3) {    unsigned commonServiceWin[4];  unsigned fastServiceWin;  unsigned vipServiceWin;  char tmp[20]={0};  ServiceWin *win;  for (int i = 0; i < 4; ++i) {  sprintf(tmp, "CommonWin%d", i);  win = new CommonSerWin(tmp);  if (win == NULL) {  std::cout << "Create common service win error." << std::endl;  return -1;  }win->setCount(i);//_beginthreadex返回handleif ((thread1[i] = (HANDLE)_beginthreadex(NULL, 0,serviceWin, (void*) win,0,&commonServiceWin[i]) )== 0) {  std::cout << "Create common service thread error." << std::endl;  return false;  }thread_Map.insert(map<int,HANDLE>::value_type(i,thread1[i]));}  memset(tmp,0,sizeof(tmp));sprintf(tmp, "FastWin");  win = new FastSerWin(tmp);  if (win == NULL) {  std::cout << "Create fast service win error." << std::endl;  return false;  }  win->setCount(THREAD2);if ((thread2 = (HANDLE )_beginthreadex(NULL, 0,serviceWin, (void*) win,0,NULL/*&fastServiceWin*/)) == 0) {  std::cout << "Create fast service thread error." << std::endl;  return false;  }thread_Map.insert(map<int,HANDLE>::value_type(THREAD2,thread2));memset(tmp,0,sizeof(tmp));sprintf(tmp, "VipWin");  win = new VipSerWin(tmp);  if (win == NULL) {  std::cout << "Create vip service win error." << std::endl;  return false;  }  win->setCount(THREAD3);if ((thread3 = (HANDLE)_beginthreadex(NULL, 0,serviceWin, (void*) win,0,&vipServiceWin)) == 0) {  std::cout << "Create vip service thread error." << std::endl;  return false;  }  thread_Map.insert(map<int,HANDLE>::value_type(THREAD3,thread3));return true;
}
int _tmain(int argc, char *argv[]) {  std::cout << "/***start : please wait 5 seconds when progress suspend *****/" << std::endl;  srand((unsigned) time(0));  if (!createServiceWin(thread1,thread2,thread3)) {  std::cout << "Create service win error." << std::endl;  return -1;  }  createClient();  for(int i = 0; i<4; i++)WaitForSingleObject(thread1[i],INFINITE);WaitForSingleObject(thread2,INFINITE);WaitForSingleObject(thread3,INFINITE);delete NumMachine::getInstance();  for(int i = 0; i<4; i++)CloseHandle(thread1[i]);CloseHandle(thread2);CloseHandle(thread3);
}  

numberMachine.cpp

#include "StdAfx.h"
#include "numMachine.h"NumMachine * NumMachine::numMachine = new NumMachine;
NumMachine::NumMachine() :
leaveClient(0), total(0), handledClient(0) {
//  pthread_mutex_init(&clientListLock, NULL);  clientListLock=CreateMutex(NULL,false,NULL);clientPressLock=CreateMutex(NULL,false,NULL);m_CommerSem=CreateSemaphore(NULL, 0, 50, NULL);//当前个资源,最大允许个同时访问m_FastSem=CreateSemaphore(NULL, 0, 50, NULL);//当前个资源,最大允许个同时访问m_VipSem=CreateSemaphore(NULL, 0, 50, NULL);//当前个资源,最大允许个同时访问m_map.insert(map<Client::ClientType,HANDLE>::value_type(Client::CLIENT_COMMON,m_CommerSem));m_map.insert(map<Client::ClientType,HANDLE>::value_type(Client::CLIENT_FAST,m_FastSem));m_map.insert(map<Client::ClientType,HANDLE>::value_type(Client::CLIENT_VIP,m_VipSem));}
NumMachine::~NumMachine() {  std::cout << "/**********     end         ****************/" << std::endl;  std::cout << "The number of total client is " << total << std::endl;  std::cout << "The number of handled client is " << handledClient << std::endl;  std::cout << "The number of waiting client is " << clientList.size() << std::endl;  std::cout << "The number of client is " << leaveClient  << " ,because waiting time too long." << std::endl;  for_each(clientList.begin(), clientList.end(), DelClientList()); CloseHandle(clientListLock);
}
NumMachine * NumMachine::getInstance() {  return numMachine;
}
void NumMachine::pressMachine(Client::ClientType clientType, int taskTime) {  ++total;  if (clientList.size() < MAX_WAITING_CLIENT_NUM) {  Client * c = new Client(clientType, taskTime);  if (c == NULL) {  ReleaseMutex(clientPressLock);std::cout << "Can't create Client" << std::endl;  exit(-1);  }  clientList.push_back(c);  //semaphoreReleaseSemaphore(m_map[clientType],1,NULL);} else {  ++leaveClient;  }
}
int NumMachine::getTotal(){return total;
}Client * NumMachine::removeClient(Client::ClientType val) {  std::list<Client*>::iterator pos;  Client *tmp;  if (total == MAX_CLIENT_NUM){DWORD rtn = WaitForSingleObject(m_map[val],1000);if (WAIT_TIMEOUT == rtn){
#ifdef DEBUGstd::cout<<"val:"<<val<<",rtn:WAIT_TIMEOUT"<<std::endl;
#endifreturn NULL;}if (WAIT_ABANDONED == rtn){
#ifdef DEBUGstd::cout<<"val:"<<val<<",rtn:WAIT_ABANDONED"<<std::endl;
#endifreturn NULL;}if (WAIT_FAILED == rtn){
#ifdef DEBUGstd::cout<<"val:"<<val<<",rtn:WAIT_FAILED"<<std::endl;
#endifreturn NULL;}}else{DWORD rtn = WaitForSingleObject(m_map[val],3000);if (WAIT_TIMEOUT == rtn){
#ifdef DEBUGstd::cout<<" total < MAX_CLIENT_NUM val:"<<val<<",rtn:WAIT_TIMEOUT"<<std::endl;
#endif}if (WAIT_ABANDONED == rtn){
#ifdef DEBUGstd::cout<<"total < MAX_CLIENT_NUM val:"<<val<<",rtn:WAIT_ABANDONED"<<std::endl;
#endif}if (WAIT_FAILED == rtn){
#ifdef DEBUGstd::cout<<"total < MAX_CLIENT_NUM val:"<<val<<",rtn:WAIT_FAILED"<<std::endl;
#endif}}WaitForSingleObject(clientListLock,INFINITE);pos = find_if(clientList.begin(), clientList.end(), FindClient(val));  if (pos != clientList.end()) {  ++handledClient;  tmp = *pos; clientList.erase(pos); ReleaseMutex(clientListLock);return tmp;  } else {   ReleaseMutex(clientListLock);   return NULL;  }
}
void DelClientList::operator ()(Client *item) {  std::cout << "Waiting client : clientType =" << item->getClientType()  << " taskTime = " << item->getServiceTime() << std::endl;  delete item;
}
FindClient::FindClient(Client::ClientType t) :
clientType(t) {
}
bool FindClient::operator ()(Client * item) {  if (item->getClientType() == clientType)  return true;  else  return false;
}  

serviceWin.cpp

#include "stdafx.h"
#include "serviceWin.h"
ServiceWin::ServiceWin(std::string n) :
winName(n) {
}
ServiceWin::~ServiceWin() {
}
std::string ServiceWin::getWinName() {  return winName;
}
CommonSerWin::CommonSerWin(std::string n) :
ServiceWin(n) {
}
CommonSerWin::~CommonSerWin() {
}
void CommonSerWin::execute() {  Client * client = NULL;  while (1) {  client = NumMachine::getInstance()->removeClient(Client::CLIENT_COMMON);  if (client != NULL) {  std::cout << "A client be handled : clientType = " << client->getClientType()  << " taskTime = " << client->getServiceTime() << " "<< getWinName()<< std::endl;  Sleep(client->getServiceTime());  delete client;  }else{if (MAX_CLIENT_NUM == NumMachine::getInstance()->getTotal()){return;}}  }
}
FastSerWin::FastSerWin(std::string n) :
ServiceWin(n) {
}
FastSerWin::~FastSerWin() {
}
void FastSerWin::execute() {  Client * client = NULL;  while (1) {  client = NumMachine::getInstance()->removeClient(Client::CLIENT_FAST);  if (client != NULL) {  std::cout << "A client be handled : clientType = " << client->getClientType()  << " taskTime = " << client->getServiceTime() << " "<< getWinName() << std::endl;  Sleep(client->getServiceTime());  delete client;  }else{if (MAX_CLIENT_NUM == NumMachine::getInstance()->getTotal()){return;}}    }
}
VipSerWin::VipSerWin(std::string n) :
ServiceWin(n) {
}
VipSerWin::~VipSerWin() {
}
void VipSerWin::execute() {  Client * client = NULL;  while (1) {  client = NumMachine::getInstance()->removeClient(Client::CLIENT_VIP);  if (client != NULL) {  std::cout << "A client be handled : clientType = " << client->getClientType()  << " taskTime = " << client->getServiceTime() << " "<< getWinName() << std::endl;  Sleep(client->getServiceTime());  delete client;  }else{if (MAX_CLIENT_NUM == NumMachine::getInstance()->getTotal()){return;}}    }
}  

执行结果:

完整程序下载地址:
http://download.csdn.net/detail/luomoshusheng/8927561

银行窗口排队叫号系统实现相关推荐

  1. 模拟银行窗口排队叫号系统的运作

    最近在网上看到了一道面试题,初看很简单,细看有点意思的一道题目: http://blog.csdn.net/zhangxiaoxiang/archive/2011/04/01/6294132.aspx ...

  2. 51单片机银行自助排队叫号系统VIP热敏打印功能DY-SV17F语音播报

    实践制作DIY- GC0138-银行自助排队叫号系统VIP 基于51单片机设计---银行自助排队叫号系统VIP 二.功能介绍: STC89C52最小系统板+0.96寸OLED显示器+DY-SV17F语 ...

  3. 排队叫号 服务器 不同区域显示,银行排队叫号系统,让你摆脱排队久的问题!...

    原标题:银行排队叫号系统,让你摆脱排队久的问题! 银行排队叫号系统主要由服务器.取号机.集中液晶屏.液晶窗口显示通屏.语音系统.线缆连接部件及其他辅件等组成. ① 网络平台:可以是大厅的局域网络,系统 ...

  4. mfc 子窗体 按钮不触发_资深程序员用c++开发MFC银行排队叫号系统,小白看了也能学会...

    这个C++ 银行排队叫号系统是看了书后写出来的程序,运用于MFC理念编写,我看的书是谭浩强的<C++面向对象程序设计>相对计科的书少了前六章 直接从对象讲起,这本书也是本班使用人数最多的一 ...

  5. VS+Qt+C++银行排队叫号系统

     程序示例精选 VS+Qt+C++银行排队叫号系统 如需安装运行环境或远程调试,见文章底部个人QQ名片,由专业技术人员远程协助! 前言 这篇博客针对<<VS+Qt+C++银行排队叫号系统& ...

  6. 基于java+ssm+vue+mysql的银行排队叫号系统

    项目介绍 银行排队叫号系统是以科学合理的机构组织合作.流畅疏通的信息渠道为平台,以客户基本信息.计算机.Internet网络.网络管理软件信息技术为手段建立的信息服务管理系统.系统将借助高速和先进的计 ...

  7. 51单片机热敏打印自助排队叫号系统银行医院柜台DY-SV17F语音播报

    实践制作DIY- GC0079-自助排队叫号系统 一.功能说明: 基于51单片机设计-自助排队叫号系统 功能介绍: STC89C52RC最小系统板+0.96寸OLED显示器+DY-SV17F语音串口语 ...

  8. 基于51单片机的双机串口通信排队叫号系统(LCD显示)设计

    基于51单片机的双机串口通信排队叫号系统(LCD显示)设计 1 开发环境 视频讲解 2 功能说明介绍 3 仿真图 4 程序 5 原理图 6 视频讲解 7 设计报告 7.1 设计目的 7.2 设计要求及 ...

  9. 什么是排队叫号系统。

        叫号系统是排队叫号系统的简称,常用于银行.医院.海关.电信.工商.税务等窗口服务性行业排队叫号,能有效地改善服务环境,提高工作效率. 随着电子信息产品.智能产品的快速发展,以及人类生活对服务环 ...

  10. 排队机叫号系统服务器安装设置,易达办事大厅排队叫号系统

    易达办事大厅排队叫号系统主要面向营业厅,办事大厅等各类服务场所(系统可接物理呼叫器和LED窗口屏,可以和评价器结合使用),为改善办事大厅和管理所存在的一些混乱.无序等弊端而开发的,系统能很好地解决顾客 ...

最新文章

  1. saiku执行过程代码跟踪
  2. CSDN 插件限时内测,新用户抢永久免费去广告特权!
  3. python b站 关注_[我叫以赏]Python获取B站UP主粉丝数
  4. 我来谈谈PHP和JAVA在web开发上的的区别
  5. BaseAction
  6. wxWidgets:异形窗示例
  7. 【旧文章搬运】Windows内核常见数据结构(线程相关)
  8. 视频直播:实时数据可视化分析
  9. Android之Android studio基本调试和快捷键
  10. 最大上升子序列和(信息学奥赛一本通-T1285)
  11. (26)VHDL实现或(数据流描述)
  12. 家境普通的年轻人出路到底在何方?
  13. shell监控Nginx服务是否存在的脚本
  14. SRv6技术课堂(一):SRv6概述
  15. 破解百词斩单词数据之旅
  16. 网站跳出率是什么?如何降低网站跳出率?
  17. windows服务器迁到_Windows服务器迁移工具使用攻略
  18. 【图解】本地IIS局域网内无法访问的问题
  19. Wireshark抓包页面的登录信息
  20. 卓克科学思维必修课0821

热门文章

  1. python安装包国内镜像,pip使用国内镜像
  2. 问题来了,个人用户可以使用短信接口吗?答案很意外
  3. 2.页面截长图工具-FireShot
  4. jmeter性能测试之录制脚本
  5. 1、登录模块测试用例
  6. 杰控连接mysql_杰控FameView软件首秦ERP系统L2级中的应用
  7. 揭秘Spring——《Spring 揭秘》读书笔记纲要
  8. 专业的售后服务管理系统
  9. 网易云音乐缓存转换至mp3(Python实现)
  10. jsdroid 教程_服装制版教程如何利用紧身胸衣来制作无袖服装417才智服装