数据结构第三章作业
This commit is contained in:
139
作业/数据结构-金健/C++/第三章作业/src/MVC.cpp
Normal file
139
作业/数据结构-金健/C++/第三章作业/src/MVC.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
// @Time : 2023-10-28 16:11:17
|
||||
// @FileName: MVC.cpp
|
||||
// @Author : 423A35C7
|
||||
// @Software: VSCode
|
||||
|
||||
#include "controller.hpp"
|
||||
#include "MVC.h"
|
||||
// #ifdef __WIN32__
|
||||
// 添加"-D_HAS_STD_BYTE=0",的方法不知道为什么没用
|
||||
// #include <windows.h>
|
||||
// #else
|
||||
// #include <unistd.h>
|
||||
// #define Sleep(a) usleep(a * 1000) // 需要小于一秒
|
||||
// #endif
|
||||
// 用_sleep了,虽然会提示不建议使用了,但是用window.h编译出来的会被认为是病毒
|
||||
|
||||
|
||||
class Simple {
|
||||
private:
|
||||
SingleQueueModel<Customer> model;
|
||||
SimpleQueueView<Customer, SingleQueueModel<Customer>> view{model, 5, 5};
|
||||
// SimpleQueueView<SingleQueueModel<Customer>> view = SimpleQueueView<SingleQueueModel<Customer>>(model, 5, 5, 20);
|
||||
BackgroundView background_view{gate_x, gate_y};
|
||||
SingleQueueController<Customer> controller{model, view, speed};
|
||||
// int probability_num = DEFAULT_PROBABILITY_NUM;
|
||||
|
||||
public:
|
||||
// main可以认为是外部对Controller的操作
|
||||
// num是序号
|
||||
Status _main() {
|
||||
static int_ num = 0;
|
||||
if (rand() % probability_num == 0) { // 0.01的概率
|
||||
Customer customer{num++, rand() % (max_money) + 1};
|
||||
this->model.push(customer);
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 可以认为此操作不应出错
|
||||
void refresh() {
|
||||
this->controller.refresh();
|
||||
this->background_view.refresh();
|
||||
this->view.refresh();
|
||||
_sleep(sleep_time);
|
||||
}
|
||||
};
|
||||
|
||||
class Multi {
|
||||
using one_model = SingleQueueModel<Customer>;
|
||||
using one_view = SimpleQueueView<Customer, SingleQueueModel<Customer>>;
|
||||
using one_controller = SingleQueueController<Customer>;
|
||||
|
||||
std::vector<std::shared_ptr<one_model>> models;
|
||||
std::vector<std::shared_ptr<one_view>> views;
|
||||
BackgroundView background_view{gate_x, gate_y};
|
||||
std::vector<std::shared_ptr<one_controller>> controllers;
|
||||
DriftingView<Customer> drifting_view;
|
||||
DriftingController<Customer> drift_controller;
|
||||
int len = 0;
|
||||
int probability_num = DEFAULT_PROBABILITY_NUM;
|
||||
int speed = DEFAULT_SPEED;
|
||||
int max_money = MAX_MONEY;
|
||||
double walk_speed = DEFAULT_WALK_SPEED;
|
||||
|
||||
public:
|
||||
Multi(int len, int probability_num = DEFAULT_PROBABILITY_NUM, int speed = DEFAULT_SPEED, int max_money = MAX_MONEY, double walk_speed = DEFAULT_WALK_SPEED) : len(len), probability_num(probability_num), speed(speed), max_money(max_money), walk_speed(walk_speed), drift_controller(controllers, drifting_view) {
|
||||
if (len < 1)
|
||||
throw std::bad_array_new_length(); // 异常这样用好像不太对
|
||||
for (int i = 0; i < len; i++) {
|
||||
// one_model _model {};
|
||||
// one_view _view(_model, 5 + i * 20, 5);
|
||||
// one_controller _controller(_model, _view); // 这里的引用很奇怪,可能是自己创建的类在_model离开作用域后继续引用它无法保证它不被修改,也就是说它引用的可能还是原来的变量名,但原来的变量名被更改了,但vector里可以继续正确引用
|
||||
// one_view _view(this->models.back()); // 这样也无法正确引用,看来还是得用指针
|
||||
// one_controller _controller(this->models.back(), this->views.back());
|
||||
|
||||
// vector内对一个对象可能有多个引用,所以可能使用了unique_ptr就无法放置在vector里
|
||||
std::shared_ptr<one_model> _model(new one_model());
|
||||
std::shared_ptr<one_view> _view(new one_view(*_model, base_x, base_y + i * sep));
|
||||
std::shared_ptr<one_controller> _controller(new one_controller(*_model, *_view, this->speed));
|
||||
this->models.push_back(_model);
|
||||
this->views.push_back(_view);
|
||||
this->controllers.push_back(_controller);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果有多个最短的,最左边的优先
|
||||
int get_shortest_queue() {
|
||||
int min_value = int_MAX;
|
||||
int argmin = 0;
|
||||
for (int i = 0; i < this->len; i++) {
|
||||
if (this->models[i]->get_length() < min_value) {
|
||||
min_value = this->models[i]->get_length();
|
||||
argmin = i;
|
||||
}
|
||||
}
|
||||
return argmin;
|
||||
}
|
||||
|
||||
Status _main() {
|
||||
static int_ num = 0;
|
||||
this->drifting_view.main();
|
||||
if (rand() % this->probability_num == 0) { // 0.01的概率
|
||||
Customer customer{num++, rand() % (this->max_money) + 1};
|
||||
// this->models[this->get_shortest_queue()]->push(customer);
|
||||
this->drift_controller.push(
|
||||
customer,
|
||||
this->get_shortest_queue(),
|
||||
std::make_pair(gate_x, gate_y),
|
||||
this->walk_speed);
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
for (auto _controller : this->controllers) {
|
||||
_controller->refresh();
|
||||
}
|
||||
this->background_view.refresh();
|
||||
for (auto _view : this->views) {
|
||||
_view->refresh();
|
||||
}
|
||||
this->drifting_view.refresh();
|
||||
_sleep(sleep_time);
|
||||
}
|
||||
};
|
||||
|
||||
Status main_simple() {
|
||||
Simple simple;
|
||||
std::function<Status()> _main = std::bind(&Simple::_main, &simple);
|
||||
std::function<void()> refresh = std::bind(&Simple::refresh, &simple);
|
||||
return mainloop(_main, refresh, total_time);
|
||||
}
|
||||
|
||||
Status main_multi() {
|
||||
Multi multi(window_num, probability_num, speed, max_money);
|
||||
std::function<Status()> _main = std::bind(&Multi::_main, &multi);
|
||||
std::function<void()> refresh = std::bind(&Multi::refresh, &multi);
|
||||
return mainloop(_main, refresh, total_time);
|
||||
}
|
||||
66
作业/数据结构-金健/C++/第三章作业/src/main.cpp
Normal file
66
作业/数据结构-金健/C++/第三章作业/src/main.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
// @Time : 2023-10-23 19:40:03
|
||||
// @FileName: main.cpp
|
||||
// @Author : 423A35C7
|
||||
// @Software: VSCode
|
||||
|
||||
#include "MVC.h" // 应该是cmake里不应该重复include一遍
|
||||
using namespace std;
|
||||
|
||||
// 以下的是通过向类传递参数设置的
|
||||
int probability_num = DEFAULT_PROBABILITY_NUM; // 每个时刻有1/probability_num的概率来人
|
||||
int speed = DEFAULT_SPEED; // 每个窗口办理的速度
|
||||
int_ total_time = DEFAULT_TOTAL_TIME; // 总时刻数
|
||||
int max_money = MAX_MONEY; // 最大携带金额
|
||||
int window_num = DEFAULT_WINDOW_NUM; // 柜台数量
|
||||
double walk_speed = DEFAULT_WALK_SPEED; // 人的走路速度
|
||||
|
||||
// 以下的是通过全局变量设置的
|
||||
int sleep_time = DEFAULT_SLEEP_TIME; // 每次刷新间隔多少毫秒
|
||||
int base_x = DEFAULT_BASE_X; // 起始位置距离终端上边几个字符的距离
|
||||
int base_y = DEFAULT_BASE_Y; // 起始位置距离终端左边几个字符的距离
|
||||
int sep = DEFAULT_SEP; // 每个窗口间隔多少距离
|
||||
int gate_x = DEFAULT_GATE_X; // 大门的位置终端上边几个字符的距离
|
||||
int gate_y = DEFAULT_GATE_Y; // 大门的位置终端左边几个字符的距离
|
||||
unsigned seed = DEFAULT_RANDOM_SEED; // 随机数种子
|
||||
|
||||
template <typename T>
|
||||
Status get_input(T &variable, string prompt) {
|
||||
cout << "请输入" << prompt << "(默认值" << variable << "): ";
|
||||
while (true) {
|
||||
if (cin >> variable) {
|
||||
break;
|
||||
}
|
||||
cin.clear();
|
||||
cin.ignore(2048, '\n');
|
||||
cout << "输入错误,请重新输入:";
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
int main(int, char **) {
|
||||
// std::cout << "Hello, from 第三章作业!\n";
|
||||
srand(seed);
|
||||
int mode = 2;
|
||||
get_input(mode, "模式,1为单个队列,2为多个队列,3为自定义,其他整数表示退出");
|
||||
switch (mode) {
|
||||
case 1:
|
||||
return main_simple();
|
||||
break;
|
||||
case 2:
|
||||
return main_multi();
|
||||
break;
|
||||
case 3:
|
||||
get_input<int>(probability_num, "每个时刻有1/{输入值}的概率来人");
|
||||
get_input<int>(speed, "每个窗口办理的速度");
|
||||
get_input<int_>(total_time, "总时刻数");
|
||||
get_input<int>(max_money, "最大携带金额");
|
||||
get_input<int>(window_num, "柜台数量");
|
||||
get_input<double>(walk_speed, "人的走路速度(浮点数)");
|
||||
get_input<int>(sleep_time, "每次刷新间隔多少毫秒");
|
||||
get_input<unsigned>(seed, "随机数种子");
|
||||
srand(seed);
|
||||
return main_multi();
|
||||
break;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
Reference in New Issue
Block a user