C++ Исходник Cpp Console Renaming

Начинающий
Статус
Оффлайн
Регистрация
17 Сен 2023
Сообщения
6
Реакции[?]
4
Поинты[?]
4K
C++:
#include <windows.h>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <ctime>

void update() {
    const int stringL = 20;

    std::srand(static_cast<unsigned int>(std::time(nullptr)));

    wchar_t module[MAX_PATH];
    GetModuleFileName(nullptr, module, MAX_PATH);
    std::wstring ofn = module;

    while (true) {
        std::wstring rstring;
        const wchar_t Char[] = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        const int CLen = sizeof(Char) / sizeof(Char[0]) - 1;
        for (int i = 0; i < stringL; ++i) {
            rstring += Char[rand() % CLen];
        }

        size_t lsp = ofn.find_last_of(L"\\");
        std::wstring nfn = ofn.substr(0, lsp + 1) + rstring + L".exe";

        SetConsoleTitle(rstring.c_str());
        MoveFile(ofn.c_str(), nfn.c_str());

        std::this_thread::sleep_for(std::chrono::milliseconds(200)); // delay
    }
}
int main() {
    std::thread titleThread(update);

    titleThread.join();
    return 0;
}

 
Начинающий
Статус
Оффлайн
Регистрация
12 Апр 2021
Сообщения
106
Реакции[?]
12
Поинты[?]
6K
Ну и нахуя? Это сделает любой человек который сидит в плюсах больше 0.000001 секунды
/del бесполезно
 
Начинающий
Статус
Оффлайн
Регистрация
17 Сен 2023
Сообщения
6
Реакции[?]
4
Поинты[?]
4K
Ну и нахуя? Это сделает любой человек который сидит в плюсах больше 0.000001 секунды
/del бесполезно

я сделал это ради забавы и опубликовал это потому что подумал что некоторым людям это может принести пользу а не потому что это сложно
 
Забаненный
Статус
Оффлайн
Регистрация
20 Ноя 2023
Сообщения
107
Реакции[?]
33
Поинты[?]
32K
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
Учим язык.День первый.
 
I Want to Die in New Orleans
Участник
Статус
Оффлайн
Регистрация
10 Окт 2020
Сообщения
498
Реакции[?]
484
Поинты[?]
73K
better use std::mt19937

can be declared as constexpr

huge naming problems, some variable names look unclear as well

C++:
int main() {
    std::thread titleThread(update);

    titleThread.join();
    return 0;
}
could be
C++:
int main() {
    std::thread(update).join();

    return 0;
}
 
Начинающий
Статус
Оффлайн
Регистрация
17 Сен 2023
Сообщения
6
Реакции[?]
4
Поинты[?]
4K
better use std::mt19937


can be declared as constexpr

huge naming problems, some variable names look unclear as well


could be
C++:
int main() {
    std::thread(update).join();

    return 0;
}
i had it in my code before in a actual console program when i was updating it after the program ran to not cause lag lmao
 
Начинающий
Статус
Оффлайн
Регистрация
25 Июн 2021
Сообщения
14
Реакции[?]
1
Поинты[?]
1K
with best yougame practics (without jthread, only C++11 code :D)
C++:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <thread>
#include <chrono>
#include <random>

static std::wstring generateRandomString(int length) {
    const wchar_t Char[] = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const int CLen = sizeof(Char) / sizeof(Char[0]) - 1;

    std::wstring rstring;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, CLen - 1);

    for (int i = 0; i < length; ++i) {
        rstring += Char[dis(gen)];
    }

    return rstring;
}

static void update() {
    constexpr int stringL = 20;
    while (true) {
        SetConsoleTitle(generateRandomString(stringL).c_str());
        std::this_thread::sleep_for(std::chrono::milliseconds(200)); // delay
    }
}

int main() {
    std::thread(update).join();
    return 0;
}
 
Эксперт
Статус
Оффлайн
Регистрация
29 Мар 2021
Сообщения
1,541
Реакции[?]
578
Поинты[?]
13K
with best yougame practics (without jthread, only C++11 code :D)
C++:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <thread>
#include <chrono>
#include <random>

