jtbg.h 头文件使用说明书

jtbg.h 使用指南

一、概述

jtbg.h 是一个为 C++ 程序员设计的综合性 Windows 工具库,提供了丰富的功能模块,包括系统操作、文件处理、加密解密、数学计算、字符串处理、网络功能等。

版本信息:2.9[正式版]
最后更新:2025/12/06
编译器要求:支持 C++11

二、配置选项

1. 宏定义配置

// 启用所有功能(Windows环境)
#define JTBG_ALL

// 启用非Windows功能(跨平台基础功能)
#define JTBG_NOWINDOWS_ALL

// OJ环境配置
#define JTBG_ON_OJ

// 优化级别配置
#define O1  // O1优化
#define O2  // O2优化
// ... 其他优化级别

2. 模块开关

每个功能模块都可以独立启用:

#define JTBG_HASH        // 哈希函数
#define JTBG_INI         // INI文件处理
#define JTBG_MSGBOX      // 消息框
#define JTBG_KMP         // 字符串匹配
#define JTBG_KEY         // 键盘输入
#define JTBG_COL         // 控制台颜色和光标
#define JTBG_FILE        // 文件操作
// ... 更多模块

三、核心功能模块详解

1. 哈希函数模块 (JTBG_HASH)

MD5类

// MD5哈希计算
static string MD5::hash(const string &input);
参数:input - 要计算哈希的字符串
返回:32位十六进制MD5哈希值

字符串哈希

// 多项式哈希
unsigned long long pHash(const string& s);
参数:s - 输入字符串
返回:无符号长整型哈希值

// 前缀哈希(支持子串查询)
struct StringHash {
    StringHash(const string& s);
    unsigned long long getHash(int l, int r);
};
参数:l, r - 子串起始和结束位置(0-based,包含)

文件MD5

string FileMD5(string path);
参数:path - 文件路径
返回:文件内容的MD5哈希值,失败返回空字符串

2. 文件操作模块 (JTBG_FILE)

文件存在性检查

bool Isfileext(const string& filePath);
参数:filePath - 文件路径
返回:文件是否存在

Save结构体

struct Save {
    string path;      // 文件路径
    int errorlevel;   // 错误代码
    
    // 方法
    void setpt(string s);           // 设置路径
    void read_nol();               // 以文本模式打开读取
    void read_bin();               // 以二进制模式打开读取
    void save_nol();               // 以文本模式打开写入
    void save_app();               // 以追加模式打开
    void save_bin();               // 以二进制模式打开写入
    void save_stop();              // 关闭输出流
    void read_stop();              // 关闭输入流
};

通用文件检查

bool fileok(const string &filename);
参数:filename - 文件名
返回:文件是否可以正常打开

3. 控制台操作模块 (JTBG_COL)

输出控制

void say(string s, int t);
参数:s - 要输出的字符串,t - 字符间延迟(ms)

void paused(string s);
参数:s - 提示信息
作用:显示提示并等待用户按键

int ext(int num);
参数:num - 退出代码
作用:退出程序并显示信息

颜色控制

enum Color {
    BLACK = 0, BLUE = 1, GREEN = 2, CYAN = 3,
    RED = 4, MAGENTA = 5, BROWN = 6, LIGHT_GRAY = 7,
    DARK_GRAY = 8, LIGHT_BLUE = 9, LIGHT_GREEN = 10,
    LIGHT_CYAN = 11, LIGHT_RED = 12, LIGHT_MAGENTA = 13,
    YELLOW = 14, WHITE = 15
};

void scg(Color x, Color y);
void scg(int x, int y);
参数:x - 前景色,y - 背景色
作用:设置控制台文本颜色

光标和屏幕控制

void setcursor(int x, int y);
参数:x, y - 光标位置坐标

void clearCA(int x, int y, int width, int height);
参数:x, y - 起始位置,width, height - 清除区域大小

void setCVis(bool visible);
参数:visible - 是否显示光标

void getCSize(int &width, int &height);
参数:width, height - 返回控制台尺寸

