您的位置:首页 > 教育 > 锐评 > 江苏企业网站排名优化_南宁网站推广费用_cms建站_网络搭建是干什么的

江苏企业网站排名优化_南宁网站推广费用_cms建站_网络搭建是干什么的

2025/1/10 16:17:10 来源:https://blog.csdn.net/weixin_45778713/article/details/144986840  浏览:    关键词:江苏企业网站排名优化_南宁网站推广费用_cms建站_网络搭建是干什么的
江苏企业网站排名优化_南宁网站推广费用_cms建站_网络搭建是干什么的

网上看到的设计要求:

基本效果已经实现,希望大家帮忙指点指点。

程序中的一些基本模块

程序处理中的一些流程图

程序运行结果如下图:

程序代码如下:

#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include <cassert>
#include <vector>
#include <queue>
#include <thread>
#include <atomic>
#include <chrono>
#include <sstream>
#include <iomanip>
#include <memory>
#include <mutex>#define DESTIME 100#define LOG(x)  std::cout << #x << std::endlint getRandomNum(int lnum, int rnum)
{// 设定随机数种子,使用当前时间std::random_device rd;std::mt19937 gen(rd());// 定义随机数分布范围,这里使用均匀分布std::uniform_int_distribution<> dis(lnum, rnum); // 例如,生成1到100之间的随机数// 生成随机数int random_number = dis(gen);return random_number;
}//HHMM时间添加int时间,返回HHMM格式时间
std::string addMinutesToTime(const std::string& timeStr, int minutesToAdd) {int hours, minutes;char delimiter;//解析输入时间std::istringstream iss(timeStr);if (!(iss >> std::setw(2) >> hours >> delimiter >> std::setw(2) >> minutes) || delimiter != '-'){throw std::invalid_argument("错误的时间格式,请输入HH-MM格式");}//将时间间隔加到分钟上minutes += minutesToAdd;hours += minutes / 60;hours %= 24;		//24小时取余minutes %= 60;	    //60分钟取余//格式化输出时间std::ostringstream oss;oss << std::setw(2) << std::setfill('0') << hours << "-"<< std::setw(2) << std::setfill('0') << minutes;return oss.str();
}//HHMM 格式时间数据转换为int类型数据
int numToHHMMtime(const std::string strTime)
{int timeNum = 0;char delimiter;int hours, minutes;std::istringstream iss(strTime);if (!(iss >> std::setw(2) >> hours >> delimiter >> std::setw(2) >> minutes) || delimiter != '-' || hours >= 24 || hours < 0 || minutes < 0 || minutes >= 60){throw std::invalid_argument("错误的时间,请输入正确的HH-MM格式数据");}minutes += (hours * 60);return minutes;
}//客户类
class Customer {
public://设置VIPvoid setVIP(bool vip);//设置进入时间void setEnterTime(const std::string time);//设置离开时间void setLeftTime(const std::string time);//设置排队号码void setQueueNumber(unsigned int num);//生成评分(传入满分,随机生成区间内分数)int getScore(int score);//判断是否是VIPbool isVIP() {return VIP;}//获取进入时间std::string getEnterTime() {return enterTime;}//获取离开时间std::string getLeftTime() {return leftTime;}//获取排队号码unsigned int getQueueNumber() {return queueNumber;}private:bool VIP = false;					//VIP标志std::string enterTime = " ";		//进入时间std::string leftTime = " ";			//离开时间unsigned int queueNumber = 0;		//排队号码
};//评分类
class Rating {
public://设置分数void setScore(int _num);//设置评分时间void setRatingTime(const std::string time);//获取分数int getScore() {return score;}//获取评分时间std::string getRatingTime() {return ratingTime;}private:std::string ratingTime = " ";		//评分时间int score = 0;				    //评分分数
};//窗口类
class CounterWindow {
public:CounterWindow(int num) :windowNum(num) {};void setVIP() {VIPflag = true;}//添加客户void addCustomer(Customer& customer);//移除客户(这里不考虑客户不排队了的情况)void removeCustomer() {//先移除VIP还是先移除普通用户。查询当前正在服务的VIP标志if (VipWorkFlag){vipCustomerQue.pop();}else {customerQue.pop();}}//添加打分void addGrade();//办理业务void conductBusiness();//VIP队列人数int getVipCustomQuenNum(){return vipCustomerQue.size();}//客户队列人数int getCustomeQuenNum(){return customerQue.size();}//设置处理业务耗时void setWorkTime(int workTime){windowTime = workTime;			    //设置窗口时间和工作时间相同}//停止业务处理void closeConduct() {					//关闭业务closeFlag = true;}//开始业务处理void openConduct() {					//开启业务closeFlag = false;}private://生成业务办理耗时void businessProcessTime();private:bool VIPflag = false;					//VIP窗口标志bool VipWorkFlag = false;				//VIP正在工作标识符bool closeFlag = false;					//停止营业标志int windowNum;							//窗口号std::queue<Customer> customerQue;		//客户队列std::queue<Customer> vipCustomerQue;	//VIP客户队列std::vector<Rating> ratingVec;			//分数容器int windowTime = 0;						//窗口时间
};class sysTime {
public:sysTime(const sysTime&) = delete;sysTime& operator = (const sysTime&) = delete;// 获取类的唯一实例static sysTime& getInstance() {static std::mutex mtx; // 用于线程安全的互斥锁std::lock_guard<std::mutex> lock(mtx); // 自动管理锁的获取和释放static sysTime instance; // 局部静态变量,线程安全地在第一次调用时初始化return instance;}//获取字符串格式时间std::string get_HHMM_time();//获取时间int getTime() {return timeNum;}private:sysTime() : workThread(&sysTime::timeLoop, this) {//检查是否启动成功if (!workThread.joinable()) {std::cerr << "线程启动失败" << std::endl;}std::cout << "时间线启动成功" << std::endl;}~sysTime() {end();if (workThread.joinable()) {workThread.join();}}void start() {timeflag.store(false);}void end() {timeflag.store(true);}//循环计时void timeLoop();private:std::atomic<bool> timeflag;std::thread workThread;				//时间计数工作线程int timeNum = 0;					//时间计数器
};//银行类
class Bank {
public://设置营业开始时间void setOpenTime(std::string time);//设置营业结束时间void setCloseTime(std::string time);//设置窗口数量void setNumWindow(int num);//进入客户void enterCustomer(Customer& customer);//打开所有线程void openAllThread();//等待关闭所有线程void joinAllThreads();private:CounterWindow* getShortWindow(bool isVIP);private:int businessOpenTime;							//营业开始时间int businessCloseTime; 							//营业结束时间std::vector<CounterWindow> counterWinVec;		//窗口维护容器std::vector<CounterWindow> counterWinVipVec;	//VIP窗口维护器bool openFlag = false;							//营业标志std::vector<std::shared_ptr<std::thread>> threads;		//线程组};class Menu {
public:void start();private:};int main()
{Menu menu;menu.start();return 0;
}void Customer::setVIP(bool vip)
{this->VIP = vip;
}void Customer::setEnterTime(const std::string time)
{this->enterTime = time;
}void Customer::setLeftTime(const std::string time)
{this->leftTime = time;
}void Customer::setQueueNumber(unsigned int num)
{this->queueNumber = num;
}int Customer::getScore(int score)
{//使用断言判断输入分数是否在区间内assert(score >= 1);// 设定随机数种子,使用当前时间std::random_device rd;std::mt19937 gen(rd());// 定义随机数分布范围,这里使用均匀分布std::uniform_int_distribution<> dis(1, score); // 例如,生成1到100之间的随机数// 生成随机数int random_number = dis(gen);return random_number;
}void Rating::setScore(int _num)
{this->score = _num;
}void Rating::setRatingTime(const std::string time)
{this->ratingTime = time;
}//添加客户
void CounterWindow::addCustomer(Customer& customer) {if (customer.isVIP()){vipCustomerQue.push(customer);}else {customerQue.push(customer);}
}void CounterWindow::addGrade()
{Rating rate;Customer customer;int scoreNum;		//分数if (VipWorkFlag){customer = vipCustomerQue.front();}else {customer = customerQue.front();}scoreNum = customer.getScore(10);			//10分满分进行评分rate.setScore(scoreNum);rate.setRatingTime(addMinutesToTime(sysTime::getInstance().get_HHMM_time(), windowTime));ratingVec.push_back(rate);int personNum = customerQue.size() + vipCustomerQue.size();std::cout << "评分是" << rate.getScore() << " 评分时间是" << rate.getRatingTime()  << " 窗口是" << windowNum << "排队人数是" << personNum <<std::endl;}void CounterWindow::conductBusiness()
{while (!closeFlag){int vipSize = vipCustomerQue.size();if (vipSize > 0) {						//判断当前窗口是否有VIP客户正在排队VipWorkFlag = true;				    //标志给VIP客户办理业务}else if (customerQue.size() > 0) {VipWorkFlag = false;}else {//两种条件都不满足,程序跳出continue;}//办理业务,这里使用线程睡眠一段时间模拟businessProcessTime();//客户评分,生成客户评分addGrade();//移除客户,移除队列里的客户removeCustomer();}
}void CounterWindow::businessProcessTime()
{int num = getRandomNum(5, 20);windowTime = num;setWorkTime(num);num = num * DESTIME;std::chrono::milliseconds desTime(num);std::this_thread::sleep_for(desTime);}//获取字符串格式时间
std::string sysTime::get_HHMM_time()
{int hours, minutes;//输入时间余多少分钟minutes = timeNum % 60;//输入时间换算为多少小时hours = timeNum / 60;hours %= 24;std::ostringstream oss;oss << std::setw(2) << std::setfill('0') << hours << "-"<< std::setw(2) << std::setfill('0') << minutes;return oss.str();
}void sysTime::timeLoop()
{start();while (!timeflag.load()){//使用chrono库定义时间间隔,这里使用100ms对应1sstd::chrono::milliseconds desTime(DESTIME);std::this_thread::sleep_for(desTime);timeNum++;}
}void Menu::start()
{// 使用单例sysTime& myTime = sysTime::getInstance();Bank bank;bank.setOpenTime("09-00");bank.setCloseTime("17-00");bank.setNumWindow(4);while (1){int num = getRandomNum(1, 10);		//1分钟到60分钟内进入一位顾客num = num * DESTIME;				//得到转换后的毫秒数std::chrono::milliseconds sleepTime(num);std::this_thread::sleep_for(sleepTime);if (myTime.getTime() >= 1440) {		//一天时间为1440分钟break;}Customer customer;bank.enterCustomer(customer);std::string strTime = myTime.get_HHMM_time();}bank.joinAllThreads();}void Bank::setOpenTime(std::string time)
{businessOpenTime = numToHHMMtime(time);
}void Bank::setCloseTime(std::string time)
{businessCloseTime = numToHHMMtime(time);
}void Bank::setNumWindow(int num)
{assert(num >= 2);//设置一个VIPCounterWindow cWindow(-1);cWindow.setVIP();counterWinVipVec.push_back(cWindow);for (int i = 0; i < num - 1; i++){CounterWindow window(i);counterWinVec.push_back(window);}
}void Bank::enterCustomer(Customer& customer)
{int nowTime = sysTime::getInstance().getTime();if (nowTime < businessOpenTime || nowTime > businessCloseTime){std::cout << "当前不在营业时间当前时间是" << sysTime::getInstance().get_HHMM_time() << std::endl;openFlag = false;if (openFlag)				//如果之前是营业的,关闭窗口营业线程,这就导致程序启动模拟要从营业前的时间段开始{openFlag = false;//关闭窗口线程joinAllThreads();		//等待所有的线程关闭}return;}else {if (!openFlag)				//如果之前是不在营业的,开启窗口营业线程{openFlag = true;//开启窗口线程openAllThread();		//开启所有的线程}}Customer customers;customers.setEnterTime(sysTime::getInstance().get_HHMM_time());//查找排队最短窗口CounterWindow* win = getShortWindow(customers.isVIP());win->addCustomer(customers);		//添加客户到排队窗口}void Bank::joinAllThreads()
{//关闭普通窗口for (auto it : counterWinVec){it.closeConduct();}//关闭VIP窗口for (auto it : counterWinVipVec){it.closeConduct();}for (auto& threadPtr : threads){if (threadPtr->joinable()){threadPtr->join();}}
}CounterWindow* Bank::getShortWindow(bool isVIP)
{CounterWindow* win, * vipWin;		//普通窗口和vip窗口if (!isVIP)			//不是VIP分支{//获取第一个队列的长度,然后以这个长度为基准int num = counterWinVec[0].getCustomeQuenNum();int counter = 0;for (int i = 0; i < counterWinVec.size(); i++){int tempNum = counterWinVec[i].getCustomeQuenNum();if (tempNum <= num){num = tempNum;counter = i;}}win = &counterWinVec[counter];return win;}else {				//是VIP分支int vipNum = counterWinVipVec[0].getVipCustomQuenNum();int vipCount = 0;for (int i = 0; i < counterWinVipVec.size(); i++){int tempNum = counterWinVipVec[i].getVipCustomQuenNum();if (tempNum <= vipNum){vipNum = tempNum;vipCount = i;}}vipWin = &counterWinVipVec[vipCount];			//获取VIP下标//获取第一个队列的长度,然后以这个长度为基准int num = counterWinVec[0].getCustomeQuenNum();int counter = 0;for (int i = 0; i < counterWinVec.size(); i++){int tempNum = counterWinVec[i].getVipCustomQuenNum();if (tempNum < num){num = tempNum;counter = i;}}win = &counterWinVec[counter];if (win->getVipCustomQuenNum() >= vipWin->getVipCustomQuenNum()){return vipWin;}else {return win;}}
}void Bank::openAllThread()
{//启动VIP窗口for (int i = 0; i < counterWinVipVec.size(); i++){auto threadPtr = std::make_shared<std::thread>([this, i]() {counterWinVipVec[i].openConduct();counterWinVipVec[i].conductBusiness();		//启动线程开启业务});threads.push_back(threadPtr);}//启动窗口for (int i = 0; i < counterWinVec.size(); i++){auto threadPtr = std::make_shared<std::thread>([this, i]() {counterWinVec[i].openConduct();counterWinVec[i].conductBusiness();			//启动线程业务});threads.push_back(threadPtr);}
}

        时间系统使用while循环实现的,sysTime类使用了懒汉单例模式,在菜单类调用中初始化。
有一些多线程之类的,智能指针、原子变量、没有上锁,因为没有库里面的读写锁,这里考虑到都是读取时间不会改变时间,就没有加锁。这里通过区间内获取随机数的方式取得一个时间间隔,模模拟顾客进入银行办理业务,Menu类中的start()函数中可以设置客流量的频率。
        练手的代码,有些地方没有处理好,比如程序一开始的时间转换函数,可以使用饿汉单例模式实现,(工具类不会占用很大资源吧)。总之,希望评论区多多指点。感谢(抱拳)。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com