C++读取txt文件中的句子在终端显示,同时操控鼠标滚轮(涉及:多线程,产生随机数,文件操作等)
文章目录
- 运行效果
- 功能描述
- 代码
- mian.cpp
- include
- MouseKeyControl.h
- TipsManagement.h
- src
- MouseControl.cpp
- TipsManagement.cpp
运行效果

功能描述
线程一:每隔n+随机秒,动一下鼠标滚轮,防止屏幕息屏。
线程二:运行时加载txt文件中的句子到数组,然后每隔n秒随机显示一个句子。
代码
mian.cpp
#include<iostream>
#include"TipsManagement.h"
#include"MouseKeyControl.h"
#include <thread>
using namespace std;// 声明两个线程函数,以便可以独立于 main 函数运行
void showTipsInThread(TipsManagement* tm) { while (true) {tm->randomShowTip(); }
} void autoRollMouseInThread(MouseKeyControl* mc)
{while (true) { mc->autoScrollMouseWheel();}
} int main()
{TipsManagement* tm = new TipsManagement();MouseKeyControl* mc = new MouseKeyControl();tm->n = 2 * 60 * 1000;mc->n = 3 * 60 * 1000;;thread tipsThread(showTipsInThread, tm); thread mouseThread(autoRollMouseInThread, mc); // 等待线程完成 tipsThread.join(); mouseThread.join(); // delete tm;// delete mc;return 0;
}
include
MouseKeyControl.h
#pragma once
#include<iostream>
#include<string>
#include <windows.h>
#include<fstream>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;class MouseKeyControl
{
public:unsigned int n; RECT screenRect; // get screen size;INPUT inputs[2] = {};int delta;int offsets[10] = {-3240,-3010,-2030,-1003,0,0,-998,-817,-603,-710};void initRandomIndexVec();void autoRandomMoveMouse();void autoScrollMouseWheel();MouseKeyControl();~MouseKeyControl();
};
TipsManagement.h
#pragma once
#include<iostream>
#include<string>
#include <windows.h>
#include<fstream>
#include <algorithm>
#include <random>
#include <ctime>
#define FILENAME "tipsFile.txt"
using namespace std;class TipsManagement
{
public:bool fileIsEmpty;int tipsNum;string* tipsArr;vector<int> randomIndexVec;unsigned int n; // sleep n minutes;void showMenu();void initTips();void initRandomIndexVec();void randomShowTip();int getTipsNum();void showAllTips();void cleanFile();void save();void addTips();void findTips();void deleteTips();void modifyTips();void sortTips();void exitSystem();string GbkToUtf8(string src_str1);string Utf8ToGbk(string src_str1);TipsManagement();~TipsManagement();
};
src
MouseControl.cpp
#include "MouseKeyControl.h"
#include <windows.h> MouseKeyControl::MouseKeyControl()
{SystemParametersInfo(SPI_GETWORKAREA, 0, &this->screenRect, 0);// 创建一个包含两个INPUT结构体的数组,一个用于按下滚轮,一个用于释放滚轮 this->delta = 1;ZeroMemory(this->inputs, sizeof(this->inputs)); // 第一个INPUT结构体:滚轮滚动(向下滚动为正,向上滚动为负) this->inputs[0].type = INPUT_MOUSE; this->inputs[0].mi.dx = 0; this->inputs[0].mi.dy = 0; this->inputs[0].mi.mouseData = delta * WHEEL_DELTA; // WHEEL_DELTA是滚动一个“滴答”的量 this->inputs[0].mi.dwFlags = MOUSEEVENTF_WHEEL; // 指定这是一个滚轮事件 this->inputs[0].mi.time = 0;
}void MouseKeyControl::autoRandomMoveMouse()
{// 初始化随机数种子 // srand(static_cast<unsigned int>(time(0))); Sleep(this->n); // 生成随机位置 int x = rand() % (this->screenRect.right - this->screenRect.left); int y = rand() % (this->screenRect.bottom - this->screenRect.top); // 将鼠标移动到随机位置 SetCursorPos(x + this->screenRect.left, y + this->screenRect.top); }void MouseKeyControl::autoScrollMouseWheel() { // 根据当前时间初始化srand(static_cast<unsigned int>(time(0))); // 生成0到10的随机整数(包含0但不包含10) int randomNum = rand() % 10; Sleep(this->n+this->offsets[randomNum]);// 正数表示向上滚动SendInput(1, this->inputs, sizeof(INPUT));
} MouseKeyControl::~MouseKeyControl()
{
}
TipsManagement.cpp
#include "TipsManagement.h"TipsManagement::TipsManagement()
{this->n = 3000; // the default sleep time is 3s.ifstream ifs;ifs.open(FILENAME, ios::in);//-----------------------1.file is not exist-------------------------if (!ifs.is_open()){cout << "The file does not exist!" << endl;// the label of the empty file this->fileIsEmpty = true;// label the tip number to 0this->tipsNum = 0;// set the tip Array is Null.this->tipsArr = NULL;ifs.close(); return;}//----------------2.file is exist, but the data is NULL.-------------------char ch;ifs >> ch; // read a char in the file.if (ifs.eof()) // eof is the end lable of the file.{cout << "The file is empty!" << endl;this->tipsNum = 0; this->fileIsEmpty = true; this->tipsArr = NULL; ifs.close();return;}//---------------------3.file is exist and the data are not null.-------------------------int num = this->getTipsNum();// cout << "the file have " << num << " tips." << endl;this->tipsNum = num; this->fileIsEmpty = false;this->tipsArr = new string[this->tipsNum];this->initTips(); // read the file tips to tipsArr// this->showAllTips();this->initRandomIndexVec();// create the random vec index}void TipsManagement::initRandomIndexVec()
{for (int i = 0; i < this->tipsNum; i++){this->randomIndexVec.push_back(i);}random_device rd; mt19937 g(rd()); shuffle(begin(this->randomIndexVec), end(this->randomIndexVec), g);
}void TipsManagement::randomShowTip()
{int index;for (int i = 0; i < this->tipsNum; i++){index = this->randomIndexVec[i];cout<<endl;cout<< this->tipsArr[index]<<endl;Sleep(this->n);system("cls");}}// when open the tips file, read all tips to tipsArr.
void TipsManagement::initTips()
{ifstream ifs;ifs.open(FILENAME, ios::in);string tip;int index = 0;while (ifs >> tip){this->tipsArr[index] = tip;index++;}ifs.close();
}// read tips file to get the number of tips.
int TipsManagement::getTipsNum()
{ifstream ifs;ifs.open(FILENAME, ios::in);string tip;int num = 0;while (ifs >> tip){num++;}ifs.close();return num;
}void TipsManagement::showAllTips()
{if (this->fileIsEmpty){cout << "File is not exist or the file is empty!" << endl;}else{for (int i = 0; i < this->tipsNum; i++){cout<<this->tipsArr[i]<<endl;}}system("pause");system("cls");
}//------------------------------------------------useless---------------------------------------------------void TipsManagement::showMenu()
{cout << "**********************************************" << endl;cout << "************0.Exit tipsManagement.*************" << endl;cout << "************1.Add tip.*******************" << endl;cout << "************2.Show tips.******************" << endl;cout << "************3.Delete someone tip.******" << endl;cout << "************4.Modify tip.****" << endl;cout << "************5.Find tip.******" << endl;cout << "************6.Sort by id.*****************" << endl;cout << "************7.Clear all documents*************" << endl;cout << "**********************************************" << endl;cout << endl;
}void TipsManagement::addTips()
{cout << "Please enter the number of tips to be added:"<<endl;int addNum = 0;cin >> addNum;if (addNum > 0){int newSize = this->tipsNum + addNum;string* newSpace = new string[newSize];if (this->tipsArr != NULL){for (int i = 0; i < this->tipsNum; i++){newSpace[i] = this->tipsArr[i];}}for (int i = 0; i < addNum; i++){string tip;cout << "Please enter the " << i + 1 << " tip:" << endl;cin >> tip; cout<<"new input:"<<tip<<endl;newSpace[this->tipsNum + i] = tip;cout<<"new input(arr):"<<newSpace[this->tipsNum + i]<<endl;}delete[] this->tipsArr;this->tipsArr = newSpace;this->tipsNum = newSize;this->fileIsEmpty = false;cout << "Successfully added " << addNum << " new tips!" << endl;this->save();}else{cout << "Your input is incorrect." << endl;}system("pause");system("cls");
}// put the tipsArr to file.
void TipsManagement::save()
{ofstream ofs;ofs.open(FILENAME, ios::out);for (int i = 0; i < this->tipsNum; i++){ofs << this->tipsArr[i]<< endl;}ofs.close();
}void TipsManagement::exitSystem()
{cout << "exit." << endl;system("pause");exit(0);
}string TipsManagement::GbkToUtf8(string src_str1)
{const char *src_str = src_str1.data();int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);wchar_t* wstr = new wchar_t[len + 1];memset(wstr, 0, len + 1);MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);char* str = new char[len + 1];memset(str, 0, len + 1);WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);std::string strTemp = str;if (wstr) delete[] wstr;if (str) delete[] str;return strTemp;
}string TipsManagement::Utf8ToGbk(string src_str1) //const char *src_str
{const char* src_str = src_str1.data();int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);wchar_t* wszGBK = new wchar_t[len + 1];memset(wszGBK, 0, len * 2 + 2);MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);char* szGBK = new char[len + 1];memset(szGBK, 0, len + 1);WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);std::string strTemp(szGBK);if (wszGBK) delete[] wszGBK;if (szGBK) delete[] szGBK;return strTemp;
}TipsManagement::~TipsManagement()
{}
相关文章:
C++读取txt文件中的句子在终端显示,同时操控鼠标滚轮(涉及:多线程,产生随机数,文件操作等)
文章目录 运行效果功能描述代码mian.cppincludeMouseKeyControl.hTipsManagement.h srcMouseControl.cppTipsManagement.cpp 运行效果 功能描述 线程一:每隔n随机秒,动一下鼠标滚轮,防止屏幕息屏。 线程二:运行时加载txt文件中的…...
Android 中使用高德地图实现根据经纬度信息画出轨迹、设置缩放倍数并定位到轨迹路线的方法
一、添加依赖和权限 在项目的build.gradle文件中添加高德地图的依赖: implementation com.amap.api:maps:latest_version在AndroidManifest.xml文件中添加必要的权限: <uses-permission android:name"android.permission.ACCESS_FINE_LOCATIO…...
LeetCode从入门到超凡(二)递归与分治算法
引言 大家好,我是GISer Liu😁,一名热爱AI技术的GIS开发者。本系列文章是我跟随DataWhale 2024年9月学习赛的LeetCode学习总结文档;在算法设计中,递归和分治算法是两种非常重要的思想和方法。它们不仅在解决复杂问题时表…...
superset 解决在 mac 电脑上发送 slack 通知的问题
参考文档: https://superset.apache.org/docs/configuration/alerts-reports/ 核心配置: FROM apache/superset:3.1.0USER rootRUN apt-get update && \apt-get install --no-install-recommends -y firefox-esrENV GECKODRIVER_VERSION0.29.0 RUN wget -q https://g…...
SQL_UNION
在 SQL 中使用 UNION 操作符时,被联合的两个或多个 SELECT 语句的列数必须相同,并且相应的列数据类型也需要兼容。这是因为 UNION 操作符会将结果组合成单个结果集,每个 SELECT 语句的结果行将按顺序放置在结果集中。 例如,如果你…...
高等代数笔记(2)————(弱/强)数学归纳法
数学归纳法的引入情景其实很简单,就是多米诺骨牌。 推倒所有多米诺骨牌的关键就是推倒第一块,以及确保第一块倒下后会带动第二块,第二块带动第三块,以此类推,也就是可以递推。由此我们可以归纳出所有的多米诺骨牌都可…...
模拟自然的本质:与IBM量子计算研究的问答
量子计算可能是计算领域的下一个重大突破,但它的一般概念仍然处于炒作和猜测的现状?它能破解所有已知的加密算法吗?它能设计出治愈所有疾病的新分子吗?它能很好地模拟过去和未来,以至于尼克奥弗曼能和他死去的儿子说话…...
Robot Operating System——带有时间戳和坐标系信息的多边形信息
大纲 应用场景1. 机器人导航场景描述具体应用 2. 环境建模场景描述具体应用 3. 路径规划场景描述具体应用 4. 无人机飞行控制场景描述具体应用 5. 机械臂运动控制场景描述具体应用 6. 自动驾驶车辆控制场景描述具体应用 定义字段解释 案例 geometry_msgs::msg::PolygonStamped …...
内网穿透(当使用支付宝沙箱的时候需要内网穿透进行回调)
内网穿透 一、为什么要使用内网穿透: 内网穿透也称内网映射,简单来说就是让外网可以访问你的内网:把自己的内网(主机)当做服务器,让外网访问 二、安装路由侠 路由侠-局域网变公网 (luyouxia.com) 安装成功如下: 三…...
Contact Form 7最新5.9.8版错误修复方案
最近有多位用户反应Contact Form 7最新5.9.8版的管理页面有错误如下图所示 具体错误文件的路径为wp-content\plugins\contact-form-7\admin\includes\welcome-panel.php on line 153 找到welcome-panel.php这个文件编辑它,将如下图选中的部分删除 删除以后…...
【第十一章:Sentosa_DSML社区版-机器学习之分类】
目录 11.1 逻辑回归分类 11.2 决策树分类 11.3 梯度提升决策树分类 11.4 XGBoost分类 11.5 随机森林分类 11.6 朴素贝叶斯分类 11.7 支持向量机分类 11.8 多层感知机分类 11.9 LightGBM分类 11.10 因子分解机分类 11.11 AdaBoost分类 11.12 KNN分类 【第十一章&…...
kafka3.8的基本操作
Kafka基础理论与常用命令详解(超详细)_kafka常用命令和解释-CSDN博客 [rootk1 bin]# netstat -tunlp|grep 90 tcp6 0 0 :::9092 :::* LISTEN 14512/java [rootk1 bin]# ./kafka-topics.s…...
如何检测并阻止机器人活动
恶意机器人流量逐年增加,占 2023 年所有互联网流量的近三分之一。恶意机器人会访问敏感数据、实施欺诈、窃取专有信息并降低网站性能。新技术使欺诈者能够更快地发动攻击并造成更大的破坏。机器人的无差别和大规模攻击对所有行业各种规模的企业都构成风险。 但您的…...
《linux系统》基础操作
二、综合应用题(共50分) 随着云计算技术、容器化技术和移动技术的不断发展,Unux服务器已经成为全球市场的主导者,因此具备常用服务器的配置与管理能力很有必要。公司因工作需要,需要建立相应部门的目录,搭建samba服务器和FTP服务器,要求将销售部的资料存放在samba服务器…...
EMT-LTR--学习任务间关系的多目标多任务优化
EMT-LTR–学习任务间关系的多目标多任务优化 title: Learning Task Relationships in Evolutionary Multitasking for Multiobjective Continuous Optimization author: Zefeng Chen, Yuren Zhou, Xiaoyu He, and Jun Zhang. journal: IEE…...
MySQL record 08 part
数据库连接池: Java DataBase Connectivity(Java语言连接数据库) 答: 使用连接池能解决此问题, 连接池,自动分配连接对象,并对闲置的连接进行回收。 常用的数据库连接池: 建立数…...
打造以太坊数据监控利器:InfluxDB与Grafana构建Geth可视化分析平台
前言 以太坊客户端收集大量数据,这些数据可以按时间顺序数据库的形式读取。为了简化监控,这些数据可以输入到数据可视化软件中。在此页面上,将配置 Geth 客户端以将数据推送到 InfluxDB 数据库,并使用 Grafana 来可视化数据。 一…...
对onlyoffice进行定制化开发
基于onlyoffice8.0源码,进行二次开发,可实现包括但不限于以下的功能 1、内容控件的插入 2、内容空间的批量替换 3、插入文本 4、插入图片 5、添加,去除水印 6、修改同时在线人数限制 7、内容域的删除 8、页面UI的定制化 9、新增插件开发 10、…...
使用llama.cpp 在推理MiniCPM-1.2B模型
llama.cpp 是一个开源项目,它允许用户在C中实现与LLaMA(Large Language Model Meta AI)模型的交互。LLaMA模型是由Meta Platforms开发的一种大型语言模型,虽然llama.cpp本身并不包含LLaMA模型的训练代码或模型权重,但它…...
分布式环境中,接口超时重试带来的的幂等问题如何解决?
目录标题 幂等不能解决接口超时吗?幂等的重要性什么是幂等?为什么需要幂等?接口超时了,到底如何处理? 如何设计幂等?幂等设计的基本流程实现幂等的8种方案1.selectinsert主键/唯一索引冲突(常用)2.直接insert 主键…...
别再死记硬背base64了!深入浅出聊聊CTF中那些‘魔改’编码的识别与对抗思路
CTF逆向工程中的编码魔法:从Base64变异到通用对抗策略 在网络安全竞赛的战场上,编码就像是一把双刃剑——它既是保护信息的盾牌,也是隐藏线索的迷雾。对于CTF逆向选手而言,面对各种"魔改"编码就像是在解谜题时突然发现规…...
Unity3d之随机生成数字
UnityEngine.Random.Range(min,max)包含最小值不包含最大值Mathf.Clamp是限定范围...
别再让一条宽带拖慢整个公司!手把手教你用H3C防火墙配置双WAN口负载均衡(附HCL模拟器配置)
中小企业网络优化实战:H3C防火墙双WAN负载均衡配置指南 当视频会议频繁卡顿、文件传输速度像蜗牛爬行时,单条宽带已成为制约企业效率的瓶颈。对于50-200人规模的中小企业,双WAN负载均衡技术能以极低成本实现带宽翻倍,本文将用一台…...
别再只盯着原理图了!FPGA/SoC硬件工程师必看的RGMII接口PCB布线实战指南(含时序约束与等长规则)
RGMII接口PCB设计实战:从时序规范到千兆以太网稳定通信 在FPGA和SoC硬件开发中,RGMII接口设计一直是工程师们又爱又恨的挑战。爱它的简洁高效——相比GMII接口减少了近一半的引脚数量;恨它的时序敏感——一个看似微小的PCB布线失误就可能导致…...
ARM9老开发板救星:用BusyBox 1.7.0和4.3.2工具链构建根文件系统(避坑实录)
ARM9开发板重生指南:BusyBox 1.7.0与4.3.2工具链的黄金组合 当一块尘封多年的ARM9开发板重新出现在你面前,那种感觉就像考古学家发现了一件珍贵的文物。S3C2440这类老将虽然性能比不上现代Cortex-A系列,但在教学、工业控制等领域依然有不可替…...
用HyperLynx VX2.5做LPDDR4X与高速串行总线仿真的完整工作流
HyperLynx VX2.5实战:LPDDR4X与高速串行总线仿真全流程解析 在当今高速电路设计领域,信号完整性问题已成为制约产品性能的关键瓶颈。尤其对于搭载LPDDR4X内存和高速串行总线的移动设备与服务器,工程师们常常陷入这样的困境:设计阶…...
告别内网穿透:用IPv6+阿里云DNS搭建你的第一个家庭NAS(保姆级避坑指南)
告别内网穿透:用IPv6阿里云DNS搭建你的第一个家庭NAS(保姆级避坑指南) 家里有一台闲置的旧电脑想改造成NAS?厌倦了内网穿透工具的速度限制和复杂配置?其实你只需要一个IPv6地址和域名解析服务,就能让家庭NA…...
备战蓝桥杯国赛【Day 17】
📌 写在前面:今天的4道题全部来自蓝桥杯真题,,核心考点包括:贪心策略排序、自定义比较器、差分思想、前缀和贪心选择。这些题目看似简单,但暗藏陷阱,是检验"代码实现能力"和"思维…...
告别论文焦虑:百考通AI,让你的本科毕业论文像“闯关升级”一样简单
又到了一年毕业季,对于广大本科生而言,那座名为“毕业论文”的大山,是否又一次压得你喘不过气?面对空白的Word文档,你是否感到无从下手?导师的催促、复杂的格式、浩如烟海的文献、以及令人心慌的查重……这…...
Perplexity新闻搜索失效真相:LLM缓存机制、地域策略与时间戳偏移的三重干扰(内部技术备忘录节选)
更多请点击: https://codechina.net 第一章:Perplexity新闻资讯搜索 Perplexity 是一款以实时性、引用可追溯性和多源聚合为特色的 AI 搜索工具,其“新闻资讯搜索”功能专为技术从业者与研究人员设计,支持按时间范围、可信信源&a…...
