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()
{}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/149339.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【RocketMQ入门】

RocketMQ入门 RocketMQ是阿里巴巴开源的分布式消息中间件&#xff0c;现在是Apache的一个顶级项目。在阿里内部使用非常 广泛&#xff0c;已经经过了"双11"这种万亿级的消息流转。 RocketMQ环境搭建 接下来我们先在linux平台下安装一个RocketMQ的服务 环境准备 …

C++ Mean Shift算法

原理 每个样本点最终会移动到核概率密度的峰值&#xff0c;移动到相同峰值的样本点属于同一种颜色 关键代码 template <typename PointType> inline typename MeanShift<PointType>::PointsVector MeanShift<PointType>::meanshift(const PointsVector &am…

解决ArmDS Fast Models 中部分内核无法上电的问题

【更多软件使用问题请点击亿道电子官方网站】 1、 文档目标 解决ArmDS Fast Models 中部分内核无法上电的问题。 2、 问题场景 在调用ArmDS的Fast Models中的Cortex-A55的模型&#xff0c;只有Core 0是上电状态&#xff0c;而Core 1处于掉电状态&#xff0c;如图2-1所示&…

Ubuntu 24.04.1 LTS 安装 node 16.20.2环境

目录 step1&#xff1a;确认版本 step2&#xff1a;选择方式 step3&#xff1a;二进制文件安装 step1&#xff1a;确认版本 不同的版本情况可能有稍许不同&#xff0c;尽可能环境安装前版本保持一致&#xff1b; lsb_release -a 或者 cat /etc/os-release 可以查看版本信…

jmeter本身常用性能优化方法

第一种设置&#xff1a; 修改Jmeter.bat文件&#xff0c;调整JVM参数(修改jmeter本身的最小最大堆内存)&#xff0c;默认都是1个G set HEAP-Xms5g -Xmx5g -XX:MaxMetaspaceSize256m我的本机内存是8G&#xff0c;那最大可以设置870%(本机内存的70%)5.6g 这里我设置的5g 如果…

Golang | Leetcode Golang题解之第题432题全O(1)的数据结构

题目&#xff1a; 题解&#xff1a; type node struct {keys map[string]struct{}count int }type AllOne struct {*list.Listnodes map[string]*list.Element }func Constructor() AllOne {return AllOne{list.New(), map[string]*list.Element{}} }func (l *AllOne) Inc(ke…

链表分割-----------lg

现有一链表的头指针 ListNode* pHead&#xff0c;给一定值x&#xff0c;编写一段代码将所有小于x的结点排在其余结点之前&#xff0c;且不能改变原来的数据顺序&#xff0c;返回重新排列后的链表的头指针。 我们可以假设x为36&#xff0c;则小于36都排在前边&#xff0c;>3…

html+css(交河故城css)

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>故城</title><link rel"stylesheet&q…

css实现类似歌词字体渐变的效果

1、HTML <view class"title">哈哈哈哈哈</view> 2、CSS animation: hue 6s infinite linear;background-image: linear-gradient(135deg, #fc00c7 0%, #1c4efd 54%, #00aded 100%);-webkit-text-fill-color: transparent;color: transparent;-webkit-ba…

使用jQuery处理Ajax

使用jQuery处理Ajax HTTP协议 超文本传输协议&#xff08;HTTP&#xff0c;HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议 设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法 所有的WWW文件都必须遵守这个标准 一次HTTP操作称为一个事务&am…

Pandas -----------------------基础知识(三)

dataframe添加列 删除行与列去重修改列自定义函数 加载数据 import pandas as pd# 加载数据集 ./data/b_LJdata.csv df pd.read_csv(./data/b_LJdata.csv).head(10) df dataframe添加列 在后面追加 df[城市] 北京 df df[区县] [朝阳区, 朝阳区, 西城区, 昌平区, 朝阳区,朝阳…

2024年中国研究生数学建模竞赛B题 (WLAN组网吞吐量机理建模+GBDT+LSTM 进阶建模文章)

2024年中国研究生数学建模竞赛B题 (WLAN组网吞吐量机理建模GBDTLSTM 进阶建模文章) 全文请从 底部名片 处获取哦~ 问题重述和分析 问题重述 本题旨在基于实际测量的WLAN&#xff08;无线局域网&#xff09;数据&#xff0c;建立一个精确的系统吞吐量预测模型。具体而言&…

计算机毕业设计 校园志愿者管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

示例说明:分布式系统实战经验

在分布式系统中&#xff0c;实战经验是非常宝贵的&#xff0c;因为它涉及到系统的高可用性、数据一致性、性能优化等多个方面。以下是一些具体的实战经验举例&#xff1a; 1. 高可用性设计 案例: 在电商系统中&#xff0c;订单服务需要保证高可用性。为了防止单点故障&#x…

kettle发邮件:使用Kettle实现自动化发信?

kettle发邮件配置教程&#xff1f;Kettle如何实现邮件发送功能&#xff1f; Kettle&#xff0c;作为一款强大的开源ETL工具&#xff0c;不仅在数据转换和加载方面表现出色&#xff0c;还能通过其内置的功能实现自动化发邮件。AokSend将详细探讨如何使用Kettle发邮件&#xff0…

【LLM多模态】文生视频综述From Sora What We Can See: A Survey of Text-to-Video Generation

note 现在很多主流的文生视频应该还是Diffusion-based 基于扩散模型的方法这篇综述将现有研究按照三个维度进行分类&#xff1a;进化生成器&#xff08;Evolutionary Generators&#xff09;、卓越追求&#xff08;Excellent Pursuit&#xff09;、现实全景&#xff08;Realis…

在IntelliJ IDEA中设置文件自动定位

当然&#xff0c;以下是一个整理成博客格式的内容&#xff0c;关于如何在IntelliJ IDEA中设置文件自动定位功能。 在IntelliJ IDEA中设置文件自动定位 背景 最近由于公司项目开发的需求&#xff0c;我从VSCode转到了IntelliJ IDEA。虽然IDEA提供了许多强大的功能&#xff0c;…

【机器学习】——支持向量机

文章目录 支持向量机&#xff08;Support Vector Machine, SVM&#xff09;概述SVM 的工作原理线性不可分数据&#xff1a;软间隔与核技巧SVM 的数学形式SVM 的优势SVM 的缺点SVM 的应用 支持向量机&#xff08;Support Vector Machine, SVM&#xff09;概述 支持向量机&#…

Linux基本命令总结(佛系更)

目录 前言 一、Linux的基本目录 目录&#xff1a; 前缀&#xff1a; 二、Linux基本命令 文件相关 1.pwd&#xff1a; 2.ls [option] [file]: 3.cd [xxx]: 4.mkdir [option] directory: 5.rmdir [option] directory: rm 6.touch file: 7.cp&#xff1a; 8.mv&…

0-1开发自己的obsidian plugin DAY 1

官网教程有点mismatch&#xff0c;而且从0-100跨度较大&#xff0c;&#x1f4dd;记录一下自己的踩坑过程 首先&#xff0c;官网给的example里只有main.ts&#xff0c;需要自己编译成main.js 在视频教程&#xff08;https://www.youtube.com/watch?v9lA-jaMNS0k&#xff09;里…