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

news2024/9/24 7:40:16

文章目录

  • 运行效果
  • 功能描述
  • 代码
    • 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 0
        this->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.coloradmin.cn/o/2159811.html

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

相关文章

前端html+css+js 基础总结

​​​HTML 行级元素 标签分为行级元素与块级元素 行级元素占据区域由其显示内容决定&#xff0c;如span&#xff0c;img(图片)&#xff0c;<a></a>基本格式: <a href"链接" target"_blank"></a>用于跳转到其他网站&#xff0c…

蓝桥杯1.小蓝的漆房

样例输入 2 5 2 1 1 2 2 1 6 2 1 2 2 3 3 3样例输出 1 2 import math import os import sys tint(input())#执行的次数 for j in range(t):n,kmap(int,input().split())#n为房间数 k为一次能涂的个数alist(map(int,input().split()))#以列表的形式存放房间的颜色maxvaluemath…

环形缓冲区例子

即使使用中断函数或者定时器函数记录按键&#xff0c;如果只能记录一个键值的话&#xff0c;如果不能 及时读走出来&#xff0c;再次发生中断时新值就会覆盖旧值。要解决数据被覆盖的问题&#xff0c;可以使用 一个稍微大点的缓冲区&#xff0c;这就涉及数据的写入、读出&#…

闯关leetcode——69. Sqrt(x)

大纲 题目地址内容 解题代码地址 题目 地址 https://leetcode.com/problems/sqrtx/description/ 内容 Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You mu…

MySQL --基本查询(上)

文章目录 1.Create1.1单行数据全列插入1.2多行数据指定列插入1.3插入否则更新1.4替换 2.Retrieve2.1 select列2.1.1全列查询2.1.2指定列查询2.1.3查询字段为表达式2.1.4 为查询结果指定别名2.1.5结果去重 2.2where 条件2.2.1英语不及格的同学及英语成绩 ( < 60 )2.2.2语文成…

深度解读 2024 Gartner DevOps 魔力象限

上周 Gartner 刚发布了 2024 年度的 DevOps 魔力象限。我们也第一时间来深度解读一下这份行业里最权威的报告。 和2023年对比 23 年入围 14 家厂商&#xff0c;24 年入围 11 家。4 家厂商从报告中消失&#xff0c;分别是 Bitrise, Codefresh, Google Cloud Platform (GCP), VM…

调度_命令行_环境变量

linux的进程调度算法 饥饿问题 新建进程/时间片结束进程&#xff0c;若放回active&#xff0c;很可能该进程优先级太高&#xff0c;下一个还是执行该进程&#xff0c;导致不断执行同一进程&#xff0c;各进程调度不均衡。 饥饿问题解决 新建进程不能到active&#xff0c;要到…

力扣 24.两两交换链表中的节点

力扣《反转链表》系列文章目录 刷题次序&#xff0c;由易到难&#xff0c;一次刷通&#xff01;&#xff01;&#xff01; 题目题解206. 反转链表反转链表的全部 题解192. 反转链表 II反转链表的指定段 题解224. 两两交换链表中的节点两个一组反转链表25. K 个一组翻转链表K …

《操作系统 - 清华大学》第一讲:操作系统概述 —— 学习内容概述

文章目录 1. 内容摘要2. 实验内容 1. 内容摘要 在这里对学习内容做一个整体上的介绍&#xff0c;那在这里包括我们要学习的内容&#xff0c;实验的内容。操作系统课涉及到计算机系统当中的资源管理&#xff0c;所以我们围绕着操作系统的实现来介绍相关内容&#xff0c;那主要分…

①大缓存ModbusRTU485数据集中采集器寄存器线圈重映射从站并发采集Modbus 串口RS485 转 RS485

大缓存ModbusRTU485数据集中采集器寄存器线圈重映射从站并发采集https://item.taobao.com/item.htm?ftt&id811821574300 产品型号&#xff1a; 一分一路 MS-A1-C011 一分2路 MS-A1-C021 一分4路 MS-A1-C041 一分7路 MS-A1-C071 一般技术规格 1.串口 MS-A1…

【hot100-java】【最长公共子序列】

R8-多维dp篇 直接上递推 class Solution {public int longestCommonSubsequence(String text1, String text2) {char[] stext1.toCharArray(); char[] ttext2.toCharArray(); int ns.length;int mt.length;int [][] fnew int[n1][m1];for (int i0;i<n;i){for (int j0;j<…

MySQL | 实战 | 4 种将数据同步到ES方案

文章目录 1. 前言2. 数据同步方案2.1 同步双写2.2 异步双写2.3 定时更新2.4 基于 Binlog 实时同步 3. 数据迁移工具选型3.1 Canal3.2 阿里云 DTS3.3 Databus3.4 Databus和Canal对比3.4 其它 4. 后记 上周听到公司新同事分享 MySQL 同步数据到 ES 的方案&#xff0c;发现很有意思…

02【Matlab系统辨识】白噪声

1.白噪声与有色噪声 1.1 白噪声(white noise) 系统辨识中所用到的数据通常都含有噪声。从工程实际出发&#xff0c;这种噪声往往可以视为具有有理谱密度的平稳随机过程。白噪声是一种最简单的随机过程&#xff0c;是由一系列不相关的随机变量组成的理想化随机过程。白噪声的数…

vue3 vxe-grid 通过数据库返回的列信息,生成columns,并且其中有一列是img类型,进行slots的格式化处理。

1、一般我们写死的列信息的时候&#xff0c;会这样定义&#xff1a; 2、然后我们在template里面&#xff0c;这样这样写slots格式化部分&#xff1a; 这样表格中就会展示出一张图片&#xff0c;并且&#xff0c;我们点击了可以查看大图。 3、那么我们从数据库中返回的列&#…

三菱FX5U PLC故障处理(各种出错的内容、原因及处理方法进行说明。)

对使用系统时发生的各种出错的内容、原因及处理方法进行说明。 故障排除的步骤 发生故障时&#xff0c;按以下顺序实施故障排除。 1.确认各模块是否正确安装或正确配线。 2、确认CPU模块的LED。 3.确认各智能功能模块的LED。(各模块的用户手册) 4、连接工程工具&#xff0c;启…

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…

分布式环境中,接口超时到底怎么处理?

目录标题 为什么会存在超时?如何应对可能发生的超时?1. 设置合理的超时时间2. 重试机制3. 熔断机制4. 监控和报警5. 日志记录6. 限流和降级7. 异步处理 以上总结 为什么会存在超时? 接口超时是分布式系统中常见的问题&#xff0c;其原因多种多样&#xff0c;涉及网络、服务…

深入探究PR:那些被忽视却超实用的视频剪辑工具

如果想要了解视频剪辑的工具&#xff0c;那一定听说过pr视频剪辑吧。如果你是新手其实我更推荐你从简单的视频剪辑工具入手&#xff0c;这次我就介绍一些简单好操作的视频剪辑工具来入门吧。 1.福晰视频剪辑 连接直达>>https://www.pdf365.cn/foxit-clip/ 这款工具操…

Docker 付费订阅价格大幅上调,免费订阅功能受限,云计算和安全产品有调整

云计算de小白 同时&#xff0c;免费的 Docker Personal 订阅将不再包含 Build Cloud 分钟数&#xff0c;支持范围将从三个 Scout 存储库变为仅一个&#xff0c;并且仅限于一个具有 2 GB 存储空间的私有 Docker Hub 容器注册存储库。 不过&#xff0c;Docker也对云计算和安全产…