1.代码
#pragma once
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
// mtl(my template library)
namespace mtl
{
void print(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
void println(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
printf("\n");
va_end(args);
}
// 求n个数的和
template <typename T>
double adds(const T &init, const T &value, ...)
{
return (init + value);
}
// 最大值
template <typename T1, typename T2>
double max(T1 a, T2 b)
{
return a > b ? a : b;
}
// 最小值
template <typename T1, typename T2>
double min(T1 a, T2 b)
{
return a < b ? a : b;
}
// 多个数求最大值
template <typename T, typename... Args>
double maxs(T a, Args... args)
{
double max = a;
for (auto &arg : {args...})
max = (arg > max ? arg : max);
return max;
}
// 多个数求最小值
template <typename T, typename... Args>
double mins(T a, Args... args)
{
double min = a;
for (auto &arg : {args...})
min = (arg < min ? arg : max);
return min;
}
// 高斯求和
long gaussian_sum(int n)
{
int sum = 0;
for (int i = 0; i <= n; i++)
{
sum += i;
}
return sum;
}
bool isPalindromic(const char *str)
{
int len = strlen(str);
for (int i = 0; i < len / 2; i++)
{
if (str[i] != str[len - i - 1])
return false;
}
return true;
}
bool isPalindromic(int num)
{
char str[20];
sprintf(str, "%d", num);
return isPalindromic(str);
}
#define noerr noexcept
#define noerror noexcept
#define lower_list "abcdefghijklmnopqrstuvwxyz"
#define upper_list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define number_list "0123456789"
#define symbol_list "!@#$%^&*()_+-=[]\\{}|;':\",./<>?"
#define ascii_list "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]\\{}|;':\",./<>?"
#define ascii_len 94
#define all_ascii_len 128
#define int_max 2147483647
#define int_min -2147483648
#define short_max 32767
#define short_min -32768
#define long_max 9223372036854775807
#define long_min -9223372036854775808
#define long_long_max 9223372036854775807
#define long_long_min -9223372036854775808
#define double_max 1.7976931348623158e+308
#define double_min -1.7976931348623158e+308
#define float_max 3.402823466e+38
#define float_min -3.402823466e+38
#define double_max 1.7976931348623158e+308
#define double_min -1.7976931348623158e+308
#define bool_true 1
#define bool_false 0
#define null 0
#define null_str ""
#define float_precision 6
#define double_precision 15
#define int_precision 10
#define short_precision 5
#define long_precision 20
#define long_long_precision 20
#define char_precision 1
inline namespace types
{
// 类型类
enum type_t
{
unsigned_int_t = 0,
signed_int_t,
unsigned_short_t,
signed_short_t,
unsigned_long_t,
signed_long_t,
unsigned_long_long_t,
signed_long_long_t,
float_t,
double_t,
long_double_t,
char_t
};
// 类型判断
type_t type(unsigned int a)
{
return mtl::types::type_t::unsigned_int_t;
}
type_t type(signed int a)
{
return mtl::types::type_t::signed_int_t;
}
type_t type(unsigned short a)
{
return mtl::types::type_t::unsigned_short_t;
}
type_t type(signed short a)
{
return mtl::types::type_t::signed_short_t;
}
type_t type(unsigned long a)
{
return mtl::types::type_t::unsigned_long_t;;
}
type_t type(signed long a)
{
return mtl::types::type_t::signed_long_t;
}
type_t type(unsigned long long a)
{
return mtl::types::type_t::unsigned_long_long_t;
}
type_t type(signed long long a)
{
return mtl::types::type_t::signed_long_long_t;
}
type_t type(float a)
{
return mtl::types::type_t::float_t;
}
type_t type(double a)
{
return mtl::types::type_t::double_t;
}
type_t type(long double a)
{
return mtl::types::type_t::long_double_t;
}
type_t type(char a)
{
return mtl::types::type_t::char_t;
}
// 类型
typedef unsigned int uint_t;
typedef signed int sint_t;
typedef unsigned int *puint_t;
typedef signed int *psint_t;
typedef int default_t;
typedef int *pdefault_t;
typedef unsigned short ushort_t;
typedef signed short sshort_t;
typedef unsigned short *pushort_t;
typedef signed short *psshort_t;
typedef short small_t;
typedef short *psmall_t;
typedef unsigned long ulong_t;
typedef signed long slong_t;
typedef unsigned long *pulong_t;
typedef signed long *pslong_t;
typedef long big_t;
typedef long *pbig_t;
typedef unsigned long long ulong_long_t;
typedef signed long long slong_long_t;
typedef unsigned long long *pulong_long_t;
typedef signed long long *pslong_long_long_t;
typedef long long biggest_long_t;
typedef long long biggest_long_long_t;
typedef float decimal_t;
typedef float *pdecimal_t;
typedef double big_decimal_t;
typedef double *pbig_decimal_t;
typedef long double biggest_decimal_t;
typedef long double *pbiggest_decimal_t;
typedef unsigned char uchar_t;
typedef signed char schar_t;
typedef char charecter_t;
typedef char *str_t;
typedef char *pchar_t;
typedef char16_t unicode16_t;
typedef char32_t unicode32_t;
// c++20后定义了char8_t,使用UTF-8
#if defined(__cplusplus) && __cplusplus >= 202002L
typedef char8_t unicode8_t;
#endif
}
inline namespace number
{
const double pi = 3.14159265358979323846264338327950288419716939937510;
const double tua = 6.28318530717958647692135328626645058223172535940812;
const double e = 2.71828182845904523536028747135266249775724709369995;
const double phi = 1.61803398874989484820458683436563811772030917980576;
}
inline namespace math
{
// 正方形面积
int squareArea(int a)
{
return a * a;
}
// 正方形周长
int squarePerimeter(int a)
{
return a * 4;
}
// 矩形面积
int rectangleArea(int a, int b)
{
return a * b;
}
// 矩形周长
int rectanglePerimeter(int a, int b)
{
return (a + b) * 2;
}
// BMI
int BMI(int high, int kg)
{
return (kg / high) * (kg / high);
}
// 圆的面积
double circularArea(int r)
{
return pi * r * r;
}
// 圆的周长
double circlePerimeter(int r)
{
return 2 * pi * r;
}
// 三角形面积
int triangleArea(int a, int b)
{
return a * b / 2;
}
// 三角形周长
int trianglePerimeter(int a, int b, int c)
{
return a + b + c;
}
// 梯形面积
int trapezoidalArea(int a, int b, int c)
{
return (a + b) * c / 2;
}
// 梯形周长
int trapezoidalPerimeter(int a, int b, int c, int d)
{
return a + b + c + d;
}
// 加法
int add(int a, int b)
{
return a + b;
}
// 减法
int sub(int a, int b)
{
return a - b;
}
// 乘法
int mul(int a, int b)
{
return a * b;
}
// 除法
int div(int a, int b)
{
return a / b;
}
// 取余
int mod(int a, int b)
{
return a % b;
}
// 幂运算
double pow(double a, double b)
{
for (int i = 0; i < b; i++)
a *= a;
return a;
}
// 斐波那契数列
int fib(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
return fib(n - 1) + fib(n - 2);
}
// 最大公约数
int gcd(int a, int b)
{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
// 最小公倍数
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
}
inline namespace charecter
{
// 判断是否为字母
bool isAlpha(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// 判断是否为数字
bool isDigit(char c)
{
return c >= '0' && c <= '9';
}
// 判断是否为字母或数字
bool isAlnum(char c)
{
return isAlpha(c) || isDigit(c);
}
// 判断是否为空格
bool isSpace(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
// 判断是否为小写字母
bool isLower(char c)
{
return c >= 'a' && c <= 'z';
}
// 判断是否为大写字母
bool isUpper(char c)
{
return c >= 'A' && c <= 'Z';
}
// 判断是否为字母
bool isLetter(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// 将字符转换为ASCII码
int toAscii(char c)
{
return int(c);
}
// 将字符转换为小写字母
char toLower(char c)
{
return isUpper(c) ? (c - 'A' + 'a') : c;
}
// 将字符转换为大写字母
char toUpper(char c)
{
return isLower(c) ? (c - 'a' + 'A') : c;
}
// 将ASCII码转换为字符
char toChar(int c)
{
return char(c);
}
}
namespace informations
{
void Mental_state()
{
mtl::println(R"(
⠀⠀⠀⠀ ⣶⣿⣿⣿⣿⣿⣿⣿⣿⣄
⣰⣿⣿⣿⡿⠟⠛⠛⠛⠿⣿⣿⣿
⠀⠀⢸⣿⣿⣥⣤⠀⠀⠀⣀⣀⠀⠘⣿⡏
⠀⠀⠈⡟⠿⠶⠂⠀⠀⠀⠶⠆⠀⠀⣿⠃
⠀⠀⠀⣷⡀⠀⣀⣀⠀⠀⠀⠀⠀⠀⠂
⠀⠀⠀⠀⢻⣦⣤⣥⣀
⠀⠀⠀⠀⠀⣿⣿⣛⡁⠀⠀⠀⠀⡀
⠀⣀⣴⠃⠘⣿⣿⣿⡿⠀⠀⠀⠀⣼⣿⣶⣄
⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⢀⣠⣾⣿⣿⣿⣿)");
}
void version() {
mtl::println("MTL Version: 1.0.0");
}
}
}
//
// ⠀⠀⠀⠀ ⣶⣿⣿⣿⣿⣿⣿⣿⣿⣄
// ⠀⠀⣰⣿⣿⣿⡿⠟⠛⠛⠛⠿⣿⣿⣿
// ⠀⠀⢸⣿⣿⣥⣤⠀⠀⠀⣀⣀⠀⠘⣿⡏
// ⠀⠀⠈⡟⠿⠶⠂⠀⠀⠀⠶⠆⠀⠀⣿⠃
// ⠀⠀⠀⣷⡀⠀⣀⣀⠀⠀⠀⠀⠀⠀⠂
// ⠀⠀⠀⠀⢻⣦⣤⣥⣀
// ⠀⠀⠀⠀⠀⣿⣿⣛⡁⠀⠀⠀⠀⡀
// ⠀⣀⣴⠃⠘⣿⣿⣿⡿⠀⠀⠀⠀⣼⣿⣶⣄
// ⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⢀⣠⣾⣿⣿⣿⣿
//
2. 注释文档
1.函数
print: 打印函数
- 用法:
print(const char*,...);
- 示例:
print("a=%d,b=%d",1,3);
- 输出:
a=1,b=3
println: 打印函数
- 用法:
println(const char*,...);
- 示例:
print("a=%d,b=%d",1,3)
- 输出:
a=1,b=3
adds: 求n个数的和
- 用法:
adds(int,...);
- 示例:
adds(1,2,3,4,5);
- 返回:
15
max: 最大值
- 用法:
max(int,int);
- 示例:
max(1,2);
- 返回:
2
min: 最小值
- 用法:
min(int,int);
- 示例:
min(1,2);
- 返回:
1
maxs: 多个数求最大值
- 用法:
maxs(int,...);
- 示例:
maxs(1,2,3,4,5);
- 返回:
5
mins: 多个数求最小值
- 用法:
mins(int,...);
- 示例:
mins(1,2,3,4,5);
- 返回:
1
gaussian_sum: 高斯求和
- 用法:
gaussian_sum(int);
- 示例:
gaussian_sum(5);
- 返回:
15
isPalindromic: 判断是否为回文
- 用法:
isPalindromic(const char*);
isPalindromic(int);
- 示例:
isPalindromic("abcba");
isPalindromic(123321);
- 返回:
true
true
2.宏
noerr
- 值:
noexcept
noerror
- 值:
noexcept
lower_list : 小写列表
- 值:
"abcdefghijklmnopqrstuvwxyz"
upper_list : 大写列表
- 值:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
number_list : 数字列表
- 值:
"0123456789"
symbol_list : 符号列表
- 值:
"!@#$%^&*()_+-=[]\\{}|;':\",./<>?"
ascii_list : ASCII列表
- 值:
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]\\{}|;':\",./<>?"
ascii_len: ASCII列表长度
- 值:
94
all_ascii_len: 全部ASCII列表长度
- 值:
128
int_max: int最大值
- 值:
2147483647
int_min: int最小值
- 值:
-2147483648
short_max: short最大值
- 值:
32767
short_min: short最小值
- 值:
-32768
long_max: long最大值
- 值:
9223372036854775807
long_min: long最小值
- 值:
-9223372036854775808
long_long_max: long long最大值
- 值:
9223372036854775807
long_long_min: long long最小值
- 值:
-9223372036854775808
float_max: float最大值
- 值:
3.4028234663852886e+38
float_min: float最小值
- 值:
1.1754943508222875e-38
double_max: double最大值
- 值:
1.7976931348623157e+308
double_min: double最小值
- 值:
2.2250738585072014e-308
bool_true: bool真值
- 值:
true
bool_false: bool假值
- 值:
false
null: 空值
- 值:
null
null_str
- 值:
""
float_precision: float精度
- 值:
6
double_precision: double精度
- 值:
15
int_precision: int精度
- 值:
10
short_precision: short精度
- 值:
5
long_precision: long精度
- 值:
20
long_long_precision: long long精度
- 值:
20
3.命名空间
types: 类型
type: 类型判断
- 用法:
type(unsigned int);
type(signed int);
type(unsigned short);
type(signed short);
type(unsigned long);
type(signed long);
type(unsigned long long);
type(signed long long);
type(float);
type(double);
type(long double);
type(char);
- 示例:
type(1);
type(1.0);
type('a');
type(1.0f);
type(1.0l);
- 返回:
mtl::types::type_t::signed_int_t
mtl::types::type_t::double_t
mtl::types::type_t::char_t
mtl::types::type_t::float_t
mtl::types::type_t::long_double_t
类型
- 代码片段:
typedef unsigned int uint_t;typedef signed int sint_t;
typedef unsigned int *puint_t;
typedef signed int *psint_t;
typedef int default_t;
typedef int *pdefault_t;
typedef unsigned short ushort_t;
typedef signed short sshort_t;
typedef unsigned short *pushort_t;
typedef signed short *psshort_t;
typedef short small_t;
typedef short* psmall_t;
typedef unsigned long ulong_t;
typedef signed long slong_t;
typedef unsigned long *pulong_t;
typedef signed long *pslong_t;
typedef long big_t;
typedef long* pbig_t;
typedef unsigned long long ulong_long_t;
typedef signed long long slong_long_t;
typedef unsigned long long *pulong_long_t;
typedef signed long long *pslong_long_long_t;
typedef long long biggest_long_t;
typedef long long biggest_long_long_t;
typedef float decimal_t;
typedef float *pdecimal_t;
typedef double big_decimal_t;
typedef double *pbig_decimal_t;
typedef long double biggest_decimal_t;
typedef long double *pbiggest_decimal_t;
typedef unsigned char uchar_t;
typedef signed char schar_t;
typedef char charecter_t;
typedef char *str_t;
typedef char *pchar_t;
typedef char16_t unicode16_t;
typedef char32_t unicode32_t;
// c++20后定义了char8_t,使用UTF-8
#if defined(__cplusplus) && __cplusplus >= 202002L
typedef char8_t unicode8_t;
#endif
- 示例:
uint a;//unsigned int
sint b;//signed int
psint c;//signed int*
number: 数字
常量
- pi :3.1415926535…
- tua:2.7182818284…
- e :2.7182818284…
- phi:1.6180339887…
math: 数学
squareArea: 正方形面积
- 用法:
squareArea(int);
- 示例:
squareArea(2);
- 返回:
4
squarePerimeter: 正方形周长
- 用法:
squarePerimeter(int);
- 示例:
squarePerimeter(2);
- 返回:
8
rectangleArea:矩形面积
- 用法:
rectangleArea(int,int);
- 示例:
rectangleArea(2,3);
- 返回:
6
rectanglePerimeter:矩形周长
- 用法:
rectanglePerimeter(int,int);
- 示例:
rectanglePerimeter(2,3);
- 返回:
10
BIM
- 用法:
BIM(int,int);
- 示例:
BIM(6,2);
- 返回:
9
circularArea:圆面积
- 用法:
circularArea(int);
- 示例:
circularArea(2);
- 返回:
12.566370614359172
circlePerimeter:圆周长
- 用法:
circlePerimeter(int);
- 示例:
circlePerimeter(2);
- 返回:
12.566370614359172
triangleArea: 三角形面积
- 用法:
triangleArea(int,int);
- 示例:
triangleArea(2,3);
- 返回:
3
trianglePerimeter: 三角形周长
- 用法:
trianglePerimeter(int,int,int);
- 示例:
trianglePerimeter(2,3,4);
- 返回:
9
trapezoidalArea: 梯形面积
- 用法:
trapezoidalArea(int,int,int,);
- 示例:
trapezoidalArea(2,3,4);
- 返回:
8
trapezoidalPerimeter:梯形周长
- 用法:
trapezoidalPerimeter(int,int,int,int);
- 示例:
trapezoidalPerimeter(2,3,4,5);
- 返回:
14
add: 加法
- 用法:
add(int,int);
- 示例:
add(2,3);
返回:
5
sub: 减法
- 用法:
sub(int,int);
- 示例:
sub(2,3);
- 返回:
-1
mul: 乘法
- 用法:
mul(int,int);
- 示例:
mul(2,3);
- 返回:
6
div: 除法
- 用法:
div(int,int);
- 示例:
div(2,3);
- 返回:
0
mod: 取余
- 用法:
mod(int,int);
- 示例:
mod(2,3);
- 返回:
2
pow: 幂
- 用法:
pow(int,int);
- 示例:
pow(2,3);
- 返回:
8
fib: 斐波那契数列
- 用法:
fib(int);
- 示例:
fib(10);
- 返回:
55
gcd: 最大公约数
- 用法:
gcd(int,int);
- 示例:
gcd(10,20);
- 返回:
10
lcm: 最小公倍数
- 用法:
lcm(int,int);
- 示例:
lcm(10,20);
- 返回:
20
charecter: 字符处理
isAlpha: 判断是否为字母
- 用法:
isAlpha(char);
- 示例:
isAlpha('a');
- 返回:
true
isDigit: 判断是否为数字
- 用法:
isDigit(char);
- 示例:
isDigit('1');
- 返回:
true
isAlNum: 判断是否为字母或数字
- 用法:
isAlNum(char);
- 示例:
isAlNum('a');
- 返回:
true
isSpace: 判断是否为空格
- 用法:
isSpace(char);
- 示例:
isSpace(' ');
- 返回:
true
isLower: 判断是否为小写字母
- 用法:
isLower(char);
- 示例:
isLower('a');
- 返回:
true
isUpper: 判断是否为大写字母
- 用法:
isUpper(char);
- 示例:
isUpper('A');
- 返回:
true
isLetter: 判断是否为字母
- 用法:
isLetter(char);
- 示例:
isLetter('a');
isLetter('A');
isLetter('1');
isLetter(' ');
- 返回:
true
true
false
false
toAscii(char): 将字符转换为ASCII码
- 用法:
toAscii(char);
- 示例:
toAscii('A');
toAscii('a');
- 返回:
65
97
toChar(int): 将ASCII码转换为字符
- 用法:
toChar(int);
- 示例:
toChar(65);
- 返回:
A
toLower(char): 将字符转换为小写
- 用法:
toLower(char);
- 示例:
toLower('A');
toLower('a');
- 返回:
a
a
toUpper(char): 将字符转换为大写
- 用法:
toUpper(char);
- 示例:
toUpper('A');
toUpper('a');
- 返回:
A
A