银行排队模拟(队列)

银行排队模拟程序

队列类Queue

#ifndef QUEUE_H
#define QUEUE_H
struct Record                                            //顾客结构体
{
    int ArrivedTime;                                     //顾客到达时间
    int CostTime;                                        //顾客办理时间
    int Num;                                             //顾客编号
};
const int QueueSize=10;
class Queue
{
    public:
        Queue();                                          //构造函数
        virtual ~Queue();                                 //析构函数
        void addFirst(int x,int y,int z);                 //入队
        int popLast();                                    //出队
        int Getqueue1();                                  //获取队头元素的到达时间
        int Getqueue2();                                  //获取队头元素的办理时间
        int Getqueue3();                                  //获取队头元素的编号
        bool Empty(){
            if(front==rear) return true;
            else return false;
        }
        Record data[QueueSize];
        int front,rear;
};
#endif
#include "Queue.h"
#include<stdio.h>
#include<stdlib.h>
Queue::Queue()
{
    //ctor
    int data=(int)malloc(2*sizeof(QueueSize));
    front=rear=QueueSize-1;
}
Queue::~Queue()
{
    //dtor
    delete []data;
}
void Queue::addFirst(int x,int y,int z)
{
    if((rear+1)%QueueSize==front) throw"иорГ";
    rear=(rear+1)%QueueSize;
    data[rear].ArrivedTime=x;
    data[rear].CostTime=y;
    data[rear].Num=z;
}
int Queue::popLast()
{
    if(rear==front) throw"обрГ";
    front=(front+1)%QueueSize;
    return 0;
}
int Queue::Getqueue1()
{
    if(rear==front) throw"обрГ";
    int i=(front+1)%QueueSize;
    return data[i].ArrivedTime;
}
int Queue::Getqueue2()
{
    if(rear==front) throw"обрГ";
    int i=(front+1)%QueueSize;
    return data[i].CostTime;
}
int Queue::Getqueue3()
{
    if(rear==front) throw"обрГ";
    int i=(front+1)%QueueSize;
    return data[i].Num;
}
#ifndef MODELQUEUE_H
#define MODELQUEUE_H
#include <Queue.h>
struct bankWindow                                  //银行窗口结构体
{
    int Runningtime;                               //银行窗口的已办公时间
    int number;                                    //银行窗口号
    int needtime;                                  //银行窗口顾客所需办理时间
    bool V;                                        //银行窗口顾客类型(普通和VIP)
};
class Modelqueue : public Queue
{
    public:
        Modelqueue();                              //构造函数
        virtual ~Modelqueue();                     //析构函数
        void Modelbank();                          //模拟函数
};
#endif // MODELQUEUE_H

模拟排队类Modelqueue

#include "Modelqueue.h"
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<bits/stdc++.h>
#include<time.h>
using namespace std;
Modelqueue::Modelqueue()
{
    //ctor
}
Modelqueue::~Modelqueue()
{
    //dtor
}
void Modelqueue::Modelbank()
{
    int time=0,f=0,r=30;
    //int Window[4];
    Queue Vipc;
    Queue comc;
    int flag;//1为VIP,0为普通
    Record customer;
    int winnum;
    cout <<"窗口数量:";
    cin>>winnum;
    struct bankWindow b[winnum];
    int n;
    cout<<"顾客数量:";
    cin>>n;
    cout << "------模拟开始------" << endl<<"初始化用户:"<<endl<<"是否VIP | 到达时间 | 办公时间 | 编号"<<endl;
    //cout<<n<<endl;
    while(n--)
    {
        scanf("%d %d %d %d",&flag,&customer.ArrivedTime,&customer.CostTime,&customer.Num);
        if(flag==1) Vipc.addFirst(customer.ArrivedTime,customer.CostTime,customer.Num);
        else comc.addFirst(customer.ArrivedTime,customer.CostTime,customer.Num);
    }
    Sleep(1000);
    system("cls");
    cout<<endl<<endl;
    for(int win=0;win<winnum;win++)
    {
        if(Vipc.Getqueue1()==time)
        {
            cout<<"   VIP客户,    编号为:"<<Vipc.Getqueue3()<<"   请进入   "<<win<<"号窗口服务"<<endl<<endl;
            b[win].number = Vipc.Getqueue3();
            b[win].needtime = Vipc.Getqueue2();
            b[win].Runningtime = 1;
            b[win].V=true;
            Vipc.popLast();
        }
        else if(comc.Getqueue1()==time)
        {
            cout<<"   普通客户,   编号为:"<<comc.Getqueue3()<<"   请进入   "<<win<<"号窗口服务"<<endl<<endl;
            b[win].number = comc.Getqueue3();
            b[win].needtime = comc.Getqueue2();
            b[win].Runningtime = 1;
            b[win].V=false;
            comc.popLast();
        }
        else b[win].Runningtime=0;
    }
    while(1)
    {
        Sleep(2000);
        system("cls");
        cout<<endl<<endl<<"-当前模拟时间:"<<time<<"-分钟-"<<endl<<endl;
        cout<<"窗口    "<<"   窗口状态   "<<"     客户类型    "<<" 客户编号   "<<endl<<"------------------------------------------------"<<endl<<endl;
        for(int i=0;i<winnum;i++)
        {
            if(time==0)
            {
                cout<<"窗口"<<i<<":    -等待服务-    "<<endl<<endl;
            }
            else if(time!=0&&b[i].Runningtime!=0)
            {
                cout<<"窗口"<<i<<":    -正在服务-    ";
                if(b[i].V==true) cout<<"    VIP        "<<b[i].number<< endl<<endl;
                else cout<<"   普通        "<<b[i].number<<endl<<endl;
            }
            else cout<<"窗口"<<i<<":    -等待服务-   "<<endl<<endl;
            if(b[i].Runningtime==b[i].needtime+1)
            {
                b[i].Runningtime=0;
            }
            else if(b[i].Runningtime==0)
            {
                if(Vipc.Empty()==false&&Vipc.Getqueue1()<time+1)
                {
                    b[i].number = Vipc.Getqueue3();
                    b[i].needtime = Vipc.Getqueue2();
                    b[i].Runningtime +=1;
                    b[i].V=true;
                    Vipc.popLast();
                }
                else if(comc.Empty()==false&&comc.Getqueue1()<time+1)
                {
                    b[i].number = comc.Getqueue3();
                    b[i].needtime = comc.Getqueue2();
                    b[i].Runningtime += 1;
                    b[i].V=false;
                    comc.popLast();
                }//if(time==6) cout<<Vipc.Empty()<<endl;
                //else time=19;
            }
            else
            {
                b[i].Runningtime++;
            }
        }
        time++;
        if(Vipc.Empty()==true&&comc.Empty()==true&&f==0)
        {
            r=time+7;
            f=1;
        }
        if(time==r) break;
    }
    Sleep(3000);
    system("cls");
    int g=10;
    while(g--)
    {
        if(g==9) cout<<endl<<endl<<"哈哈,服务结束啦,银行下班啦"<<endl;
        for(int i=0;i<100-g*10;i++)
        {
            cout<<" ";
        }
        cout<<"谢谢观看"<<endl;
    }
}