static std::wstring generateRandomString(int length) {
    const wchar_t Char[] L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
can't you construct this using a for loop and an 'a' char?
 
Начинающий
Статус
Оффлайн
Регистрация
10 Май 2023
Сообщения
60
Реакции[?]
3
Поинты[?]
3K
with best yougame practics (without jthread, only C++11 code :D)
C++:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <thread>
#include <chrono>
#include <random>

static std::wstring generateRandomString(int length) {
    const wchar_t Char[] = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const int CLen = sizeof(Char) / sizeof(Char[0]) - 1;

    std::wstring rstring;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, CLen - 1);

    for (int i = 0; i < length; ++i) {
        rstring += Char[dis(gen)];
    }

    return rstring;
}

static void update() {
    constexpr int stringL = 20;
    while (true) {
        SetConsoleTitle(generateRandomString(stringL).c_str());
        std::this_thread::sleep_for(std::chrono::milliseconds(200)); // delay
    }
}

int main() {
    std::thread(update).join();
    return 0;
}
Код:
#include <iostream>
#include <thread>
#include <chrono>
#include <random>

constexpr int STRING_LENGTH = 20;
constexpr int DELAY_MS = 200;

std::wstring generateRandomString(int length) {
    const wchar_t CHARACTERS[] = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const int CHARACTERS_COUNT = sizeof(CHARACTERS) / sizeof(CHARACTERS[0]) - 1;

    std::wstring result;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, CHARACTERS_COUNT - 1);

    for (int i = 0; i < length; ++i) {
        result += CHARACTERS[dis(gen)];
    }

    return result;
}

void updateTitle() {
    while (true) {
        SetConsoleTitle(generateRandomString(STRING_LENGTH).c_str());
        std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_MS));
    }
}

int main() {
    std::cout << "Чтобы выйти, нажмите Ctrl+C." << std::endl;

    std::thread(updateTitle).join();

    return 0;
}
clеan
 
Начинающий
Статус
Оффлайн
Регистрация
25 Июн 2021
Сообщения
14
Реакции[?]
1
Поинты[?]
1K
Код:
#include <iostream>
#include <thread>
#include <chrono>
#include <random>

constexpr int STRING_LENGTH = 20;
constexpr int DELAY_MS = 200;

std::wstring generateRandomString(int length) {
    const wchar_t CHARACTERS[] = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const int CHARACTERS_COUNT = sizeof(CHARACTERS) / sizeof(CHARACTERS[0]) - 1;

    std::wstring result;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, CHARACTERS_COUNT - 1);

    for (int i = 0; i < length; ++i) {
        result += CHARACTERS[dis(gen)];
    }

    return result;
}

void updateTitle() {
    while (true) {
        SetConsoleTitle(generateRandomString(STRING_LENGTH).c_str());
        std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_MS));
    }
}

int main() {
    std::cout << "Чтобы выйти, нажмите Ctrl+C." << std::endl;

    std::thread(updateTitle).join();

    return 0;
}
clеan
SetConsoleTitle don't include, have a nice day :roflanEbalo: change pls SetConsoleTitle(generateRandomString(STRING_LENGTH).c_str()); to std::cout << "\033]0;" << generateRandomString(STRING_LENGTH) << "\007";, thx
 
Начинающий
Статус
Оффлайн
Регистрация
25 Июн 2021
Сообщения
14
Реакции[?]
1
Поинты[?]
1K
using 'a' to populate the char array, because 'a'+1 is 'b', etc.

assuming en_US.UTF-8
C++:
#include <iostream>
#include <thread>
#include <chrono>
#include <random>

constexpr int STRING_LENGTH = 20;
constexpr int DELAY_MS = 200;

static std::string generateRandomString(int length) {
    constexpr char startChar = 'a';
    constexpr int CLen = 26; // Number of characters in the English alphabet

    std::string rstring;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, CLen - 1);

    for (int i = 0; i < length; ++i) {
        rstring += startChar + dis(gen);
    }

    return rstring;
}

static void updateTitle() {
    while (true) {
        std::cout << "\033]0;" << generateRandomString(STRING_LENGTH) << "\007";
        std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_MS));
    }
}

int main() {
    std::cout << "Press Ctrl+C to exit." << std::endl;

    std::thread(updateTitle).join();

    return 0;
}
Good luck :)
 
I Want to Die in New Orleans
Участник
Статус
Оффлайн
Регистрация
10 Окт 2020
Сообщения
498
Реакции[?]
484
Поинты[?]
73K
Сверху Снизу