|
| 1 | +-- 计算器插件 |
| 2 | +-- author: https://github.com/ChaosAlphard |
| 3 | +local calc = {} |
| 4 | + |
| 5 | +function calc.init( env ) |
| 6 | + local config = env.engine.schema.config |
| 7 | + env.prefix = config:get_string( 'calculator/prefix' ) or 'cC' |
| 8 | + env.show_prefix = config:get_bool( 'calculator/show_prefix' ) -- set to true to show prefix in preedit area |
| 9 | + -- env.decimalPlaces = config:get_string('calculator/decimalPlaces') or '4' |
| 10 | +end |
| 11 | + |
| 12 | +local function startsWith( str, start ) return string.sub( str, 1, string.len( start ) ) == start end |
| 13 | + |
| 14 | +local function truncateFromStart( str, truncateStr ) return string.sub( str, string.len( truncateStr ) + 1 ) end |
| 15 | + |
| 16 | +local function yield_calc_cand( seg, cand_text, cand_comment, cand_preedit, show_prefix ) |
| 17 | + local cand = Candidate( 'calc', seg.start, seg._end, cand_text, cand_comment ) |
| 18 | + cand.quality = 99999 |
| 19 | + if not show_prefix then cand.preedit = cand_preedit end |
| 20 | + yield( cand ) |
| 21 | +end |
| 22 | + |
| 23 | +-- 函数表 |
| 24 | +local calcPlugin = { |
| 25 | + -- e, exp(1) = e^1 = e |
| 26 | + e = math.exp( 1 ), |
| 27 | + -- π |
| 28 | + pi = math.pi, |
| 29 | +} |
| 30 | + |
| 31 | +-- random([m [,n ]]) 返回m-n之间的随机数, n为空则返回1-m之间, 都为空则返回0-1之间的小数 |
| 32 | +local function random( ... ) return math.random( ... ) end |
| 33 | +-- 注册到函数表中 |
| 34 | +calcPlugin['rdm'] = random |
| 35 | +calcPlugin['random'] = random |
| 36 | + |
| 37 | +-- 正弦 |
| 38 | +local function sin( x ) return math.sin( x ) end |
| 39 | +calcPlugin['sin'] = sin |
| 40 | + |
| 41 | +-- 双曲正弦 |
| 42 | +local function sinh( x ) return math.sinh( x ) end |
| 43 | +calcPlugin['sinh'] = sinh |
| 44 | + |
| 45 | +-- 反正弦 |
| 46 | +local function asin( x ) return math.asin( x ) end |
| 47 | +calcPlugin['asin'] = asin |
| 48 | + |
| 49 | +-- 余弦 |
| 50 | +local function cos( x ) return math.cos( x ) end |
| 51 | +calcPlugin['cos'] = cos |
| 52 | + |
| 53 | +-- 双曲余弦 |
| 54 | +local function cosh( x ) return math.cosh( x ) end |
| 55 | +calcPlugin['cosh'] = cosh |
| 56 | + |
| 57 | +-- 反余弦 |
| 58 | +local function acos( x ) return math.acos( x ) end |
| 59 | +calcPlugin['acos'] = acos |
| 60 | + |
| 61 | +-- 正切 |
| 62 | +local function tan( x ) return math.tan( x ) end |
| 63 | +calcPlugin['tan'] = tan |
| 64 | + |
| 65 | +-- 双曲正切 |
| 66 | +local function tanh( x ) return math.tanh( x ) end |
| 67 | +calcPlugin['tanh'] = tanh |
| 68 | + |
| 69 | +-- 反正切 |
| 70 | +-- 返回以弧度为单位的点相对于x轴的逆时针角度。x是点的横纵坐标比值 |
| 71 | +-- 返回范围从−π到π (以弧度为单位),其中负角度表示向下旋转,正角度表示向上旋转 |
| 72 | +local function atan( x ) return math.atan( x ) end |
| 73 | +calcPlugin['atan'] = atan |
| 74 | + |
| 75 | +-- 反正切 |
| 76 | +-- atan( y/x ) = atan2(y, x) |
| 77 | +-- 返回以弧度为单位的点相对于x轴的逆时针角度。y是点的纵坐标,x是点的横坐标 |
| 78 | +-- 返回范围从−π到π (以弧度为单位),其中负角度表示向下旋转,正角度表示向上旋转 |
| 79 | +-- 与 math.atan(y/x) 函数相比,具有更好的数学定义,因为它能够正确处理边界情况(例如x=0) |
| 80 | +local function atan2( y, x ) return math.atan2( y, x ) end |
| 81 | +calcPlugin['atan2'] = atan2 |
| 82 | + |
| 83 | +-- 将角度从弧度转换为度 e.g. deg(π) = 180 |
| 84 | +local function deg( x ) return math.deg( x ) end |
| 85 | +calcPlugin['deg'] = deg |
| 86 | + |
| 87 | +-- 将角度从度转换为弧度 e.g. rad(180) = π |
| 88 | +local function rad( x ) return math.rad( x ) end |
| 89 | +calcPlugin['rad'] = rad |
| 90 | + |
| 91 | +-- 返回两个值, 无法参与运算后续, 只能单独使用 |
| 92 | +-- 返回m,e 使得x = m * 2^e |
| 93 | +local function frexp( x ) |
| 94 | + local m, e = math.frexp( x ) |
| 95 | + return m .. ' * 2^' .. e |
| 96 | +end |
| 97 | +calcPlugin['frexp'] = frexp |
| 98 | + |
| 99 | +-- 返回 x * 2^y |
| 100 | +local function ldexp( x, y ) return math.ldexp( x, y ) end |
| 101 | +calcPlugin['ldexp'] = ldexp |
| 102 | + |
| 103 | +-- 返回 e^x |
| 104 | +local function exp( x ) return math.exp( x ) end |
| 105 | +calcPlugin['exp'] = exp |
| 106 | + |
| 107 | +-- 返回x的平方根 e.g. sqrt(x) = x^0.5 |
| 108 | +local function sqrt( x ) return math.sqrt( x ) end |
| 109 | +calcPlugin['sqrt'] = sqrt |
| 110 | + |
| 111 | +-- y为底x的对数, 使用换底公式实现 |
| 112 | +local function log( y, x ) |
| 113 | + -- 不能为负数或0 |
| 114 | + if x <= 0 or y <= 0 then return nil end |
| 115 | + |
| 116 | + return math.log( x ) / math.log( y ) |
| 117 | +end |
| 118 | +calcPlugin['log'] = log |
| 119 | + |
| 120 | +-- e为底x的对数 |
| 121 | +local function loge( x ) |
| 122 | + -- 不能为负数或0 |
| 123 | + if x <= 0 then return nil end |
| 124 | + |
| 125 | + return math.log( x ) |
| 126 | +end |
| 127 | +calcPlugin['loge'] = loge |
| 128 | + |
| 129 | +-- 10为底x的对数 |
| 130 | +local function log10( x ) |
| 131 | + -- 不能为负数或0 |
| 132 | + if x <= 0 then return nil end |
| 133 | + |
| 134 | + return math.log10( x ) |
| 135 | +end |
| 136 | +calcPlugin['log10'] = log10 |
| 137 | + |
| 138 | +-- 平均值 |
| 139 | +local function avg( ... ) |
| 140 | + local data = { ... } |
| 141 | + local n = select( '#', ... ) |
| 142 | + -- 样本数量不能为0 |
| 143 | + if n == 0 then return nil end |
| 144 | + |
| 145 | + -- 计算总和 |
| 146 | + local sum = 0 |
| 147 | + for _, value in ipairs( data ) do sum = sum + value end |
| 148 | + |
| 149 | + return sum / n |
| 150 | +end |
| 151 | +calcPlugin['avg'] = avg |
| 152 | + |
| 153 | +-- 方差 |
| 154 | +local function variance( ... ) |
| 155 | + local data = { ... } |
| 156 | + local n = select( '#', ... ) |
| 157 | + -- 样本数量不能为0 |
| 158 | + if n == 0 then return nil end |
| 159 | + |
| 160 | + -- 计算均值 |
| 161 | + local sum = 0 |
| 162 | + for _, value in ipairs( data ) do sum = sum + value end |
| 163 | + local mean = sum / n |
| 164 | + |
| 165 | + -- 计算方差 |
| 166 | + local sum_squared_diff = 0 |
| 167 | + for _, value in ipairs( data ) do sum_squared_diff = sum_squared_diff + (value - mean) ^ 2 end |
| 168 | + |
| 169 | + return sum_squared_diff / n |
| 170 | +end |
| 171 | +calcPlugin['var'] = variance |
| 172 | + |
| 173 | +-- 阶乘 |
| 174 | +local function factorial( x ) |
| 175 | + -- 不能为负数 |
| 176 | + if x < 0 then return nil end |
| 177 | + if x == 0 or x == 1 then return 1 end |
| 178 | + |
| 179 | + local result = 1 |
| 180 | + for i = 1, x do result = result * i end |
| 181 | + |
| 182 | + return result |
| 183 | +end |
| 184 | +calcPlugin['fact'] = factorial |
| 185 | + |
| 186 | +-- 实现阶乘计算(!) |
| 187 | +local function replaceToFactorial( str ) |
| 188 | + -- 替换[0-9]!字符为fact([0-9])以实现阶乘 |
| 189 | + return str:gsub( '([0-9]+)!', 'fact(%1)' ) |
| 190 | +end |
| 191 | + |
| 192 | +-- 简单计算器 |
| 193 | +function calc.func( input, seg, env ) |
| 194 | + if not seg:has_tag( 'calculator' ) or input == '' then return end |
| 195 | + -- 提取算式 |
| 196 | + local express = truncateFromStart( input, env.prefix ) |
| 197 | + if express == '' then return end -- 防止用户写错了正则表达式造成错误 |
| 198 | + local code = replaceToFactorial( express ) |
| 199 | + local success, result = pcall( load( 'return ' .. code, 'calculate', 't', calcPlugin ) ) |
| 200 | + if success and result and (type( result ) == 'string' or type( result ) == 'number') and #tostring( result ) > 0 then |
| 201 | + yield_calc_cand( seg, result, '', express, env.show_prefix ) |
| 202 | + yield_calc_cand( seg, express .. '=' .. result, '', express, env.show_prefix ) |
| 203 | + else |
| 204 | + yield_calc_cand( seg, express, '解析失败', express, env.show_prefix ) |
| 205 | + yield_calc_cand( seg, code, '入参', express, env.show_prefix ) |
| 206 | + end |
| 207 | +end |
| 208 | + |
| 209 | +return calc |
0 commit comments