C++读取txt文件中的句子在终端显示,同时操控鼠标滚轮(涉及:多线程,产生随机数,文件操作等)

server/2024/9/24 7:13:32/

文章目录

  • 运行效果
  • 功能描述
  • 代码
    • 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.ppmy.cn/server/121230.html

相关文章

基于OpenMV和STM32的车内温度监测与预警系统设计流程

一、项目概述 随着汽车普及率的增加&#xff0c;车内安全问题日益受到关注。近年来&#xff0c;婴儿甚至成人被锁在车内而导致中暑或晕倒的事件频频发生&#xff0c;严重威胁到人们的生命安全。因此&#xff0c;开发一个有效的车内温度预警系统显得尤为重要。本项目旨在设计一…

springboot 控制器

springboot 控制器 文章目录 springboot 控制器1.Controller和RestController**Controller&#xff1a;数据和页面****RestController&#xff1a;数据**所以我们的controller一般在springmvc中使用&#xff0c;返回页面&#xff0c;但是现在的项目基本上都是前后端分离项目&am…

GitLab 批量创建用户

GitLab 批量创建用户 引言 在团队管理中&#xff0c;经常需要为多个新成员创建 GitLab 账户。手动创建每个账户不仅耗时&#xff0c;而且容易出错。为了提高效率&#xff0c;我们可以编写一个简单的 Bash 脚本来批量创建用户。本文将详细介绍如何使用 GitLab API 和 Bash 脚本…

win10 win11 设置文件权限以解决Onedrive不能同步问题

初级代码游戏的专栏介绍与文章目录-CSDN博客 我的github&#xff1a;codetoys&#xff0c;所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。 这些代码大部分以Linux为目标但部分代码是纯C的&#xff0c;可以在任何平台上使用。 源码指引&#xff1a;github源…

使用llama.cpp 在推理MiniCPM-1.2B模型

llama.cpp 是一个开源项目&#xff0c;它允许用户在C中实现与LLaMA&#xff08;Large Language Model Meta AI&#xff09;模型的交互。LLaMA模型是由Meta Platforms开发的一种大型语言模型&#xff0c;虽然llama.cpp本身并不包含LLaMA模型的训练代码或模型权重&#xff0c;但它…

前端大屏自适应方案

一般后台管理页面&#xff0c;需要自适应的也就是大屏这一个&#xff0c;其他的尺寸我感觉用第三方框架继承好的就挺合适的&#xff0c;当然自适应方案也可以同步到所有页面&#xff0c;但我感觉除了 to c 的项目&#xff0c;不太需要所有页面自适应&#xff0c;毕竟都是查看和…

C++继承(上)

1.继承的概念 继承是一个类继承另外一个类&#xff0c;称继承的类为子类/派生类&#xff0c;被继承的类称为父类/基类。 比如下面两个类&#xff0c;Student和Person&#xff0c;Student称为子类&#xff0c;Person称为父类。 #include<iostream> using namespace std…

PhpStudy —— README

本教程为 PhpStudy 教程合集&#xff0c;有啥问题尽管留言笔者刚好完善一下。 0x01&#xff1a;PhpStudy 入门手册 0x0101&#xff1a;PhpStudy 简介0x0102&#xff1a;PhpStudy 安装0x0103&#xff1a;PhpStudy PHP 版本切换流程 0x02&#xff1a;PhpStudy 问题解决