源碼已經(jīng)更新在CSDN的碼庫里:
git clone https://gitcode.com/funsion/CLua.git
在src文件夾下的lmathlib.c 數(shù)學庫 函數(shù),Standard mathematical library:表明這個C源文件實現(xiàn)了Lua的標準數(shù)學庫(Standard mathematical library),即提供了與數(shù)學相關(guān)的API和功能實現(xiàn)。
增加中文版mathlib數(shù)學函數(shù)名列表,保留英文版mathlib數(shù)學函數(shù)名列表。
原始的代碼為:
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},
{"自然指數(shù)", math_exp},
{"tointeger", math_toint},
{"轉(zhuǎn)整數(shù)", math_toint},
{"floor", math_floor},
{"下整", math_floor},
{"fmod", math_fmod},
{"余數(shù)", math_fmod},
{"ult", math_ult},
{"絕對小", math_ult},
{"log", math_log},
{"自然對數(shù)", math_log},
{"max", math_max},
{"最大值", math_max},
{"min", math_min},
{"最小值", math_min},
{"modf", math_modf},
{"余數(shù)", 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)
/*
* 下面的結(jié)構(gòu)體定義了數(shù)學函數(shù)的映射,用于lua的數(shù)學庫中。
* 每個結(jié)構(gòu)體成員都包含一個函數(shù)名和對應(yīng)的函數(shù)指針。
*/
{"atan2", math_atan}, // 計算給定兩個參數(shù)的反正切值
{"二參反正切", math_atan},
{"cosh", math_cosh}, // 計算 hyperbolic cosinus
{"雙曲余弦", math_cosh},
{"sinh", math_sinh}, // 計算雙曲正弦函數(shù)
{"雙曲正弦", math_sinh},
{"tanh", math_tanh}, // 計算 hyperbolic tangent
{"雙曲正切", math_tanh},
{"pow", math_pow}, // 計算一個數(shù)的另一個數(shù)次冪
{"冪", math_pow}, // 和pow相同,但是使用中文名稱
{"frexp", math_frexp}, // 分解浮點數(shù)為一個 mantissa 和一個 exponent
{"分解浮點數(shù)", math_frexp},
{"ldexp", math_ldexp}, // 根據(jù)給定的 exponent 調(diào)整浮點數(shù)
{"調(diào)整浮點數(shù)", math_ldexp},
{"log10", math_log10}, // 計算給定數(shù)的以10為底的對數(shù)
{"常用對數(shù)", math_log10},
#endif
{"random", NULL},
{"隨機數(shù)", NULL},
{"randomseed", NULL},
{"隨機種子", NULL},
{"pi", NULL},
{"π", NULL},
{"huge", NULL},
{"無窮", NULL},
{"maxinteger", NULL},
{"整大", NULL},
{"mininteger", NULL},
{"整小", NULL},
{NULL, NULL}
};
為了保證中英文協(xié)程函數(shù)都可以加載,以便你可以復制英文原碼來進行更改。所以保留了英文版協(xié)程函數(shù)名列表,這樣就能使用兩種文的函數(shù)。
?? {"pow", math_pow}, ? ? // 計算一個數(shù)的另一個數(shù)次冪
? ?{"冪", math_pow}, ? ? ?// 和pow相同,但是使用中文名稱
其實它們都是加載同樣的庫名,算是加載了2次,以Lua內(nèi)部算法,應(yīng)該只會加載一次。
更改完之后,同樣需要重新編譯Lua的源碼,實現(xiàn)以上列出的關(guān)鍵詞的中文化。
注意,在Window系統(tǒng)下編譯Lua, 最好將所有Lua的源碼,重新保存成ANSI格式的文件,剛下載的默認的源碼會是UTF-8格式的。
這個事情說三遍,文章來源:http://www.zghlxwxcb.cn/news/detail-843698.html
1,不然就會出現(xiàn),Window下的UTF-8源碼可編譯,但Shell里的中文輸出會亂碼。
2,要不然就是Window的ANSI源碼不可編譯(假如你沒做以上步驟),
3,如果是用ANSI格式的源碼編譯的Lua.exe,對應(yīng)的,你在Window下寫的Lua程序也是需要保存成ANSI格式的。這樣就可以在Shell里輸出正確的中文顯示。文章來源地址http://www.zghlxwxcb.cn/news/detail-843698.html
到了這里,關(guān)于Lua中文語言編程源碼-第六節(jié),更改lmathlib.c 數(shù)學庫函數(shù), 使Lua加載中文庫關(guān)鍵詞(與數(shù)學庫相關(guān))的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!