大佬教程收集整理的这篇文章主要介绍了STL实践项目之用queue模拟超市结账环节,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#ifndef CUSTOMER_H #define CUSTOMER_H class Customer { protected: size_t service_t {}; //顾客结账需要的时间 public: explicit Customer(size_t st = 10) :service_t {st}{} //模拟随着时间的变化,顾客结账所需时间也会减短 Customer& time_decrement() { if (service_t > 0) --service_t; return *this; } bool done() const { return service_t == 0; } }; #endif这里只有一个成员变量 service_t,用来记录顾客结账需要的时间。每个顾客的结账时间都不同。每过一分钟,会调用一次 time_decrement() 函数,这个函数会减少 service_t 的值,它可以反映顾客结账所花费的时间。当 service_t 的值为 0 时,成员函数 done() 返回 true。
#ifndef @R_944_10943@KOUT_H #define @R_944_10943@KOUT_H #include <queue> // For queue container #include "Customer.h" class @R_944_10943@kout { private: std::queue<Customer> customers; //该队列等到结账的顾客数量 public: void add(const customer& customer) { customers.push(customer); } size_t qlength() const { return customers.size(); } void time_increment() { if (!customers.empty()) { //有顾客正在等待结账,如果顾客结账了,就出队列 if (customers.front().time_decrement().done()) customers.pop(); } } bool operator<(const checkout& other) const { return qlength() < other.qlength(); } bool operator>(const checkout& other) const { return qlength() > other.qlength(); } }; #endif可以看到,queue 容器是 @R_944_10943@kout 唯一的成员变量,用来保存等待结账的 Customer 对象。成员函数 add() 可以向队列中添加新顾客。只能处理队列中的第一个元素。 每过一分钟,调用一次 @R_944_10943@kout 对象的成员函数 time_increment(},它会调用第一个 Customer 对象的成员函数 time_decrement() 来减少剩余的等待时间,然后再调用成员函数 done()。如果 done() 返回 true,表明顾客结账完成,因此把他从队列中移除。@R_944_10943@kout 对象的比较运算符可以比较队列的长度。
std::uniform_int_diStribution<> d {10,100};这里只定义了分布对象 d,它指定了整数值分布的范围。为了获取这个范围内的随机数,我们需要使用一个随机数生成器,然后把它作为参数传给 d 的调用运算符,从而返回一个随机整数。 random 头文件中定义了几种随机数生成器。这里我们使用最简单的一个,可以按如下方式定义:
std::random_device random_number_ENGIne;为了在 d 分布范围内生成随机数,我们可以这样写:
auto value = d(random_number_ENGInE);value 的值在 d 分布范围内。
#include <iostream> // For standard streams #include <iomanip> // For stream manipulators #include <vector> // For vector container #include <String> // For String class #include <numeric> // For accumulate() #include <algorithm> // For min_element & max_element #include <random> // For random number generation #include "Customer.h" #include "@R_944_10943@kout.h" using std::string; using diStribution = std::uniform_int_diStribution<>; // 以横向柱形图的方式输出每个服务时间出现的次数 void histogram(const std::vector<int>& v,int min) { String bar (60,'*'); for (size_t i {}; i < v.size(); ++i) { std::cout << std::setw(3) << i+min << " " //结账等待时间为 index + min << std::setw(4) << v[i] << " " //输出出现的次数 << bar.substr(0,v[i]) << (v[i] > static_cast<int>(bar.size()) ? "...": "") << std::endl; } } int main() { std::random_device random_n; //设置最大和最小的结账时间,以分钟为单位 int service_t_min {2},service_t_max {15}; diStribution service_t_d {service_t_min,service_t_max}; //设置在超市开业时顾客的人数 int min_customers {15},max_customers {20}; diStribution n_1st_customers_d {min_customers,max_customers}; // 设置顾客到达的最大和最小的时间间隔 int min_arr_interval {1},max_arr_interval {5}; diStribution arrival_interval_d {min_arr_interval,max_arr_interval}; size_t n_@R_944_10943@kouts {}; std::cout << "输入超市中结账柜台的数量:"; std::cin >> n_@R_944_10943@kouts; if (!n_@R_944_10943@kouts) { std::cout << "结账柜台的数量必须大于 0,这里将默认设置为 1" << std::endl; n_@R_944_10943@kouts = 1; } std::vector<@R_944_10943@kout> @R_944_10943@kouts {n_@R_944_10943@kouts}; std::vector<int> service_times(service_t_max-service_t_min+1); //等待超市营业的顾客人数 int count {n_1st_customers_d(random_n)}; std::cout << "等待超市营业的顾客人数:" << count << std::endl; int added {}; int service_t {}; while (added++ < count) { service_t = service_t_d(random_n); std::min_element(std::begin(@R_944_10943@kouts),std::end(@R_944_10943@kouts))->add(Customer(service_t)); ++service_times[service_t - service_t_min]; } size_t time {}; const size_t @R_958_10586@l_time {600}; // 设置超市持续营业的时间 size_t longest_q {}; // 等待结账最长队列的长度 // 新顾客到达的时间 int new_cust_interval {arrival_interval_d(random_n)}; //模拟超市运转的过程 while (time < @R_958_10586@l_timE) { ++time; //时间增长 // 新顾客到达 if (--new_cust_interval == 0) { service_t = service_t_d(random_n); // 设置顾客结账所需要的时间 std::min_element(std::begin(@R_944_10943@kouts),std::end(@R_944_10943@kouts))->add(Customer(service_t)); ++service_times[service_t - service_t_min]; // 记录结账需要等待的时间 //记录最长队列的长度 for (auto & @R_944_10943@kout : @R_944_10943@kouts) longest_q = std::max(longest_q,@R_944_10943@kout.qlength()); new_cust_interval = arrival_interval_d(random_n); } // 更新每个队列中第一个顾客的结账时间 for (auto & @R_944_10943@kout : @R_944_10943@kouts) @R_944_10943@kout.time_increment(); } std::cout << "最大的队列长度为:" << longest_q << std::endl; std::cout << "\n各个结账时间出现的次数::\n"; histogram(service_times,service_t_min); std::cout << "\n总的顾客数:" << std::accumulate(std::begin(service_times),std::end(service_times),0) << std::endl; return 0; }直接使用 using 指令可以减少代码输入,简化代码。顾客结账信息记录在 vector 中。结账时间减去 service_times 的最小值可以用来索引需要自增的 vector 元素,这导致 vector 的第一个元素会记录下最少结账时间出现的次数。histogram() 函数会以水平条形图的形式生成每个服务时间出现次数的柱状图。
输入超级中结账柜台的数量:2
等待超市营业的顾客人数:20
最大的队列长度为:43
各个结账时间出现的次数:
2 13 *************
3 20 ********************
4 11 ***********
5 16 ****************
6 12 ************
7 18 ******************
8 17 *****************
9 18 ******************
10 10 **********
11 22 **********************
12 19 *******************
13 13 *************
14 16 ****************
15 18 ******************
总的顾客数:223
以上是大佬教程为你收集整理的STL实践项目之用queue模拟超市结账环节全部内容,希望文章能够帮你解决STL实践项目之用queue模拟超市结账环节所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。