本文主要是介绍Lua中文语言编程源码-第六节,更改lmathlib.c 数学库函数, 使Lua加载中文库关键词(与数学库相关),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
源码已经更新在CSDN的码库里:
git clone https://gitcode.com/funsion/CLua.git
在src文件夹下的lmathlib.c 数学库 函数,Standard mathematical library:表明这个C源文件实现了Lua的标准数学库(Standard mathematical library),即提供了与数学相关的API和功能实现。
增加中文版mathlib数学函数名列表,保留英文版mathlib数学函数名列表。
原始的代码为:
static const luaL_Reg mathlib[] = {{"abs", math_abs},{"acos", math_acos},{"asin", math_asin},{"atan", math_atan},{"ceil", math_ceil},{"cos", math_cos},{"deg", math_deg},{"exp", math_exp},{"tointeger", math_toint},{"floor", math_floor},{"fmod", math_fmod},{"ult", math_ult},{"log", math_log},{"max", math_max},{"min", math_min},{"modf", math_modf},{"rad", math_rad},{"sin", math_sin},{"sqrt", math_sqrt},{"tan", math_tan},{"type", math_type},
#if defined(LUA_COMPAT_MATHLIB){"atan2", math_atan},{"cosh", math_cosh},{"sinh", math_sinh},{"tanh", math_tanh},{"pow", math_pow},{"frexp", math_frexp},{"ldexp", math_ldexp},{"log10", math_log10},
#endif/* placeholders */{"random", NULL},{"randomseed", NULL},{"pi", NULL},{"huge", NULL},{"maxinteger", NULL},{"mininteger", NULL},{NULL, NULL}
};
更改成以下代码:
static const luaL_Reg mathlib[] = {{"abs", math_abs},{"绝对值", math_abs},{"acos", math_acos},{"反余弦", math_acos},{"asin", math_asin},{"反正弦", math_asin},{"atan", math_atan},{"反正切", math_atan},{"ceil", math_ceil},{"上整", math_ceil},{"cos", math_cos},{"余弦", math_cos},{"deg", math_deg},{"角度", math_deg},{"exp", math_exp},{"自然指数", math_exp},{"tointeger", math_toint},{"转整数", math_toint},{"floor", math_floor},{"下整", math_floor},{"fmod", math_fmod},{"余数", math_fmod},{"ult", math_ult},{"绝对小", math_ult},{"log", math_log},{"自然对数", math_log},{"max", math_max},{"最大值", math_max},{"min", math_min},{"最小值", math_min},{"modf", math_modf},{"余数", math_modf},{"rad", math_rad},{"弧度", math_rad},{"sin", math_sin},{"正弦", math_sin},{"sqrt", math_sqrt},{"开方", math_sqrt},{"tan", math_tan},{"正切", math_tan},{"type", math_type},{"类", math_type},
#if defined(LUA_COMPAT_MATHLIB)/** 下面的结构体定义了数学函数的映射,用于lua的数学库中。* 每个结构体成员都包含一个函数名和对应的函数指针。*/{"atan2", math_atan}, // 计算给定两个参数的反正切值{"二参反正切", math_atan},{"cosh", math_cosh}, // 计算 hyperbolic cosinus{"双曲余弦", math_cosh},{"sinh", math_sinh}, // 计算双曲正弦函数{"双曲正弦", math_sinh},{"tanh", math_tanh}, // 计算 hyperbolic tangent{"双曲正切", math_tanh},{"pow", math_pow}, // 计算一个数的另一个数次幂{"幂", math_pow}, // 和pow相同,但是使用中文名称{"frexp", math_frexp}, // 分解浮点数为一个 mantissa 和一个 exponent{"分解浮点数", math_frexp},{"ldexp", math_ldexp}, // 根据给定的 exponent 调整浮点数{"调整浮点数", math_ldexp},{"log10", math_log10}, // 计算给定数的以10为底的对数{"常用对数", math_log10},
#endif{"random", NULL},{"随机数", NULL},{"randomseed", NULL},{"随机种子", NULL},{"pi", NULL},{"π", NULL},{"huge", NULL},{"无穷", NULL},{"maxinteger", NULL},{"整大", NULL},{"mininteger", NULL},{"整小", NULL},{NULL, NULL}
};
为了保证中英文协程函数都可以加载,以便你可以复制英文原码来进行更改。所以保留了英文版协程函数名列表,这样就能使用两种文的函数。
{"pow", math_pow}, // 计算一个数的另一个数次幂
{"幂", math_pow}, // 和pow相同,但是使用中文名称
其实它们都是加载同样的库名,算是加载了2次,以Lua内部算法,应该只会加载一次。
更改完之后,同样需要重新编译Lua的源码,实现以上列出的关键词的中文化。
注意,在Window系统下编译Lua, 最好将所有Lua的源码,重新保存成ANSI格式的文件,刚下载的默认的源码会是UTF-8格式的。
这个事情说三遍,
1,不然就会出现,Window下的UTF-8源码可编译,但Shell里的中文输出会乱码。
2,要不然就是Window的ANSI源码不可编译(假如你没做以上步骤),
3,如果是用ANSI格式的源码编译的Lua.exe,对应的,你在Window下写的Lua程序也是需要保存成ANSI格式的。这样就可以在Shell里输出正确的中文显示。
这篇关于Lua中文语言编程源码-第六节,更改lmathlib.c 数学库函数, 使Lua加载中文库关键词(与数学库相关)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!