主函数

#include <iostream>
#include<stdio.h>
#include<queue>
#include<stdlib.h>
#include<time.h>
#include "Modelqueue.h"
using namespace std;
/*
样例
4
10
0 0 5 1001
0 0 4 1002
1 0 5 1003
0 1 4 1004
1 1 6 1005
0 1 7 1006
0 2 4 1007
1 4 5 1008
0 5 5 1009
1 5 4 1010
*/
int main()
{
    Modelqueue bank;
    bank.Modelbank();
    return 0;
}

以下是运行窗口图:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 14
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
假设某银行有n个窗口对外接待客户,从早晨银行9点开门起到5点关门,不断有客户进入银行,由于每个窗口在某个时刻只能接待一个客户。因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进银行的客户。如果某个窗口的业务员正空闲,则可上前输业务。反之,若个窗口均有客户所占,他便会排在为数最少的队伍后面。编制一个程序模拟银行的这种业务活动并计算一天中客户在银行的平均逗留时间。 首先从题目分析:N个窗口排队,首先就要建立N个队列来存储排队的用户信息 ,然后算出那个队列最短就用户就到那个队伍排队,同时通过随机生成他办理业务的时间和到来的时间,通过计算用户的到来时间和离开时间就可以计算出某个用户在银行的逗留时间 ;话不多说直接上代码。 下面是主函数,由用户输入银行上下班时间,计算营业多长时间Total_time,如何当前时间小于关门的时间,就一直进入customer_into();函数,用户不断的进来 #define FALSE 0 #define TRUE 1 #define QUEUE_SUM 4 //窗口的数量 int rand_business_time=0, rand_wait_time=0;//定义办理时间,等待时间变量 int Total_time=0,now_tim=0;//总时间,当前时间 int go_time[4] = {0,0,0,0};//定义数组存储每个窗口最后一位办理完业务的时间 int sum_nan[4] = {0,0,0,0};//定义数组存储每个窗口排队的人数 int Sign=TRUE; //是否关门标志位 float Sum_Wait_Time=0.0; //等待的总时间 float Sun_Nan=0.0; //总人数 int open_time;//开门时间 int off_time; //关门时间 int main() { Prompted(); printf("输入银行的24小时制营业时间:如营业时间为9:00--17:00,则应输入:9,17\n"); scanf("%d,%d", &open;_time,&off;_time); Total_time = (off_time - open_time) * 60;//计算银行总营业多少分钟 for (int i = 0; i now_time) { customer_into(); //客户进入函数 } printf("银行关门时间到不再接收客人\n\n"); for (int i = 0; i < QUEUE_SUM; i++) { DisposeQueue(&queue;[i],i);//输入在银行关门前还没有办理完业务的客户信息 } printf("平均时间为%.2f分钟",Sum_Wait_Time/Sun_Nan); /*通过各个客户的总等待时间总和/总人数算出逗留平均时间*/ _getch(); return 0; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值