输入缓冲清除

void flushed();
作用:清除标准输入缓冲区

4. 随机与字符串工具 (JTBG_VOLDO)

随机数生成

int rd(int a, int b);
参数:a, b - 范围[a, b]
返回:范围内的随机整数

double random(double min, double max);
参数:min, max - 范围
返回:范围内的随机浮点数

string randS(int length);
参数:length - 字符串长度
返回:随机字符串(字母数字)

类型转换

template <typename to_string_T>
string tostring(to_string_T k);
参数:k - 任意类型值
返回:字符串表示

字符串处理

void lowerstring(string &s);
void uperstring(string &s);
void Testsuper(string &s);
参数:s - 输入字符串
作用:转换为小写/大写/首字母大写

数字验证

bool isNumber(const string& str);
参数:str - 字符串
返回:是否是有效数字(整数或小数)

5. 键盘输入模块 (JTBG_KEY)

// 键盘检测宏
#define iskeydown() _kbhit()

int chkey(int t);
参数:t - 等待时间(秒),-1为无限等待
返回:按下的键码,超时返回-1

6. 时间处理模块 (JTBG_TIME)

获取时间

enum TimeType { yer=0, mon=1, dat=2, hor=3, mit=4, sec=5, mst=6 };

int Gettime(TimeType x);
int Gettime(string x);
参数:x - 时间类型或字符串标识
返回:对应时间值

高精度计时器

struct Timer {
    void starttick();      // 开始计时
    void pausetick();      // 暂停计时
    void resettick();      // 重置计时器
    void rettime(double t);// 设置已过时间
    double timesec();      // 获取秒数
    double timemst();      // 获取毫秒数
};

7. 系统操作模块 (JTBG_SYSTEM)

系统管理

bool setPassword(const string& user, const string& pass);
参数:user - 用户名,pass - 新密码
返回:是否成功

bool getNetconnet(const string& ip, const string& user, const string& pass);
参数:ip - 远程IP,user - 用户名,pass - 密码
返回:是否成功连接

系统架构检测

int getSystemArch();
返回:系统架构代码(SX64, SX86等)

string getArchName(int archCode);
参数:archCode - 架构代码
返回:架构名称字符串

权限管理

bool IsRunAsAdmin();
返回:是否以管理员权限运行

void RunAsAdmin();
作用:以管理员权限重新运行程序

bool AddToCurrentUserStartup(const string& appName, const string& appPath);
参数:appName - 应用名称,appPath - 应用路径
返回:是否成功添加到启动项

进程管理

bool isRun(const char* processName);
参数:processName - 进程名
返回:进程是否正在运行

void Hide();
void Show();
void Windowset(int s);
作用:控制窗口显示/隐藏
参数:s - 窗口状态(SW_HIDE, SW_SHOW等)

文件和内存工具

long long getFileSize(const string &filename);
参数:filename - 文件名
返回:文件大小(字节)

void msSleep(int milliseconds);
参数:milliseconds - 毫秒数
作用:高精度延迟

int getFileCountInDir(const string& dirPath);
参数:dirPath - 目录路径
返回:文件数量

字节单位转换

struct memory {
    long long bytes;
    double toB();   // 字节
    double toKB();  // 千字节
    double toMB();  // 兆字节
    double toGB();  // 千兆字节
    double toTB();  // 太字节
    double toPB();  // 拍字节
};

8. 加密解密模块 (JTBG_DECANDENC)

XOR加密

string xorFile(const string& filePath, const string& key);
参数:filePath - 文件路径,key - 密钥
返回:加密/解密后的内容

void xortoFile(const string& filePath, const string& key);
作用:直接对文件进行XOR加密/解密

string xorpass(const string &str, const string &key);
参数:str - 字符串,key - 密钥
返回:XOR处理后的字符串

凯撒密码

void caesarFile(const string& filePath, int shift);
void FileCEnc(const string& filePath, int shift);
void FileCDec(const string& filePath, int shift);
参数:filePath - 文件路径,shift - 移位值
作用:文件凯撒加密/解密

