コンテンツにスキップ

モジュール:乗算

モジュールの解説[作成]
multiplication = {};

local getArgs = require('Module:Arguments').getArgs

local function toN(num,base)
  ret = ""
  if num == 0 then
    return 0
  end
  while num >= 1 do
    ret = tostring(num % base) .. ret
    num = (num - num % base) / base
  end
  return ret
end

multiplication.table = function(frame, args)
	if not args then
		args = getArgs(frame)
	end
	local start_num = args[1]
	local end_num = args[2]
	local base = args[3]

	local output = ""

	local regex = "^%s*%d+%s*$"
	if (not string.match(start_num, regex)) or (not string.match(end_num, regex)) or (not string.match(base, regex)) then
		error("第1引数、第2引数、第3引数は整数を指定してください。")
		return output
	elseif tonumber(start_num) > tonumber(end_num) then
		error("第1引数が第2引数よりも大きい数値です。")
		return output
	elseif tonumber(base) < 2 or tonumber(base) > 36 then
		-- 「Template:乗算表」版番47632924の実装にあわせる
		error("第2引数の数値が大きすぎます。")
		return output
	end

	local body_style = ''
	local header_style_attribute = ''
	if args.bodystyle then
		body_style = args.bodystyle
	end
	if args.headerstyle then
		header_style_attribute = string.format(" style='%s' |", args.headerstyle)
	end
	output = output .. string.format("{| class='wikitable' border='1' style='text-align:right;%s'\n|-\n!%s \n", body_style, header_style_attribute)
	for i = start_num,end_num do
		output = output .. string.format("!%s %s\n", header_style_attribute, toN(i,base))
	end

	for i = start_num,end_num do
		output = output .. string.format("\n|-\n! %s \n", toN(i,base))
		for j = start_num,end_num do
			output = output .. string.format("|| %s ", toN(i*j,base))
		end
	end

	output = output .. string.format("\n|}")

	return output
end

return multiplication