存档

文章标签 ‘银行排队’

数据结构队列C++应用:模拟银行排队系统(下)

2010年3月25日 Yarkee 没有评论

接上文……

main.cpp的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include<vector>
#include <queue>
#include <time.h>
#include<iomanip>
#include "customer.h"
using namespace std;
const int CASHIERS = 5; //设置5个窗口
 
void simulate(int cashiers, double prob_of_arrival, int service_time, int last_time);
 
int main()
{
    int cashiers=CASHIERS;
 
    // 顾客的到达概率
    double prob_of_arrival;
 
    // 每名顾客接受服务所需的时间
    int service_time;
 
    // 程序模拟持续的总时间
    int last_time;
 
    cout << "***********************模拟银行排队系统***********************\n" << endl;
    cout << "请输入每分钟内顾客的到达概率(概率应介于0和1之间): ";
    cin >> prob_of_arrival ;
    cout << "请输入每个顾客接受服务所需要的时间(时间单位:分): ";
    cin >> service_time;
    cout << "请输入银行的工作总时间(时间单位:分): ";
    cin >> last_time;
    cout << endl;
 
    // 调用函数,仿真银行运行情况
    simulate(cashiers, prob_of_arrival, service_time, last_time);
 
    system("pause");
    return 0;
}
 
void simulate(int cashiers, double prob_of_arrival, int service_time, int last_time)
{
// 用于存储顾客信息
Customer customer;
 
//5个Window的对象,分别对应5个窗口
Window Wnd[5];
 
// 顾客排队队列
queue<Customer> wait_queue;
 
// 接受了服务的顾客总数
int served_customers; 
 
// 存储每个顾客的等待时间
vector<int> wait_time; 
 
// 总共的服务时间
int total_wait_time = 0; 
 
// 随机数种子
srand(time(NULL));
 
// 随机数
double rand_num;
int count;
 
//用于给顾客编号,每来一位顾客就顺序加1
int ticket=0;
 
// 对每个窗口的时间和处理顾客数进行初始化
for (count = 0; count < cashiers; ++count){
    //cashier_vacant[count] = 0;
    //cashier_customer[count]=0;
    Wnd[count].set_cashier_vacant(0);
    Wnd[count].set_cashier_customer(0);
}
 
//开始仿真,一个单位时间扫描一次
for ( int t = 0; t < last_time; ++t){
 
    // 小于到达概率,表示说明顾客到达,顾客入队
    if ((rand_num = (rand()%100)/100.0 )<= prob_of_arrival){
        customer.set_arrival_time(ticket++,t);
        wait_queue.push(customer);
    }
 
    cout<<"****************第"<<t+1<<"个时间单元******************"<<endl;
 
    // 对窗口进行逐一扫描,判断其是否有空闲, 并且判断队列是否非空
    for(count=0;count<cashiers;++count){
        //if ((cashier_vacant[count]>t)){
        if(Wnd[count].get_cashier_vacant()>t){
            cout<<"第"<<count+1<<"号窗口正在为第"<< Wnd[count].get_cashier_ticket()+1<<"号顾客服务…"<<endl;
        }
    }
    for (count=0;count<cashiers;++count){
 
        if( (Wnd[count].get_cashier_vacant()<=t) && !wait_queue.empty() && (Wnd[count].get_cashier_vacant()<last_time)){
        // 顾客得到服务
            customer = wait_queue.front();
            wait_queue.pop();
 
            Wnd[count].set_cashier_customer( Wnd[count].get_cashier_customer()+1 );
            Wnd[count].set_cashier_ticket(customer.get_ticket_num());
 
            // 设置顾客离去时间
            customer.set_departure_time(t);
 
            // 顾客等待时间
            wait_time.push_back( t - customer.get_arrival_time());
 
            //所有已服务顾客等待所花费的总时间
            total_wait_time += customer.total_time();
 
            // 设置该窗口的下一个空闲时刻
            Wnd[count].set_cashier_vacant( t+service_time );
            cout<<"第"<<customer.get_ticket_num()+1<<"号顾客请到第"<<count+1<<"号窗口办理业务,您所花费的等待时间为"
                << t - customer.get_arrival_time()<<"分钟!"<<endl;
 
        }//end if
    }// end for
    cout<<"目前共有"<<wait_queue.size()<<"位顾客正在排队等待…"<<endl<<endl;
}// end for
 
cout<<"*******已下班,剩余顾客明天请早,不便之处,敬请原谅*******"<<endl<<endl;
 
// 接受了服务的顾客总数
served_customers = wait_time.size();
 
double average_wait_time =(double)total_wait_time/served_customers ;
 
// 输出统计结果
cout<<"*****************今日银行工作情况总结*********************"<<endl;
for(count=0;count<cashiers;count++){
    cout<<"第"<<count+1<<"号窗口接待的顾客数为"<<Wnd[count].get_cashier_customer()<<"名"<<endl;
}
cout<<endl;
cout << "得到服务的顾客数为"<< served_customers << "名"<<endl;
cout << "得到服务的顾客所需要的平均等待时间为" << setprecision(2)<<average_wait_time<<"分钟"<< endl;
cout << endl;
return 0;
}
分类: 编程 标签: ,

数据结构队列C++应用:模拟银行排队系统(上)

2010年3月25日 Yarkee 没有评论

前几天做的一个数据结构课程的作业,题目要求如下图:

在做之前,有参考网上的一份有点相似的代码,所以有些变量的命名就没有自己想了。程序分为两个文件,一个是头文件customer.h,该头文件中包含和所需要的类的声明和定义,另外一个是main.cpp文件,即主程序文件。写了比较详细的注释,应该能让读者比较好理解。

customer.h文件中的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef CUSTOMER_H_
#define CUSTOMER_H_
 
class Customer{
public:
    // 设置到达时刻
    void set_arrival_time(int ticket,int arrives)
    {
        ticket_num=ticket;
        arrival_time = arrives;
    }
 
    //读出到达队列的时刻
    int get_arrival_time() const
    {
        return arrival_time;
    }
 
    // 设置离开队列的时刻
    void set_departure_time(int departs)
    {
        departure_time = departs;
    }
 
    // 求出等待的时间
    int total_time()
    {
        return departure_time - arrival_time;
    }
 
    //返回顾客编号
    int get_ticket_num()
    {
        return ticket_num;
    }
 
private:
    int ticket_num,arrival_time, departure_time;
};
 
class Window{
public:
    //存储该窗口处理的顾客数
    void set_cashier_customer(int num)
    {
        cashier_customer=num;
    }
 
    //读取该窗口处理的顾客数
    int get_cashier_customer()
    {
        return cashier_customer;
    }
 
    //存储该窗口正在服务的顾客编号
    void set_cashier_ticket(int num)
    {
        cashier_ticket=num;
    }
 
    //读取该窗口正在服务的顾客编号
    int get_cashier_ticket()
    {
        return cashier_ticket;
    }
 
    //设置该窗口的下一个空闲时刻
    void set_cashier_vacant(int num)
    {
        cashier_vacant=num;
    }
    //读取该窗口的下一个空闲时刻
    int get_cashier_vacant()
    {
        return cashier_vacant;
    }
 
private:
    //该窗口一共服务的顾客数
    int cashier_customer;
 
    //该窗口当前服务的顾客的编号
    int cashier_ticket;
 
    //该窗口的空闲时刻
    int cashier_vacant;
};
#endif



未完待续……

分类: 编程 标签: ,

WP SlimStat