模組:NumberUtil
外观
local p={}
local lib_arg={};
local yesno = {}
function p.foreachNumber(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {parentFirst=true})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local input_article = args[1] or args['1'] or ''
local input_calc = args[2] or args['2'] or '$1'
local pattern = '#expr:' .. mw.ustring.gsub(input_calc,'%%(%d+)','%%%%%1') .. '}}'
if mw.isSubsting() then pattern = 'safesubst:' .. pattern end
pattern = '{{' .. pattern
pattern = mw.ustring.gsub(pattern,'%$','(%%1)')
local result = mw.ustring.gsub(input_article,'(%-?[%d%.]+)',pattern)
return working_frame:preprocess( result )
end
function p.forrangeNumber(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {
parentFirst=true,
trim = false,
removeBlanks = false
})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local expr=args[4] or args['4'] or 'x'
local var_name=args[5] or args['5'] or 'x'
local var_str=args[6] or args['6'] or 'x'
var_str = mw.ustring.gsub(var_str,"%%","%%%%");
expr = mw.ustring.gsub(expr,"%a+",function(str)
if str==var_name then return "("..var_str..")" end
end)
expr = mw.ustring.gsub(expr,"[質质素][數数]","primeIndexAtModuleFactorization")
local output_str=args[1] or args['1'] or '$1,'
local start_n = tonumber(args[2] or args['2'] or args.min or '1')or 1
local end_n = tonumber(args[3] or args['3'] or args.max or '10')or 10
local math_lib = math
local tonumber_func = tonumber
if args.class then
local comp_number;
local num_class = mw.ustring.lower(args.class)
if num_class == 'cmath' then
if comp_number == nil then comp_number = require("Module:Complex Number") end
math_lib = comp_number.cmath.init()
tonumber_func = math_lib.toComplexNumber
elseif num_class == 'qmath' then
if comp_number == nil then comp_number = require("Module:Complex Number") end
math_lib = comp_number.qmath.init()
tonumber_func = math_lib.toQuaternionNumber
end
end
if args.delnowiki then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
if yesno(args.delnowiki or 'no') then
output_str = mw.text.unstripNoWiki( output_str )
end
end
if args.delmsgnw then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
if yesno(args.delmsgnw or 'no') then
output_str = mw.text.decode( output_str )
end
end
local num_arr=mw.text.split(require("Module:Complex Number/Calculate")._functionGraph({expr},
start_n,end_n,math.abs(start_n-end_n), nil, nil, {useOtherModule="yes",last1=args.last1,last2=args.last2},math_lib,tonumber_func).y1,',')
local body=""
for i=1,#num_arr do
body = body .. mw.ustring.gsub(output_str,"(%$[+-]?[%d]*)",function(str)
local tail = ''
if str=="$+" or str=="$-" then tail = mw.ustring.sub(str,-1) end
local check, num = mw.ustring.find(str,"[+-]?[%d]+")
if check then
num = tonumber(mw.ustring.sub(str, check, num) or '') or 1
else
num = 1
end
local get_val = tonumber_func(num_arr[num+i-1])
if mw.text.trim(tostring(get_val or '')) ~= '' then return tostring(get_val) .. tail end
return str
end)
end
if args.preprocess then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
if yesno(args.preprocess or 'no') then
return working_frame:preprocess( body )
end
end
return body
end
function p.combineNumber(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {
parentFirst=true,
trim = false,
removeBlanks = false
})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local input_article = args[1] or args['1'] or ''
local spliter = {}
input_spliter = mw.ustring.gsub(input_article, '(%d)', '%1%1%1')
mw.ustring.gsub(input_spliter,'[%d%.]([^%d%.]+)[%d%.]',function(str)
spliter[str] =(spliter[str] or 0)+1
return str
end)
local max_count = 0
local spliter_str = args[2] or args['2'] or ','
for value,v_count in pairs(spliter) do
if v_count > max_count then
max_count = v_count
spliter_str = value
end
end
if (args[2] or args['2']) ~= nil and (args[2] or args['2']) ~= '' then spliter_str = args[2] or args['2'] end
if mw.text.trim(spliter_str or '') =='' then spliter_str = ' ' end
local counting_sort = {}
mw.ustring.gsub(input_article,'[%d%.]+',function(str)
local n_first, n_last = mw.ustring.find(str,'%d+')
if n_first then
local num = tonumber(mw.ustring.sub(str, n_first, n_last))
if num then counting_sort[num] = counting_sort[num] or str end
end
return str
end)
local quick_sort = {}
for value,v_count in pairs(counting_sort) do
quick_sort[#quick_sort + 1] = {value,v_count}
end
table.sort( quick_sort, function(left,fight) return left[1] < fight[1] end )
local write_number = ''
local output_arr = {}
local trmp_str = ''
for i=1,#quick_sort do
if type(write_number) ~= type(0) then
write_number = quick_sort[i][1]
trmp_str = '' .. quick_sort[i][2]
end
if quick_sort[i][1] + 1 ~= (quick_sort[i+1]or{quick_sort[i][1]-1})[1] then
if write_number ~= quick_sort[i][1] then
trmp_str = trmp_str .. "-"..quick_sort[i][2]
end
output_arr[#output_arr + 1] = trmp_str
write_number = ''
end
end
return table.concat( output_arr, spliter_str )
end
return p