焦糖饼干头文件c++最新同步
\(\color{green}正在更新\)
windos-2.0
点击查看代码
#define _WIN32_WINNT 0x0601
//#pragma cpp_std c++11
//你可以换成指令: -std=gnu++11
#include <bits/stdc++.h>
#include <Windows.h>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <cctype>
#include <mutex>
#include <fstream>
#include <chrono>
#include <conio.h>
#include <limits> // 用于清除输入缓冲区
#include <wininet.h>
#include <shlobj.h>
#include <powrprof.h>
#include <lmaccess.h>
#include <tlhelp32.h>
#include <commctrl.h>
#include <shellapi.h>
#include <mmsystem.h>
#include <psapi.h>
#include <thread>
#include <vector>
#include <map>
//#include <unistd.h> linux/macOS
using namespace std;
//你需要定义 _agree_ 表示同意 CC BY-NC-ND 4.0
#ifndef _agree_
#undef _JTBG_H_
#define _JTBG_H_
#endif
#ifndef _JTBG_H_
#define _JTBG_H_
#define MUL(a,b) (a)*(b)
#define __JTBG_NORETURN(a) ext(a)
#define MUS(a,b) (a)-(b)
#define PLS(a,b) (a)+(b)
#define DEF(a,b) (a)/(b)
class MD5
{
private:
typedef unsigned int uint32;
typedef unsigned long long uint64;
struct Context
{
uint32 state[4];
uint32 count[2];
unsigned char buffer[64];
};
static void init(Context &ctx)
{
ctx.count[0] = ctx.count[1] = 0;
ctx.state[0] = 0x67452301;
ctx.state[1] = 0xefcdab89;
ctx.state[2] = 0x98badcfe;
ctx.state[3] = 0x10325476;
}
static void update(Context &ctx, const unsigned char *input, size_t length)
{
uint32 i, index, partLen;
index = (uint32)((ctx.count[0] >> 3) & 0x3F);
if ((ctx.count[0] += ((uint32)length << 3)) < ((uint32)length << 3))
ctx.count[1]++;
ctx.count[1] += (uint32)(length >> 29);
partLen = 64 - index;
if (length >= partLen)
{
memcpy(&ctx.buffer[index], input, partLen);
transform(ctx.state, ctx.buffer);
for (i = partLen; i + 63 < length; i += 64)
transform(ctx.state, &input[i]);
index = 0;
}
else
{
i = 0;
}
memcpy(&ctx.buffer[index], &input[i], length - i);
}
static void final(unsigned char digest[16], Context &ctx)
{
unsigned char padding[64] = {0x80};
unsigned char bits[8];
size_t index, padLen;
encode(bits, ctx.count, 8);
index = (uint32)((ctx.count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
update(ctx, padding, padLen);
update(ctx, bits, 8);
encode(digest, ctx.state, 16);
}
static void transform(uint32 state[4], const unsigned char block[64])
{
uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
decode(x, block, 64);
FF(a, b, c, d, x[0], 7, 0xd76aa478);
FF(d, a, b, c, x[1], 12, 0xe8c7b756);
FF(c, d, a, b, x[2], 17, 0x242070db);
FF(b, c, d, a, x[3], 22, 0xc1bdceee);
FF(a, b, c, d, x[4], 7, 0xf57c0faf);
FF(d, a, b, c, x[5], 12, 0x4787c62a);
FF(c, d, a, b, x[6], 17, 0xa8304613);
FF(b, c, d, a, x[7], 22, 0xfd469501);
FF(a, b, c, d, x[8], 7, 0x698098d8);
FF(d, a, b, c, x[9], 12, 0x8b44f7af);
FF(c, d, a, b, x[10], 17, 0xffff5bb1);
FF(b, c, d, a, x[11], 22, 0x895cd7be);
FF(a, b, c, d, x[12], 7, 0x6b901122);
FF(d, a, b, c, x[13], 12, 0xfd987193);
FF(c, d, a, b, x[14], 17, 0xa679438e);
FF(b, c, d, a, x[15], 22, 0x49b40821);
GG(a, b, c, d, x[1], 5, 0xf61e2562);
GG(d, a, b, c, x[6], 9, 0xc040b340);
GG(c, d, a, b, x[11], 14, 0x265e5a51);
GG(b, c, d, a, x[0], 20, 0xe9b6c7aa);
GG(a, b, c, d, x[5], 5, 0xd62f105d);
GG(d, a, b, c, x[10], 9, 0x02441453);
GG(c, d, a, b, x[15], 14, 0xd8a1e681);
GG(b, c, d, a, x[4], 20, 0xe7d3fbc8);
GG(a, b, c, d, x[9], 5, 0x21e1cde6);
GG(d, a, b, c, x[14], 9, 0xc33707d6);
GG(c, d, a, b, x[3], 14, 0xf4d50d87);
GG(b, c, d, a, x[8], 20, 0x455a14ed);
GG(a, b, c, d, x[13], 5, 0xa9e3e905);
GG(d, a, b, c, x[2], 9, 0xfcefa3f8);
GG(c, d, a, b, x[7], 14, 0x676f02d9);
GG(b, c, d, a, x[12], 20, 0x8d2a4c8a);
HH(a, b, c, d, x[5], 4, 0xfffa3942);
HH(d, a, b, c, x[8], 11, 0x8771f681);
HH(c, d, a, b, x[11], 16, 0x6d9d6122);
HH(b, c, d, a, x[14], 23, 0xfde5380c);
HH(a, b, c, d, x[1], 4, 0xa4beea44);
HH(d, a, b, c, x[4], 11, 0x4bdecfa9);
HH(c, d, a, b, x[7], 16, 0xf6bb4b60);
HH(b, c, d, a, x[10], 23, 0xbebfbc70);
HH(a, b, c, d, x[13], 4, 0x289b7ec6);
HH(d, a, b, c, x[0], 11, 0xeaa127fa);
HH(c, d, a, b, x[3], 16, 0xd4ef3085);
HH(b, c, d, a, x[6], 23, 0x04881d05);
HH(a, b, c, d, x[9], 4, 0xd9d4d039);
HH(d, a, b, c, x[12], 11, 0xe6db99e5);
HH(c, d, a, b, x[15], 16, 0x1fa27cf8);
HH(b, c, d, a, x[2], 23, 0xc4ac5665);
II(a, b, c, d, x[0], 6, 0xf4292244);
II(d, a, b, c, x[7], 10, 0x432aff97);
II(c, d, a, b, x[14], 15, 0xab9423a7);
II(b, c, d, a, x[5], 21, 0xfc93a039);
II(a, b, c, d, x[12], 6, 0x655b59c3);
II(d, a, b, c, x[3], 10, 0x8f0ccc92);
II(c, d, a, b, x[10], 15, 0xffeff47d);
II(b, c, d, a, x[1], 21, 0x85845dd1);
II(a, b, c, d, x[8], 6, 0x6fa87e4f);
II(d, a, b, c, x[15], 10, 0xfe2ce6e0);
II(c, d, a, b, x[6], 15, 0xa3014314);
II(b, c, d, a, x[13], 21, 0x4e0811a1);
II(a, b, c, d, x[4], 6, 0xf7537e82);
II(d, a, b, c, x[11], 10, 0xbd3af235);
II(c, d, a, b, x[2], 15, 0x2ad7d2bb);
II(b, c, d, a, x[9], 21, 0xeb86d391);
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
static inline uint32 F(uint32 x, uint32 y, uint32 z) { return (x & y) | (~x & z); }
static inline uint32 G(uint32 x, uint32 y, uint32 z) { return (x & z) | (y & ~z); }
static inline uint32 H(uint32 x, uint32 y, uint32 z) { return x ^ y ^ z; }
static inline uint32 I(uint32 x, uint32 y, uint32 z) { return y ^ (x | ~z); }
static inline void FF(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 t)
{
a += F(b, c, d) + x + t;
a = (a << s) | (a >> (32 - s));
a += b;
}
static inline void GG(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 t)
{
a += G(b, c, d) + x + t;
a = (a << s) | (a >> (32 - s));
a += b;
}
static inline void HH(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 t)
{
a += H(b, c, d) + x + t;
a = (a << s) | (a >> (32 - s));
a += b;
}
static inline void II(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 t)
{
a += I(b, c, d) + x + t;
a = (a << s) | (a >> (32 - s));
a += b;
}
static void encode(unsigned char *output, const uint32 *input, size_t length)
{
for (size_t i = 0, j = 0; j < length; i++, j += 4)
{
output[j] = (unsigned char)(input[i] & 0xFF);
output[j + 1] = (unsigned char)((input[i] >> 8) & 0xFF);
output[j + 2] = (unsigned char)((input[i] >> 16) & 0xFF);
output[j + 3] = (unsigned char)((input[i] >> 24) & 0xFF);
}
}
static void decode(uint32 *output, const unsigned char *input, size_t length)
{
for (size_t i = 0, j = 0; j < length; i++, j += 4)
output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) |
(((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24);
}
public:
static std::string hash(const std::string &input)
{
Context ctx;
init(ctx);
update(ctx, (const unsigned char*)input.c_str(), input.length());
unsigned char digest[16];
final(digest, ctx);
char hex_str[33];
for (int i = 0; i < 16; i++)
sprintf(hex_str + i * 2, "%02x", digest[i]);
hex_str[32] = '\0';
return std::string(hex_str);
}
};
//MD5::hash(str)
unsigned long long pHash(const string& s)
{
const unsigned long long base = 911382629ULL;
const unsigned long long mod = 1000000000000000003ULL;
unsigned long long hashValue = 0;
// 使用传统for循环兼容C++98
for (size_t i = 0; i < s.size(); ++i)
{
char c = s[i];
hashValue = (hashValue * base + static_cast<unsigned long long>(c)) % mod;
}
return hashValue;
}
struct StringHash
{
static const unsigned long long base = 911382629ULL;
static const unsigned long long mod = 1000000000000000003ULL;
vector<unsigned long long> hash;
vector<unsigned long long> power;
StringHash(const string& s)
{
int n = s.size();
hash.resize(n + 1);
power.resize(n + 1);
power[0] = 1;
for (int i = 0; i < n; ++i) {
hash[i+1] = (hash[i] * base + static_cast<unsigned long long>(s[i])) % mod;
power[i+1] = (power[i] * base) % mod;
}
}
// 获取子串s[l..r]的哈希值(0-based,包含l和r)
unsigned long long getHash(int l, int r)
{
int len = r - l + 1;
unsigned long long res = (hash[r+1] - hash[l] * power[len]) % mod;
return res < 0 ? res + mod : res;
}
};
//StringHash strHash(str);
//cout << "前缀哈希完整字符串: " << strHash.getHash(0, str.size()-1) << endl;
//cout << "子串哈希(0-4): " << strHash.getHash(0, 4) << " (对应\"Hello\")" << endl;
size_t Hash(const string& s)
{
size_t hashVal = 0;
for (size_t i = 0; i < s.size(); ++i)
{
// 简单的哈希组合算法
hashVal = hashVal * 31 + static_cast<size_t>(s[i]);
}
return hashVal;
}
struct Save
{
string path;
int errorlevel=-1;
ifstream save_i;
ofstream save_o;
void setpt(string s)
{
path=s;
return ;
}
void read_nol()
{
save_i.open(path.c_str());
if(!save_i.good()) errorlevel=1100;
return ;
}
void read_bin()
{
save_i.open(path.c_str(),ios::in|ios::binary);
if(!save_i.good()) errorlevel=1100;
return ;
}
void save_nol()
{
save_o.open(path.c_str());
if(!save_o.good()) errorlevel=1000;
return ;
}
void save_app()
{
save_o.open(path.c_str(),ios::app|ios::out);
if(!save_o.good()) errorlevel=1000;
return ;
}
void save_bin()
{
save_o.open(path.c_str(),ios::binary|ios::out);
if(!save_o.good()) errorlevel=1000;
return ;
}
//save_o.write(s.data(),s.size());
void save_stop()
{
// save_i.close();
save_o.close();
return ;
}
void read_stop()
{
save_i.close();
// save_o.close();
return ;
}
};
string FileMD5(string path)
{
ifstream file(path,ios::in|ios::binary);
if (!file.is_open())
{
return "";
}
string fileContent;
file.seekg(0,ios::end);
size_t fileSize = static_cast<size_t>(file.tellg());
file.seekg(0,ios::beg);
fileContent.reserve(fileSize);
fileContent.assign(std::istreambuf_iterator<char>(file),std::istreambuf_iterator<char>());
if (!file && !file.eof())
{
return "";
}
return MD5::hash(fileContent);
}
void say(string s,int t)
{
if(t<0) return ;
int len=int(s.length());
for(int i=0;i<len;i++)
{
cout<<s[i];
Sleep(t);
}
return ;
}
void pause(string s)
{
cout<<s<<endl;
system("pause > nul");
return ;
}
int ext(int num)
{
cout<<"[jtbg.h] Process exited with return value "<<num<<endl;
exit(num);
return num;
}
template <typename to_string_T>
string tostring(to_string_T k)
{
stringstream ans;
ans<<k;
return ans.str();
}
int chkey(int t)
{
if(t!=-1)
{
t*=100;
while(t--)
{
if (_kbhit())
{
int key = _getch();
return key;
}
}
return -1;
}
else
{
while(1)
{
if (_kbhit())
{
int key = _getch();
return key;
}
}
}
}
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)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// 设置文本颜色和背景颜色,高4位是背景,低4位是文本
SetConsoleTextAttribute(hConsole, (y << 4) | x);
return ;
}
void scg(int x,int y)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// 设置文本颜色和背景颜色,高4位是背景,低4位是文本
SetConsoleTextAttribute(hConsole, (y << 4) | x);
return ;
}
void set_back_color()
{
scg(7,0);
return ;
}
int rd(int a,int b)
{
srand(GetTickCount()+rand());
return a+rand()%(b+1);
}
enum TimeType
{
yer=0,
mon=1,
dat=2,
hor=3,
mit=4,
sec=5,
mst=6,
};
int Gettime(TimeType x)
{
time_t now = time(0);
tm *localTime = localtime(&now);
if(x==0) return 1900 + localTime->tm_year;//年份
if(x==1) return 1 + localTime->tm_mon;//月份
if(x==2) return localTime->tm_mday;//日期
if(x==3) return localTime->tm_hour;//小时
if(x==4) return localTime->tm_min;//分钟
if(x==5) return localTime->tm_sec;//秒
if(x==6) return GetTickCount();//ms
return -1;
}
int Gettime(string x)
{
time_t now = time(0);
tm *localTime = localtime(&now);
if(x=="year") return 1900 + localTime->tm_year;//年份
if(x=="mon") return 1 + localTime->tm_mon;//月份
if(x=="dat") return localTime->tm_mday;//日期
if(x=="hor") return localTime->tm_hour;//小时
if(x=="mit") return localTime->tm_min;//分钟
if(x=="sec") return localTime->tm_sec;//秒
if(x=="mst") return GetTickCount();//ms
return -1;
}
//控制台光标位置控制
void setcursor(int x,int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//隐藏/显示控制台光标
void setCVis(bool visible)
{
CONSOLE_CURSOR_INFO cursorInfo = {1, visible};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
//获取控制台尺寸
void getCSize(int &width,int &height)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
//生成指定长度的随机字符串
string randS(int length)
{
const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string result;
for (int i = 0; i < length; ++i) {
result += chars[rand() % chars.size()];
}
return result;
}
//计算文件大小(字节)
long long getFileSize(const string &filename)
{
ifstream file(filename, ios::binary | ios::ate);
if(!file.good()) return -1;
return static_cast<long long>(file.tellg());
}
//延迟函数(高精度ms)
void msSleep(int milliseconds)
{
auto start = chrono::high_resolution_clock::now();
while (chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start).count() < milliseconds);
}
//检测文件读写是否
bool fileok(const string &filename)
{
ifstream file(filename);
return file.good();
}
//清除控制台指定区域内容
void clearCA(int x, int y, int width, int height)
{
for(int i=0;i<height;i++)
{
setcursor(x, y + i);
cout << string(width, ' ');
}
}
//计算文件夹内文件数量(仅统计一级目录)
int getFileCountInDir(const string& dirPath) {
int count = 0;
WIN32_FIND_DATA findData;
HANDLE hFind = FindFirstFile((dirPath + "\\*").c_str(), &findData);
if (hFind == INVALID_HANDLE_VALUE) return -1;
do
{
if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
count++; // 只统计文件,不统计文件夹
}
} while (FindNextFile(hFind, &findData) != 0);
FindClose(hFind);
return count;
}
double random(double min, double max)
{
return min + (max - min) * (rand() / (double)RAND_MAX);
}
//检查字符串是否为数字(整数或小数)
bool isNumber(const string& str)
{
if (str.empty()) return false;
size_t start = 0;
if (str[0] == '+' || str[0] == '-') start = 1;
bool hasDot = false;
for (size_t i = start; i < str.size(); ++i)
{
if (str[i] == '.')
{
if (hasDot) return false;
hasDot = true;
}
else if (!isdigit(str[i]))
{
return false;
}
}
return true;
}
struct memory
{
long long bytes;
double toB()
{
return static_cast<double>(bytes);
}
double toKB()
{
const double KB = 1024.0;
return bytes / KB;
}
double toMB()
{
const double MB = 1024.0 * 1024.0;
return bytes / MB;
}
double toGB()
{
const double GB = 1024.0 * 1024.0 * 1024.0;
return bytes / GB;
}
double toTB()
{
const double TB = 1024.0 * 1024.0 * 1024.0 * 1024.0;
return bytes / TB;
}
double toPB()
{
const double PB = 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0;
return bytes / PB;
}
};
// 将字节转换为B(字节)
double rtoB(int bytes)
{
return static_cast<double>(bytes);
}
// 将字节转换为KB(千字节)
double rtoKB(int bytes)
{
const double KB = 1024.0;
return bytes / KB;
}
// 将字节转换为MB(兆字节)
double rtoMB(int bytes)
{
const double MB = 1024.0 * 1024.0;
return bytes / MB;
}
// 将字节转换为GB(千兆字节)
double rtoGB(int bytes)
{
const double GB = 1024.0 * 1024.0 * 1024.0;
return bytes / GB;
}
// 将字节转换为TB(太字节)
double rtoTB(int bytes)
{
const double TB = 1024.0 * 1024.0 * 1024.0 * 1024.0;
return bytes / TB;
}
// 将字节转换为PB(拍字节)
double rtoPB(int bytes)
{
const double PB = 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0;
return bytes / PB;
}
void lowerstring(string &s)
{
for(int i=0;i<int(s.size());i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
s[i]=s[i]-'A'+'a';
}
}
return ;
}
void uperstring(string &s)
{
for(int i=0;i<int(s.size());i++)
{
if(s[i]>='a'&&s[i]<='z')
{
s[i]=s[i]-'a'+'A';
}
}
return ;
}
void flushed()
{
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.clear();
return ;
}
//xor加/解密
string xorpass(const string &str, const string &key)
{
string result = str;
size_t keyLen = key.size();
if (keyLen == 0) return result;
for (size_t i = 0; i < result.size(); ++i)
{
result[i] ^= key[i % keyLen];
}
return result;
}
string base64up(const string &str,const string &base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
{
string result;
int val = 0, valBits = 0;
for (unsigned char c : str)
{
val = (val << 8) + c;
valBits += 8;
while (valBits >= 6)
{
result += base64Chars[(val >> (valBits - 6)) & 0x3F];
valBits -= 6;
}
}
if (valBits > 0)
{
val <<= (6 - valBits);
result += base64Chars[val & 0x3F];
}
while (result.size() % 4 != 0)
{
result += '=';
}
return result;
}
string base64dec(const string &str,const string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
{
string result;
vector<int> val(4, -1);
for (size_t i = 0; i < str.size(); ++i)
{
char c = str[i];
if (c == '=') break;
size_t pos = base64Chars.find(c);
if (pos == string::npos) continue;
val[i % 4] = pos;
if (i % 4 == 3)
{
result += (val[0] << 2) + ((val[1] >> 4) & 0x3);
if (val[2] != -1)
{
result += ((val[1] << 4) & 0xF0) + ((val[2] >> 2) & 0xF);
}
if (val[3] != -1)
{
result += ((val[2] << 6) & 0xC0) + val[3];
}
val.assign(4, -1);
}
}
return result;
}
void Testsuper(string &s)
{
if (s.empty()) return;
s[0] = toupper(s[0]);
for (size_t i = 1; i < s.size();i++)
{
s[i]=tolower(s[i]);
}
}
struct Timer
{
private:
using Clock = std::chrono::high_resolution_clock;
using TimePoint = std::chrono::time_point<Clock>;
TimePoint start_;
TimePoint pause_;
bool isRunning_;
std::chrono::duration<double> elapsed_;
public:
Timer() : isRunning_(false), elapsed_(0) {}
void starttick()
{
if (!isRunning_)
{
start_ = Clock::now();
isRunning_ = true;
}
}
void pausetick()
{
if (isRunning_)
{
pause_ = Clock::now();
elapsed_ += pause_ - start_;
isRunning_ = false;
}
}
void resettick()
{
isRunning_ = false;
elapsed_ = std::chrono::duration<double>(0);
}
double timesec()
const
{
if (isRunning_)
{
auto now = Clock::now();
return (elapsed_ + (now - start_)).count();
}
return elapsed_.count();
}
double timemst()
const
{
return timesec() * 1000.0;
}
};
#ifndef _NOMATH_
#define _NOMATH_
constexpr double PI = 3.14159265358979323846264338327950288419716939937510582097494459;
constexpr double E = 2.71828182845904523536;
constexpr double SQRT2 = 1.41421356237309504880;
constexpr double DEG_TO_RAD = PI / 180.0; // 角度转弧度系数
constexpr double RAD_TO_DEG = 180.0 / PI; // 弧度转角度系数
//角度转弧度
inline double toRadians(double degrees)
{
return degrees * DEG_TO_RAD;
}
// 弧度转角度
inline double toDegrees(double radians)
{
return radians * RAD_TO_DEG;
}
// 计算阶乘
inline long long fac(int n)
{
if(n<0) return -1;
long long result = 1;
for (int i = 2; i <= n; ++i)
{
result *= i;
}
return result;
}
double qpow(double base, int exponent)
{
if (exponent==0) return 1.0;
if (base==0) return 0.0;
bool isNegative = exponent < 0;
unsigned int absExponent = static_cast<unsigned int>(isNegative ? -exponent : exponent);
double result = 1.0;
double current = base;
while (absExponent > 0)
{
if (absExponent % 2 == 1)
{
result *= current;
}
current *= current;
absExponent /= 2;
}
return isNegative ? 1.0 / result : result;
}
long long A(int a, int b)
{
if(a < 0 || b < 0 || a < b)
{
return -1;
}
if (b == 0)
{
return 1;
}
long long result = 1;
for (int i = 0; i < b; ++i)
{
result *= (a - i);
if (result < 0)
{
return -1;
}
}
return result;
}
long long C(int a, int b)
{
if (a < 0 || b < 0 || a < b)
{
return -1;
}
if (b == 0 || a == b)
{
return 1;
}
b = min(b, a - b);
long long result = 1;
for (int i = 1; i <= b; ++i)
{
result = result * (a - b + i) / i;
if (result < 0)
{
return -1;
}
}
return result;
}
bool isHe(int x)
{
if(x<2) return 0;
for(int i=2;i*i<=x;i++)
{
if(x%i==0) return 0;
}
return 1;
}
#endif
#ifndef _NOSHORT_
#define _NOSHORT_
#define syst system("cls")
#define sys(x) system(x)
#define no_pause() system("pause > nul")
#define pause() system("pause")
#define to_int(x) stoi(x) // 转换为int类型
#define to_long(x) stol(x) // 转换为long类型
#define to_double(x) stod(x) // 转换为double类型
#define to_str(x) tostring(x) // 转换为string类型
#define sleep(ms) Sleep(ms) // 延迟指定毫秒数
#define time_now() time(0) // 获取当前时间戳(秒级)
#define getms() GetTickCount()
#define shuta() system("shutdown -a")
#define shuts() system("shutdown -s -t 0")
#define shutr() system("shutdown -g -t 0")
#define about() jtbg_h_about()
#endif
struct logger
{
Save rep;
enum error_type
{
error=0,
info=1,
warn=2,
debug=3,
};
void reset_logger(string path)
{
rep.setpt(path);
rep.save_nol();
rep.save_o<<"["<<Gettime(yer)<<"/"<<Gettime(mon)<<"/"<<Gettime(dat)<<"-"<<Gettime(hor)<<":"<<Gettime(mit)<<":"<<Gettime(sec)<<"]"<<endl;
return ;
}
void report(string s,error_type tp)
{
rep.save_o<<"["<<Gettime(hor)<<":"<<Gettime(mit)<<":"<<Gettime(sec)<<"("<<Gettime(mst)%1000<<")] ";
if(tp==0) rep.save_o<<"{type=error} ";
else if(tp==1) rep.save_o<<"{type=info} ";
else if(tp==2) rep.save_o<<"{type=warn} ";
else if(tp==3) rep.save_o<<"{type=debug} ";
else rep.save_o<<"{type=unknow} ";
rep.save_o<<s<<endl;
flush(rep.save_o);
return ;
}
void stop()
{
rep.save_stop();
return ;
}
};
void thred(logger p,string s)
{
p.report(s,logger::error);
return ;
}
string xorFile(const string& filePath,const string& key)
{
if (key.empty())
{
return "";
}
std::ifstream file(filePath.c_str(), std::ios::in | std::ios::binary);
if (!file.is_open())
{
return "";
}
std::string fileContent;
char buffer[4096];
while (file.read(buffer, sizeof(buffer)))
{
fileContent.append(buffer, sizeof(buffer));
}
fileContent.append(buffer, file.gcount());
const size_t keyLen = key.size();
size_t keyIndex = 0;
for (string::iterator it = fileContent.begin(); it != fileContent.end(); ++it)
{
*it = static_cast<char>(static_cast<uint8_t>(*it) ^ static_cast<uint8_t>(key[keyIndex]));
keyIndex = (keyIndex + 1) % keyLen;
}
return fileContent;
}
void xortoFile(const string& filePath,const string& key)
{
if (key.empty())
{
return ;
}
ifstream file(filePath.c_str(),ios::in | ios::binary);
if (!file.is_open())
{
return ;
}
string fileContent;
char buffer[4096];
while (file.read(buffer, sizeof(buffer)))
{
fileContent.append(buffer, sizeof(buffer));
}
fileContent.append(buffer, file.gcount());
file.close();
const size_t keyLen = key.size();
size_t keyIndex = 0;
for (std::string::iterator it = fileContent.begin(); it != fileContent.end(); ++it)
{
*it = static_cast<char>(static_cast<uint8_t>(*it) ^ static_cast<uint8_t>(key[keyIndex]));
keyIndex = (keyIndex + 1) % keyLen;
}
std::ofstream outFile(filePath.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
if (outFile.is_open())
{
outFile.write(fileContent.data(), fileContent.size());
outFile.close();
}
}
void caesarFile(const string& filePath,int shift)
{
shift %= 256;
if (shift < 0)
{
shift += 256;
}
std::ifstream file(filePath.c_str(), std::ios::in | std::ios::binary);
if (!file.is_open())
{
return ;
}
string fileContent;
char buffer[4096];
while (file.read(buffer, sizeof(buffer)))
{
fileContent.append(buffer, sizeof(buffer));
}
fileContent.append(buffer, file.gcount());
file.close();
for(string::iterator it = fileContent.begin(); it != fileContent.end();it++)
{
*it = static_cast<char>((static_cast<uint8_t>(*it) + shift) % 256);
}
std::ofstream outFile(filePath.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
if (outFile.is_open())
{
outFile.write(fileContent.data(), fileContent.size());
outFile.close();
}
}
//加
void FileCEnc(const string& filePath, int shift)
{
caesarFile(filePath, shift);
}
//解
void FileCDec(const string& filePath, int shift)
{
caesarFile(filePath, 256 - (shift % 256));
}
//加密
string CEnc(const string& input, int shift)
{
shift %= 256;
if (shift < 0)
{
shift += 256;
}
string result = input;
const size_t len = result.size();
for (size_t i = 0; i < len; ++i)
{
result[i] = static_cast<char>((static_cast<uint8_t>(result[i]) + shift) % 256);
}
return result;
}
//解密
string CDec(const string& input, int shift)
{
shift %= 256;
if (shift < 0)
{
shift += 256;
}
return CEnc(input, 256 - shift);
}
void jtbg_h_about()
{
cout<<"jtbg.h\n";
cout<<"wroten by @jiaotangbinggan\n";
cout<<"Process start with 2025/10/4\n";
cout<<"Lv: 2.0.0 -std11\n";
cout<<"Used CC BY-NC-ND 4.0\n";
cout<<"join&talk to me(us):"<<endl;
cout<<"luogu-1497734"<<endl;
cout<<"197474286@qq.com"<<endl;
cout<<"guitu1125@yeah.net"<<endl;
cout<<"this file hash:none"<<endl;
cout<<"this file will be new in a long time"<<endl;
return ;
}
#endif
linux/macOS-Unix
点击查看代码
#define _JTBG_UNIX_VERSION "2.0.0(unix-1.0.0) -std=c++11"
#define _JTBG_LICENSE "CC BY-NC-ND 4.0"
// 适配Linux/macOS平台,替换Windows依赖头文件
#include <bits/stdc++.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/time.h>
#include <ctime>
#include <cstdint>
#include <csignal>
#include <locale>
#include <codecvt>
using namespace std;
// 防止头文件重复包含
#ifndef _JTBG_UNIX_H_
#define _JTBG_UNIX_H_
// 基础数学运算宏定义
#define MUL(a,b) (a)*(b)
#define __JTBG_NORETURN(a) ext(a)
#define MUS(a,b) (a)-(b)
#define PLS(a,b) (a)+(b)
#define DEF(a,b) ((b)!=0?(a)/(b):throw invalid_argument("Division by zero"))
// ========================== 1. 哈希计算模块 ==========================
class MD5
{
private:
typedef uint32_t uint32;
typedef uint64_t uint64;
struct Context
{
uint32 state[4];
uint32 count[2];
unsigned char buffer[64];
};
static void init(Context &ctx)
{
ctx.count[0] = ctx.count[1] = 0;
ctx.state[0] = 0x67452301;
ctx.state[1] = 0xefcdab89;
ctx.state[2] = 0x98badcfe;
ctx.state[3] = 0x10325476;
}
static void update(Context &ctx, const unsigned char *input, size_t length)
{
uint32 i, index, partLen;
index = (uint32)((ctx.count[0] >> 3) & 0x3F);
if ((ctx.count[0] += ((uint32)length << 3)) < ((uint32)length << 3))
ctx.count[1]++;
ctx.count[1] += (uint32)(length >> 29);
partLen = 64 - index;
if (length >= partLen) {
memcpy(&ctx.buffer[index], input, partLen);
transform(ctx.state, ctx.buffer);
for (i = partLen; i + 63 < length; i += 64)
transform(ctx.state, &input[i]);
index = 0;
} else {
i = 0;
}
memcpy(&ctx.buffer[index], &input[i], length - i);
}
static void final(unsigned char digest[16], Context &ctx)
{
unsigned char padding[64] = {0x80};
unsigned char bits[8];
size_t index, padLen;
encode(bits, ctx.count, 8);
index = (uint32)((ctx.count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
update(ctx, padding, padLen);
update(ctx, bits, 8);
encode(digest, ctx.state, 16);
}
static void transform(uint32 state[4], const unsigned char block[64])
{
uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
decode(x, block, 64);
#define FF(a,b,c,d,x,s,t) { a += F(b,c,d) + x + t; a = (a << s) | (a >> (32 - s)); a += b; }
#define GG(a,b,c,d,x,s,t) { a += G(b,c,d) + x + t; a = (a << s) | (a >> (32 - s)); a += b; }
#define HH(a,b,c,d,x,s,t) { a += H(b,c,d) + x + t; a = (a << s) | (a >> (32 - s)); a += b; }
#define II(a,b,c,d,x,s,t) { a += I(b,c,d) + x + t; a = (a << s) | (a >> (32 - s)); a += b; }
FF(a,b,c,d,x[0],7,0xd76aa478); FF(d,a,b,c,x[1],12,0xe8c7b756);
FF(c,d,a,b,x[2],17,0x242070db); FF(b,c,d,a,x[3],22,0xc1bdceee);
FF(a,b,c,d,x[4],7,0xf57c0faf); FF(d,a,b,c,x[5],12,0x4787c62a);
FF(c,d,a,b,x[6],17,0xa8304613); FF(b,c,d,a,x[7],22,0xfd469501);
FF(a,b,c,d,x[8],7,0x698098d8); FF(d,a,b,c,x[9],12,0x8b44f7af);
FF(c,d,a,b,x[10],17,0xffff5bb1); FF(b,c,d,a,x[11],22,0x895cd7be);
FF(a,b,c,d,x[12],7,0x6b901122); FF(d,a,b,c,x[13],12,0xfd987193);
FF(c,d,a,b,x[14],17,0xa679438e); FF(b,c,d,a,x[15],22,0x49b40821);
GG(a,b,c,d,x[1],5,0xf61e2562); GG(d,a,b,c,x[6],9,0xc040b340);
GG(c,d,a,b,x[11],14,0x265e5a51); GG(b,c,d,a,x[0],20,0xe9b6c7aa);
GG(a,b,c,d,x[5],5,0xd62f105d); GG(d,a,b,c,x[10],9,0x02441453);
GG(c,d,a,b,x[15],14,0xd8a1e681); GG(b,c,d,a,x[4],20,0xe7d3fbc8);
GG(a,b,c,d,x[9],5,0x21e1cde6); GG(d,a,b,c,x[14],9,0xc33707d6);
GG(c,d,a,b,x[3],14,0xf4d50d87); GG(b,c,d,a,x[8],20,0x455a14ed);
GG(a,b,c,d,x[13],5,0xa9e3e905); GG(d,a,b,c,x[2],9,0xfcefa3f8);
GG(c,d,a,b,x[7],14,0x676f02d9); GG(b,c,d,a,x[12],20,0x8d2a4c8a);
HH(a,b,c,d,x[5],4,0xfffa3942); HH(d,a,b,c,x[8],11,0x8771f681);
HH(c,d,a,b,x[11],16,0x6d9d6122); HH(b,c,d,a,x[14],23,0xfde5380c);
HH(a,b,c,d,x[1],4,0xa4beea44); HH(d,a,b,c,x[4],11,0x4bdecfa9);
HH(c,d,a,b,x[7],16,0xf6bb4b60); HH(b,c,d,a,x[10],23,0xbebfbc70);
HH(a,b,c,d,x[13],4,0x289b7ec6); HH(d,a,b,c,x[0],11,0xeaa127fa);
HH(c,d,a,b,x[3],16,0xd4ef3085); HH(b,c,d,a,x[6],23,0x04881d05);
HH(a,b,c,d,x[9],4,0xd9d4d039); HH(d,a,b,c,x[12],11,0xe6db99e5);
HH(c,d,a,b,x[15],16,0x1fa27cf8); HH(b,c,d,a,x[2],23,0xc4ac5665);
II(a,b,c,d,x[0],6,0xf4292244); II(d,a,b,c,x[7],10,0x432aff97);
II(c,d,a,b,x[14],15,0xab9423a7); II(b,c,d,a,x[5],21,0xfc93a039);
II(a,b,c,d,x[12],6,0x655b59c3); II(d,a,b,c,x[3],10,0x8f0ccc92);
II(c,d,a,b,x[10],15,0xffeff47d); II(b,c,d,a,x[1],21,0x85845dd1);
II(a,b,c,d,x[8],6,0x6fa87e4f); II(d,a,b,c,x[15],10,0xfe2ce6e0);
II(c,d,a,b,x[6],15,0xa3014314); II(b,c,d,a,x[13],21,0x4e0811a1);
II(a,b,c,d,x[4],6,0xf7537e82); II(d,a,b,c,x[11],10,0xbd3af235);
II(c,d,a,b,x[2],15,0x2ad7d2bb); II(b,c,d,a,x[9],21,0xeb86d391);
#undef FF
#undef GG
#undef HH
#undef II
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
static inline uint32 F(uint32 x, uint32 y, uint32 z) { return (x & y) | (~x & z); }
static inline uint32 G(uint32 x, uint32 y, uint32 z) { return (x & z) | (y & ~z); }
static inline uint32 H(uint32 x, uint32 y, uint32 z) { return x ^ y ^ z; }
static inline uint32 I(uint32 x, uint32 y, uint32 z) { return y ^ (x | ~z); }
static void encode(unsigned char *output, const uint32 *input, size_t length)
{
for (size_t i = 0, j = 0; j < length; i++, j += 4)
{
output[j] = (unsigned char)(input[i] & 0xFF);
output[j + 1] = (unsigned char)((input[i] >> 8) & 0xFF);
output[j + 2] = (unsigned char)((input[i] >> 16) & 0xFF);
output[j + 3] = (unsigned char)((input[i] >> 24) & 0xFF);
}
}
static void decode(uint32 *output, const unsigned char *input, size_t length)
{
for (size_t i = 0, j = 0; j < length; i++, j += 4)
output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) |
(((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24);
}
public:
static string hash(const string &input)
{
Context ctx;
init(ctx);
update(ctx, (const unsigned char*)input.c_str(), input.length());
unsigned char digest[16];
final(digest, ctx);
char hex_str[33];
for (int i = 0; i < 16; i++)
snprintf(hex_str + i * 2, 3, "%02x", digest[i]);
hex_str[32] = '\0';
return string(hex_str);
}
};
// 字符串哈希(多项式哈希)
unsigned long long pHash(const string& s)
{
const unsigned long long base = 911382629ULL;
const unsigned long long mod = 1000000000000000003ULL;
unsigned long long hashValue = 0;
for (size_t i = 0; i < s.size(); ++i)
{
char c = s[i];
hashValue = (hashValue * base + static_cast<unsigned long long>(c)) % mod;
}
return hashValue;
}
// 前缀哈希(支持子串哈希计算)
struct StringHash
{
static const unsigned long long base = 911382629ULL;
static const unsigned long long mod = 1000000000000000003ULL;
vector<unsigned long long> hash;
vector<unsigned long long> power;
StringHash(const string& s)
{
int n = s.size();
hash.resize(n + 1);
power.resize(n + 1);
power[0] = 1;
for (int i = 0; i < n; ++i) {
hash[i+1] = (hash[i] * base + static_cast<unsigned long long>(s[i])) % mod;
power[i+1] = (power[i] * base) % mod;
}
}
// 获取子串s[l..r]的哈希值(0-based,包含l和r)
unsigned long long getHash(int l, int r)
{
if (l > r || l < 0 || r >= (int)hash.size()-1)
throw invalid_argument("Invalid substring range");
int len = r - l + 1;
unsigned long long res = (hash[r+1] - hash[l] * power[len]) % mod;
return res < 0 ? res + mod : res;
}
};
// 基础字符串哈希(31为基数)
size_t Hash(const string& s)
{
size_t hashVal = 0;
for (size_t i = 0; i < s.size(); ++i)
{
hashVal = hashVal * 31 + static_cast<size_t>(s[i]);
}
return hashVal;
}
// ========================== 2. 文件操作模块 ==========================
struct Save
{
string path;
int errorlevel = -1; // 1100:读失败, 1000:写失败
ifstream save_i;
ofstream save_o;
// 设置文件路径
void setpt(const string& s) { path = s; }
// 文本方式读取(无锁)
void read_nol()
{
save_i.open(path.c_str());
if (!save_i.good())
errorlevel = 1100;
}
// 二进制方式读取
void read_bin()
{
save_i.open(path.c_str(), ios::in | ios::binary);
if (!save_i.good())
errorlevel = 1100;
}
// 文本方式写入(覆盖)
void save_nol()
{
save_o.open(path.c_str());
if (!save_o.good())
errorlevel = 1000;
}
// 文本方式追加写入
void save_app()
{
save_o.open(path.c_str(), ios::app | ios::out);
if (!save_o.good())
errorlevel = 1000;
}
// 二进制方式写入(覆盖)
void save_bin()
{
save_o.open(path.c_str(), ios::binary | ios::out);
if (!save_o.good())
errorlevel = 1000;
}
// 关闭写入流
void save_stop() { save_o.close(); }
// 关闭读取流
void read_stop() { save_i.close(); }
};
// 计算文件MD5值
string FileMD5(const string& path)
{
ifstream file(path, ios::in | ios::binary);
if (!file.is_open())
return "";
string fileContent;
file.seekg(0, ios::end);
size_t fileSize = static_cast<size_t>(file.tellg());
file.seekg(0, ios::beg);
fileContent.reserve(fileSize);
fileContent.assign(istreambuf_iterator<char>(file), istreambuf_iterator<char>());
if (!file && !file.eof())
return "";
return MD5::hash(fileContent);
}
// 获取文件大小(字节)
long long getFileSize(const string& filename)
{
struct stat fileStat;
if (stat(filename.c_str(), &fileStat) != 0)
return -1;
return static_cast<long long>(fileStat.st_size);
}
// 检查文件是否可读写
bool fileok(const string& filename)
{
ifstream file(filename);
return file.good();
}
// 统计文件夹内文件数量(仅一级目录,不包含子文件夹)
int getFileCountInDir(const string& dirPath)
{
DIR* dir = opendir(dirPath.c_str());
if (dir == nullptr)
return -1;
int count = 0;
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr)
{
// 跳过.和..目录
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// 判断是否为文件(非目录)
string fullPath = dirPath + "/" + entry->d_name;
struct stat statBuf;
if (stat(fullPath.c_str(), &statBuf) == 0 && S_ISREG(statBuf.st_mode))
count++;
}
closedir(dir);
return count;
}
// ========================== 3. 控制台控制模块 ==========================
// 控制台颜色枚举(ANSI转义码对应)
enum Color
{
BLACK = 30, // 前景色:黑
RED = 31, // 前景色:红
GREEN = 32, // 前景色:绿
YELLOW = 33, // 前景色:黄
BLUE = 34, // 前景色:蓝
MAGENTA = 35, // 前景色:品红
CYAN = 36, // 前景色:青
WHITE = 37, // 前景色:白
BRIGHT_BLACK = 90, // 亮前景:黑
BRIGHT_RED = 91, // 亮前景:红
BRIGHT_GREEN = 92, // 亮前景:绿
BRIGHT_YELLOW = 93, // 亮前景:黄
BRIGHT_BLUE = 94, // 亮前景:蓝
BRIGHT_MAGENTA = 95, // 亮前景:品红
BRIGHT_CYAN = 96, // 亮前景:青
BRIGHT_WHITE = 97, // 亮前景:白
// 背景色(+40)
BG_BLACK = 40,
BG_RED = 41,
BG_GREEN = 42,
BG_YELLOW = 43,
BG_BLUE = 44,
BG_MAGENTA = 45,
BG_CYAN = 46,
BG_WHITE = 47,
BG_BRIGHT_BLACK = 100,
BG_BRIGHT_RED = 101,
BG_BRIGHT_GREEN = 102,
BG_BRIGHT_YELLOW = 103,
BG_BRIGHT_BLUE = 104,
BG_BRIGHT_MAGENTA = 105,
BG_BRIGHT_CYAN = 106,
BG_BRIGHT_WHITE = 107
};
// 设置控制台文本颜色(前景色+背景色)
void scg(Color fg, Color bg)
{
cout << "\033[" << static_cast<int>(fg) << ";" << static_cast<int>(bg) << "m";
}
// 重置控制台颜色为默认
void set_back_color()
{
cout << "\033[0m"; // ANSI重置码
}
// 获取控制台尺寸(宽x高,单位:字符)
void getCSize(int &width, int &height)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
width = w.ws_col;
height = w.ws_row;
}
// 设置控制台光标位置(x:列,y:行,0-based)
void setcursor(int x, int y)
{
cout << "\033[" << (y + 1) << ";" << (x + 1) << "H"; // ANSI光标定位码(1-based)
}
// 隐藏/显示控制台光标
void setCVis(bool visible)
{
if (visible)
cout << "\033[?25h"; // 显示光标
else
cout << "\033[?25l"; // 隐藏光标
}
// 清除控制台指定区域内容(x:起始列,y:起始行,width:宽度,height:高度)
void clearCA(int x, int y, int width, int height)
{
for (int i = 0; i < height; ++i)
{
setcursor(x, y + i);
cout << string(width, ' ');
}
setcursor(x, y); // 回到起始位置
}
// 清除整个控制台
void syst()
{
cout << "\033[2J\033[H"; // ANSI清屏+光标归位
}
// ========================== 4. 时间与定时器模块 ==========================
// 时间类型枚举
enum TimeType
{
yer = 0, // 年份
mon = 1, // 月份(1-12)
dat = 2, // 日期(1-31)
hor = 3, // 小时(0-23)
mit = 4, // 分钟(0-59)
sec = 5, // 秒(0-59)
mst = 6 // 毫秒(0-999)
};
// 获取指定类型的时间(支持枚举参数)
int Gettime(TimeType x)
{
time_t now = time(nullptr);
tm *localTime = localtime(&now);
switch (x)
{
case yer: return 1900 + localTime->tm_year;
case mon: return 1 + localTime->tm_mon;
case dat: return localTime->tm_mday;
case hor: return localTime->tm_hour;
case mit: return localTime->tm_min;
case sec: return localTime->tm_sec;
case mst:
{
struct timeval tv;
gettimeofday(&tv, nullptr);
return tv.tv_usec / 1000; // 微秒转毫秒
}
default: return -1;
}
}
// 获取指定类型的时间(支持字符串参数)
int Gettime(const string& x)
{
if (x == "year") return Gettime(yer);
if (x == "mon") return Gettime(mon);
if (x == "dat") return Gettime(dat);
if (x == "hor") return Gettime(hor);
if (x == "mit") return Gettime(mit);
if (x == "sec") return Gettime(sec);
if (x == "mst") return Gettime(mst);
return -1;
}
// 高精度定时器
struct Timer
{
private:
using Clock = chrono::high_resolution_clock;
using TimePoint = chrono::time_point<Clock>;
TimePoint start_;
TimePoint pause_;
bool isRunning_;
chrono::duration<double> elapsed_;
public:
Timer() : isRunning_(false), elapsed_(0) {}
// 开始计时
void starttick()
{
if (!isRunning_)
{
start_ = Clock::now();
isRunning_ = true;
}
}
// 暂停计时
void pausetick()
{
if (isRunning_)
{
pause_ = Clock::now();
elapsed_ += pause_ - start_;
isRunning_ = false;
}
}
// 重置计时
void resettick()
{
isRunning_ = false;
elapsed_ = chrono::duration<double>(0);
}
// 获取已流逝时间(秒)
double timesec() const
{
if (isRunning_)
{
auto now = Clock::now();
return (elapsed_ + (now - start_)).count();
}
return elapsed_.count();
}
// 获取已流逝时间(毫秒)
double timemst() const
{
return timesec() * 1000.0;
}
};
// 延迟函数(毫秒,高精度)
void msSleep(int milliseconds)
{
this_thread::sleep_for(chrono::milliseconds(milliseconds));
}
// 获取当前时间戳(秒级)
time_t time_now()
{
return time(nullptr);
}
// 获取当前时间戳(毫秒级)
long long getms()
{
struct timeval tv;
gettimeofday(&tv, nullptr);
return (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
// ========================== 5. 数据加密与编码模块 ==========================
// XOR字符串加解密(密钥循环复用)
string xorpass(const string &str, const string &key)
{
if (key.empty()) return str;
string result = str;
size_t keyLen = key.size();
for (size_t i = 0; i < result.size(); ++i)
{
result[i] ^= key[i % keyLen];
}
return result;
}
// XOR文件加解密(返回加密后内容)
string xorFile(const string& filePath, const string& key)
{
if (key.empty()) return "";
int fd = open(filePath.c_str(), O_RDONLY | O_BINARY);
if (fd == -1) return "";
struct stat st;
fstat(fd, &st);
size_t fileSize = st.st_size;
string fileContent(fileSize, 0);
read(fd, &fileContent[0], fileSize);
close(fd);
const size_t keyLen = key.size();
size_t keyIndex = 0;
for (auto& c : fileContent)
{
c = static_cast<char>(static_cast<uint8_t>(c) ^ static_cast<uint8_t>(key[keyIndex]));
keyIndex = (keyIndex + 1) % keyLen;
}
return fileContent;
}
// XOR文件加解密(直接写入原文件)
void xortoFile(const string& filePath, const string& key)
{
if (key.empty()) return;
int fd = open(filePath.c_str(), O_RDWR | O_BINARY);
if (fd == -1) return;
struct stat st;
fstat(fd, &st);
size_t fileSize = st.st_size;
string fileContent(fileSize, 0);
read(fd, &fileContent[0], fileSize);
const size_t keyLen = key.size();
size_t keyIndex = 0;
for (auto& c : fileContent)
{
c = static_cast<char>(static_cast<uint8_t>(c) ^ static_cast<uint8_t>(key[keyIndex]));
keyIndex = (keyIndex + 1) % keyLen;
}
lseek(fd, 0, SEEK_SET);
write(fd, &fileContent[0], fileSize);
close(fd);
}
// 凯撒文件加解密(移位加密)
void caesarFile(const string& filePath, int shift)
{
shift %= 256;
if (shift < 0) shift += 256;
int fd = open(filePath.c_str(), O_RDWR | O_BINARY);
if (fd == -1) return;
struct stat st;
fstat(fd, &st);
size_t fileSize = st.st_size;
string fileContent(fileSize, 0);
read(fd, &fileContent[0], fileSize);
for (auto& c : fileContent)
{
c = static_cast<char>((static_cast<uint8_t>(c) + shift) % 256);
}
lseek(fd, 0, SEEK_SET);
write(fd, &fileContent[0], fileSize);
close(fd);
}
// 凯撒加密(字符串)
string CEnc(const string& input, int shift)
{
shift %= 256;
if (shift < 0) shift += 256;
string result = input;
for (auto& c : result)
{
c = static_cast<char>((static_cast<uint8_t>(c) + shift) % 256);
}
return result;
}
// 凯撒解密(字符串)
string CDec(const string& input, int shift)
{
shift %= 256;
if (shift < 0) shift += 256;
return CEnc(input, 256 - shift);
}
// 文件凯撒加密
void FileCEnc(const string& filePath, int shift)
{
caesarFile(filePath, shift);
}
// 文件凯撒解密
void FileCDec(const string& filePath, int shift)
{
caesarFile(filePath, 256 - (shift % 256));
}
// Base64编码
string base64up(const string &str, const string &base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
{
string result;
int val = 0, valBits = 0;
for (unsigned char c : str)
{
val = (val << 8) + c;
valBits += 8;
while (valBits >= 6)
{
result += base64Chars[(val >> (valBits - 6)) & 0x3F];
valBits -= 6;
}
}
if (valBits > 0)
{
val <<= (6 - valBits);
result += base64Chars[val & 0x3F];
}
while (result.size() % 4 != 0)
{
result += '=';
}
return result;
}
// Base64解码
string base64dec(const string &str, const string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
{
string result;
vector<int> val(4, -1);
for (size_t i = 0; i < str.size(); ++i)
{
char c = str[i];
if (c == '=') break;
size_t pos = base64Chars.find(c);
if (pos == string::npos) continue;
val[i % 4] = pos;
if (i % 4 == 3)
{
result += (val[0] << 2) + ((val[1] >> 4) & 0x3);
if (val[2] != -1)
{
result += ((val[1] << 4) & 0xF0) + ((val[2] >> 2) & 0xF);
}
if (val[3] != -1)
{
result += ((val[2] << 6) & 0xC0) + val[3];
}
val.assign(4, -1);
}
}
return result;
}
// ========================== 6. 数学计算模块 ==========================
#ifndef _NOMATH_
#define _NOMATH_
// 数学常量(高精度)
constexpr double PI = 3.14159265358979323846264338327950288419716939937510582097494459;
constexpr double E = 2.718281828459045235360287471352662497757247093699959574966967628;
constexpr double SQRT2 = 1.414213562373095048801688724209698078569671875376948073176679738;
constexpr double DEG_TO_RAD = PI / 180.0; // 角度转弧度系数
constexpr double RAD_TO_DEG = 180.0 / PI; // 弧度转角度系数
// 角度转弧度
inline double toRadians(double degrees)
{
return degrees * DEG_TO_RAD;
}
// 弧度转角度
inline double toDegrees(double radians)
{
return radians * RAD_TO_DEG;
}
// 计算阶乘(n<0返回-1,溢出返回-1)
inline long long fac(int n)
{
if (n < 0) return -1;
if (n == 0 || n == 1) return 1;
long long result = 1;
for (int i = 2; i <= n; ++i)
{
if (result > LLONG_MAX / i) // 防止溢出
return -1;
result *= i;
}
return result;
}
// 快速幂(支持负指数)
double qpow(double base, int exponent)
{
if (exponent == 0) return 1.0;
if (base == 0.0) return 0.0;
bool isNegative = exponent < 0;
unsigned int absExponent = static_cast<unsigned int>(isNegative ? -exponent : exponent);
double result = 1.0;
double current = base;
while (absExponent > 0)
{
if (absExponent % 2 == 1)
result *= current;
current *= current;
absExponent /= 2;
}
return isNegative ? 1.0 / result : result;
}
// 排列数计算(A(a,b) = a!/(a-b)!,非法输入返回-1)
long long A(int a, int b)
{
if (a < 0 || b < 0 || a < b) return -1;
if (b == 0) return 1;
long long result = 1;
for (int i = 0; i < b; ++i)
{
if (result > LLONG_MAX / (a - i)) // 防止溢出
return -1;
result *= (a - i);
}
return result;
}
// 组合数计算(C(a,b) = a!/(b!(a-b)!),非法输入返回-1)
long long C(int a, int b)
{
if (a < 0 || b < 0 || a < b) return -1;
if (b == 0 || a == b) return 1;
// 优化:C(a,b) = C(a,a-b),取较小值减少计算
b = min(b, a - b);
long long result = 1;
for (int i = 1; i <= b; ++i)
{
// 防止溢出:先乘后除(确保整除)
if (result > LLONG_MAX / (a - b + i))
return -1;
result = result * (a - b + i) / i;
}
return result;
}
// 判断素数(优化版:遍历至sqrt(x))
bool isHe(int x)
{
if (x <= 1) return false;
if (x == 2 || x == 3) return true;
if (x % 2 == 0 || x % 3 == 0) return false;
for (int i = 5; i * i <= x; i += 6)
{
if (x % i == 0 || x % (i + 2) == 0)
return false;
}
return true;
}
#endif // _NOMATH_
// ========================== 7. 字符串处理模块 ==========================
// 字符串转为全小写
void lowerstring(string &s)
{
for (auto& c : s)
{
if (c >= 'A' && c <= 'Z')
c += ('a' - 'A');
}
}
// 字符串转为全大写
void uperstring(string &s)
{
for (auto& c : s)
{
if (c >= 'a' && c <= 'z')
c -= ('a' - 'A');
}
}
// 字符串首字母大写,其余小写
void Testsuper(string &s)
{
if (s.empty()) return;
s[0] = toupper(s[0]);
for (size_t i = 1; i < s.size(); ++i)
{
s[i] = tolower(s[i]);
}
}
// 清除输入缓冲区
void flushed()
{
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.clear();
}
// 检查字符串是否为数字(支持整数和小数,可带正负号)
bool isNumber(const string& str)
{
if (str.empty()) return false;
size_t start = 0;
if (str[0] == '+' || str[0] == '-')
start = 1;
bool hasDot = false;
for (size_t i = start; i < str.size(); ++i)
{
if (str[i] == '.')
{
if (hasDot) return false; // 只能有一个小数点
hasDot = true;
}
else if (!isdigit(str[i]))
{
return false;
}
}
// 避免单独的符号或小数点(如"+"、"."、"-.")
return (start < str.size()) && !(start == 1 && str.size() == 1) && !(hasDot && start == str.size() - 1);
}
// 任意类型转为字符串(基于stringstream)
template <typename to_string_T>
string tostring(to_string_T k)
{
stringstream ans;
ans << k;
return ans.str();
}
// ========================== 8. 随机数模块 ==========================
// 生成指定区间整数随机数([a,b])
int rd(int a, int b)
{
if (a > b) swap(a, b);
// 基于当前时间初始化随机种子(避免多次调用导致重复)
static bool seeded = false;
if (!seeded)
{
srand(static_cast<unsigned int>(time(nullptr)) ^ getpid());
seeded = true;
}
return a + (rand() % (b - a + 1));
}
// 生成指定区间浮点数随机数([min,max])
double random(double min, double max)
{
if (min > max) swap(min, max);
static bool seeded = false;
if (!seeded)
{
srand(static_cast<unsigned int>(time(nullptr)) ^ getpid());
seeded = true;
}
return min + (max - min) * (static_cast<double>(rand()) / RAND_MAX);
}
// 生成指定长度的随机字符串(包含数字和大小写字母)
string randS(int length)
{
if (length <= 0) return "";
const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string result;
result.reserve(length);
static bool seeded = false;
if (!seeded)
{
srand(static_cast<unsigned int>(time(nullptr)) ^ getpid());
seeded = true;
}
for (int i = 0; i < length; ++i)
{
result += chars[rand() % chars.size()];
}
return result;
}
// ========================== 9. 日志模块 ==========================
struct logger
{
Save rep;
enum error_type
{
error = 0, // 错误
info = 1, // 信息
warn = 2, // 警告
debug = 3 // 调试
};
// 初始化日志文件(覆盖原有内容)
void reset_logger(const string& path)
{
rep.setpt(path);
rep.save_nol();
if (rep.errorlevel != 1000)
{
rep.save_o << "[" << Gettime(yer) << "/" << Gettime(mon) << "/" << Gettime(dat)
<< "-" << Gettime(hor) << ":" << Gettime(mit) << ":" << Gettime(sec) << "] "
<< "Logger initialized" << endl;
}
}
// 写入日志
void report(const string& s, error_type tp)
{
if (!rep.save_o.good()) return;
// 时间戳(精确到毫秒)
rep.save_o << "[" << Gettime(hor) << ":" << Gettime(mit) << ":" << Gettime(sec)
<< "(" << Gettime(mst) << ")] ";
// 日志类型
switch (tp)
{
case error: rep.save_o << "{type=error} "; break;
case info: rep.save_o << "{type=info} "; break;
case warn: rep.save_o << "{type=warn} "; break;
case debug: rep.save_o << "{type=debug} "; break;
default: rep.save_o << "{type=unknow} ";
}
// 日志内容
rep.save_o << s << endl;
rep.save_o.flush(); // 强制刷新缓冲区
}
// 关闭日志文件
void stop()
{
if (rep.save_o.good())
{
rep.save_o << "[" << Gettime(hor) << ":" << Gettime(mit) << ":" << Gettime(sec)
<< "] Logger stopped" << endl;
}
rep.save_stop();
}
};
// ========================== 10. 系统操作与辅助模块 ==========================
// 执行系统命令
void sys(const string& cmd)
{
system(cmd.c_str());
}
// 无提示暂停(等待按键)
void no_pause()
{
cout << "\033[?25h"; // 显示光标
cout << "Press any key to continue...";
flushed();
getchar();
cout << "\033[?25l"; // 隐藏光标
}
// 标准暂停(带提示)
void pause()
{
system("read -n 1 -s -r -p \"Press any key to continue...\"");
cout << endl;
}
// 类型转换快捷宏
#define to_int(x) stoi(x)
#define to_long(x) stol(x)
#define to_double(x) stod(x)
#define to_str(x) tostring(x)
#define sleep(ms) msSleep(ms)
// 进程退出(带提示)
int ext(int num)
{
cout << "[jtbgunix.h] Process exited with return value " << num << endl;
exit(num);
return num;
}
// 内存单位转换结构体
struct memory
{
long long bytes;
double toB() { return static_cast<double>(bytes); }
double toKB() { return bytes / 1024.0; }
double toMB() { return bytes / (1024.0 * 1024.0); }
double toGB() { return bytes / (1024.0 * 1024.0 * 1024.0); }
double toTB() { return bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0); }
double toPB() { return bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0); }
};
// 内存单位转换全局函数
double rtoB(int bytes) { return static_cast<double>(bytes); }
double rtoKB(int bytes) { return bytes / 1024.0; }
double rtoMB(int bytes) { return bytes / (1024.0 * 1024.0); }
double rtoGB(int bytes) { return bytes / (1024.0 * 1024.0 * 1024.0); }
double rtoTB(int bytes) { return bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0); }
double rtoPB(int bytes) { return bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0); }
// 按字符延迟输出字符串
void say(const string& s, int t)
{
if (t < 0) return;
for (char c : s)
{
cout << c << flush;
msSleep(t);
}
}
// 头文件信息
void jtbgunix_h_about()
{
cout << "jtbgunix.h\n";
cout << "Written by @jiaotangbinggan\n";
cout << "Process started with 2025/10/4\n";
cout << "Version: " << _JTBG_UNIX_VERSION << "\n";
cout << "License: " << _JTBG_LICENSE << "\n";
cout << "Contact:\n";
cout << " Luogu: luogu-1497734\n";
cout << " Email: 197474286@qq.com, guitu1125@yeah.net\n";
cout << "This file hash: none\n";
}
// 关于信息快捷宏
#define about() jtbgunix_h_about()
#endif // _JTBG_UNIX_H_
本文来自博客园,作者:[GuiHua],转载请注明原文链接:https://www.cnblogs.com/pcworld/p/-/h