string CEnc(const string& input, int shift);
string CDec(const string& input, int shift);
参数:input - 输入字符串,shift - 移位值
返回:凯撒加密/解密后的字符串

Base64编码

string base64up(const string &str, const string &base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
string base64dec(const string &str, const string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
参数:str - 输入字符串,base64Chars - 字符集
返回:Base64编码/解码后的字符串

9. 数学计算模块 (JTBG_MATH)

数学常量

constexpr double PI = 3.14159265358979323846;
constexpr double E = 2.71828182845904523536;
constexpr double SQRT2 = 1.41421356237309504880;
// ... 更多常量

基本运算

double getSqrtNt(double n, double t);
参数:n - 要开方的数,t - 精度阈值
返回:平方根,失败返回NAN

double toRadians(double degrees);
double toDegrees(double radians);
参数:degrees - 角度,radians - 弧度
返回:转换后的值

long long fac(int n);
参数:n - 非负整数
返回:阶乘值

幂运算

double qpow(double base, int exponent);
long long ipow(long long base, int exponent);
参数:base - 底数,exponent - 指数
返回:幂运算结果

组合数学

long long GetA(int a, int b);  // 排列数A(a,b)
long long GetC(int a, int b);  // 组合数C(a,b)
参数:a, b - 参数
返回:排列/组合数

数论函数

bool isPrime(int x);
bool isPrime(long long x);
参数:x - 要检查的数
返回:是否是质数

long long jtbg_gcd(long long a, long long b, bool usesystem=0);
int jtbg_gcd(int a, int b, bool usesystem=0);
参数:a, b - 数字,usesystem - 是否使用系统gcd
返回:最大公约数

long long jtbg_lcm(long long a, long long b, bool usesystem=0);
int jtbg_lcm(int a, int b, bool usesystem=0);
参数:a, b - 数字,usesystem - 是否使用系统gcd
返回:最小公倍数

long long jtbg_lcm_multi(const vector<long long>& numbers, bool usesystem = 0);
参数:numbers - 数字数组,usesystem - 是否使用系统gcd
返回:多个数的最小公倍数

质数生成

vector<int> MakePrimelist_AiShai(int from, int to);          // 埃氏筛
vector<int> MakePrimelist_OLaShai(int from, int to);         // 欧拉筛
vector<int> MakePrimelist_OLaShai_quick(int from, int to);   // 快速欧拉筛
参数:from, to - 范围
返回:范围内的质数列表

数字处理

int jtbg_countDigits(long long n);      // 数字位数
int jtbg_digitSum(long long n);         // 各位数字和
long long jtbg_reverseNumber(long long n); // 数字反转
bool jtbg_isPalindrome(long long n);    // 是否是回文数
bool jtbg_isPerfectSquare(int n);       // 是否是完全平方数
bool jtbg_isPerfectCube(int n);         // 是否是完全立方数

三角函数

double jtbg_sind(double degrees);    // 角度制正弦
double jtbg_cosd(double degrees);    // 角度制余弦
double jtbg_tand(double degrees);    // 角度制正切
double jtbg_asind(double x);         // 反正弦(返回角度)
double jtbg_acosd(double x);         // 反余弦(返回角度)
double jtbg_atand(double x);         // 反正切(返回角度)

几何计算

double jtbg_distance(double x1, double y1, double x2, double y2);  // 二维距离
double jtbg_distance3D(double x1, double y1, double z1, double x2, double y2, double z2);  // 三维距离
double jtbg_manhattanDistance(double x1, double y1, double x2, double y2);  // 曼哈顿距离
double jtbg_chebyshevDistance(double x1, double y1, double x2, double y2);  // 切比雪夫距离

复数运算

struct Complex {
    double re, im;
    // 构造函数
    Complex(double r = 0, double i = 0);
};

Complex jtbg_cadd(Complex a, Complex b);     // 复数加法
Complex jtbg_csub(Complex a, Complex b);     // 复数减法
Complex jtbg_cmul(Complex a, Complex b);     // 复数乘法
Complex jtbg_cdiv(Complex a, Complex b);     // 复数除法
double jtbg_cabs(Complex c);                // 复数模长
Complex jtbg_cconj(Complex c);              // 复数共轭
Complex jtbg_cexp(Complex c);               // 复数指数
Complex jtbg_cln(Complex c);                // 复数对数
Complex jtbg_cpow(Complex c, int n);        // 复数幂

统计函数

template<typename T>
double jtbg_mean(vector<T> data);           // 平均值
double jtbg_median(vector<T> data);         // 中位数
double jtbg_var(vector<T> data);            // 方差
double jtbg_std(vector<T> data);            // 标准差

矩阵运算

void jtbg_mat2_mul(double a[2][2], double b[2][2], double res[2][2]);  // 2x2矩阵乘法
double jtbg_mat2_det(double m[2][2]);        // 2x2矩阵行列式
bool jtbg_mat2_inv(double m[2][2], double inv[2][2]);  // 2x2矩阵求逆
double jtbg_mat3_det(double m[3][3]);        // 3x3矩阵行列式

数值计算

double jtbg_int_trap(double (*f)(double), double a, double b, int n = 1000);  // 梯形法积分
double jtbg_int_simp(double (*f)(double), double a, double b, int n = 1000);  // 辛普森法积分
double jtbg_deriv(double (*f)(double), double x, double h = 1e-5);            // 数值微分
double jtbg_root_bisect(double (*f)(double), double a, double b, double eps = 1e-10, int maxit = 1000);  // 二分法求根

10. 日志系统模块 (JTBG_LOG)

文件日志

struct logger {
    enum error_type { error=0, info=1, warn=2, debug=3 };
    
    void reset_logger(string path);            // 初始化日志文件
    void report(string s, error_type tp);      // 记录日志
    void stop();                               // 停止日志
};

新日志系统

struct new_logger {
    void logs(const string& s, int mod=LOG_NORMOL);
};
// 日志模式:
// LOG_NORMOL - 带时间戳和换行
// LOG_NOTIME - 不带时间戳,带换行
// LOG_NOTIMEANDENDL - 不带时间戳和换行

11. INI文件处理模块 (JTBG_INI)

读取INI

struct ifinistream {
    int set_ini_reader_path(const string& new_path);     // 设置INI文件路径
    int read_ini();                                      // 读取INI文件
    string ini_get_log();                                // 获取处理日志
    string ini_get_vol(const string& title, const string& key_name);  // 获取值
};

写入INI

struct ofinistream {
    int ini_reset();                                                // 重置并读取现有INI
    int set_ini_WRITER_path(const string& new_path);               // 设置INI文件路径
    int ini_write();                                               // 写入INI文件
    string ini_get_log();                                          // 获取处理日志
    int ini_set_title(const string& title);                        // 设置当前标题
    int ini_add_vol(const string& key, const string& vol);         // 添加键值对
};

12. 许可证验证模块 (JTBG_CC)

class cc_right_check {
    int cc_check(const string& path, int s);
};
参数:path - 许可证文件路径,s - 许可证类型(109,110,111)
返回:验证结果

struct cc_right {
    int cc_right_reset();      // 从配置文件重置许可证信息
    int cc_check();            // 验证许可证
};

13. 网络功能模块 (JTBG_WEB)

bool Iswebok();
返回:网络连接状态
    0 - 无网络
    1 - 局域网连接
    2 - 调制解调器连接
    3 - 其他连接

14. Luhn校验模块 (JTBG_LUHN)

string prepluhn(const string& input);
参数:input - 输入字符串
返回:提取的数字字符串

int toluhn(const string& num);
参数:num - 数字字符串
返回:Luhn校验码

bool visluhn(const string& fb);
参数:fb - 完整字符串(含校验码)
返回:是否通过Luhn校验

15. 快捷宏定义 (JTBG_SHORT)

#define syst system("cls")                    // 清屏
#define sys(x) system(x)                      // 执行系统命令
#define pause() system("pause")               // 暂停
#define noy_pause() system("pause > nul")     // 静默暂停
#define sleep(ms) Sleep(ms)                   // 毫秒延迟
#define sleep_s(s) Sleep(s*1000)              // 秒延迟
#define get_time() time(0)                    // 当前时间戳
#define getms() GetTickCount()                // 毫秒计时
#define jstoi(x) atoi(x.c_str())              // 字符串转int
#define jstol(x) stol(x)                      // 字符串转long
#define jstod(x) stod(x)                      // 字符串转double

16. 消息框模块 (JTBG_MSGBOX)

// 消息框类型宏
#define MSB_OK MB_OK
#define MSB_OKCL MB_OKCANCEL
#define MSB_TF MB_YESNO
#define MSB_INFO MB_ICONINFORMATION
#define MSB_WARN MB_ICONWARNING
#define MSB_ERROR MB_ICONERROR

int msgbox(string tit, string vol, UINT type=MSB_OKCL);
参数:tit - 标题,vol - 内容,type - 消息框类型
返回:用户点击的按钮

17. KMP算法模块 (JTBG_KMP)

// 注意:此模块的具体实现需查看源代码
// 提供KMP字符串匹配算法

18. 大数运算模块 (JTBG_BIGNUM)

// 注意:此模块的具体实现需查看源代码
// 提供大整数运算功能

19. 进程管理模块 (JTBG_PROS)

// 注意:此模块的具体实现需查看源代码
// 提供进程管理和监控功能

20. 进度条模块 (JTBG_PROGRESS)

// 注意:此模块的具体实现需查看源代码
// 提供进度条显示功能

21. 数组向量模块 (JTBG_ARGCV)

// 注意:此模块的具体实现需查看源代码
// 提供命令行参数和向量处理功能

22. RSA加密模块 (JTBG_RSA)

// 注意:此模块的具体实现需查看源代码
// 提供RSA加密解密功能

23. UDP网络模块 (JTBG_UDP)

// 注意:此模块的具体实现需查看源代码
// 提供UDP网络通信功能

四、使用示例

1. 基本使用

#define JTBG_COL
#define JTBG_MATH
#include "jtbg.h"

int main() {
    scg(RED, BLACK);
    say("Hello, World!", 50);
    
    scg(GREEN, BLACK);
    cout << "2 + 2 = " << PLS(2, 2) << endl;
    
    return 0;
}

2. 文件操作

#define JTBG_FILE
#include "jtbg.h"

int main() {
    Save file;
    file.setpt("test.txt");
    file.save_nol();
    file.save_o << "Hello, File!" << endl;
    file.save_stop();
    
    if (fileok("test.txt")) {
        cout << "File created successfully!" << endl;
    }
    
    return 0;
}

3. 数学计算

#define JTBG_MATH
#include "jtbg.h"

int main() {
    cout << "PI = " << PI << endl;
    cout << "sqrt(2) = " << getSqrtNt(2, 0.0001) << endl;
    cout << "5! = " << fac(5) << endl;
    cout << "Is 17 prime? " << isPrime(17) << endl;
    
    return 0;
}

五、注意事项

  1. 平台依赖:大多数功能仅适用于Windows平台
  2. 编译器:需要支持C++11标准的编译器
  3. 库依赖:需要链接Windows系统库(winmm.lib, wininet.lib等)
  4. 许可证:使用时需要定义 _agree_ 宏表示同意CC BY-NC-ND 4.0协议
  5. 错误处理:重要操作建议检查返回值或错误码

六、常见问题

  1. 编译错误:确保已包含必要的Windows头文件和库
  2. 链接错误:检查是否链接了所需的库文件
  3. 功能不可用:确认已正确定义对应的功能宏
  4. 性能问题:在性能敏感的场景中,注意函数的复杂度

此指南涵盖了jtbg.h库的主要功能,具体使用时请参考源代码中的详细实现和注释。

posted @ 2025-12-21 11:46  jtbg  阅读(3)  评论(0)    收藏  举报