Idi na sadržaj

Modul:Rimski brojevi

S Wikipedije, slobodne enciklopedije

Dokumentaciju za ovaj modul možete napraviti na straniciModul:Rimski brojevi/dok

-- This module implements {{Roman}}.

localp={}

-- This function implements the {{overline}} template.
localfunctionoverline(s)
returnmw.ustring.format('<span style= "text-decoration:overline;" >%s</span>',s)
end

-- Gets the Roman numerals for a given numeral table. Returns both the string of
-- numerals and the value of the number after it is finished being processed.
localfunctiongetLetters(num,t)
localret={}
for_,vinipairs(t)do
localval,letter=unpack(v)
whilenum>=valdo
num=num-val
table.insert(ret,letter)
end
end

returntable.concat(ret),num
end

-- The main control flow of the module.
localfunction_main(args)
-- Get input and exit displaying nothing if the input is empty.
ifargs[1]==nilthenreturnend
localnum=tonumber(args[1])
ifnotnumornum<0ornum==math.hugethen
error('Invalid number '..args[1],2)
elseifnum==0then
return'N'
end

-- Return a message for numbers too big to be expressed in Roman numerals.
ifnum>=5000000then
returnargs[2]or'N/A'
end

localret=''
-- Find the Roman numerals for the large part of numbers.
-- 23 April 2016 - tweaked to >= 4000 to accept big Roman 'IV'
-- The if statement is not strictly necessary, but makes the algorithm
-- more efficient for smaller numbers.
ifnum>=4000then
localbigRomans={
{1000000,'M'},
{900000,'CM'},{500000,'D'},{400000,'CD'},{100000,'C'},
{90000,'XC'},{50000,'L'},{40000,'XL'},{10000,'X'},
{9000,'IX'},{5000,'V'},{4000,'IV'},
}
localbigLetters
bigLetters,num=getLetters(num,bigRomans)
ret=overline(bigLetters)
end

-- Find the Roman numerals for numbers less than the big Roman threshold.
localsmallRomans={
{1000,'M'},
{900,'CM'},{500,'D'},{400,'CD'},{100,'C'},
{90,'XC'},{50,'L'},{40,'XL'},{10,'X'},
{9,'IX'},{5,'V'},{4,'IV'},{1,'I'}
}
localsmallLetters=getLetters(num,smallRomans)
ret=ret..smallLetters

ifargs.fraction=='yes'then
-- Find the Roman numerals for the fractional parts of numbers.
-- If num is not a whole number, add half of 1/1728 (the smallest unit) to equate to rounding.
-- Ensure we're not less than the smallest unit or larger than 1 - smallest unit
-- to avoid getting two "half" symbols or no symbols at all
num=num-math.floor(num)
ifnum~=0then
num=math.max(1.1/1728,math.min(1727.1/1728,num+1/3456))
end
localfractionalRomans={
{1/2,'S'},{5/12,"''':'''•''':'''"},{1/3,"'''::'''"},
{1/4,"''':'''•"},{1/6,"''':'''"},{1/12,'•'},
{1/24,'Є'},{1/36,'ƧƧ'},{1/48,'Ɔ'},{1/72,'Ƨ'},{1/144,'ƻ'},
{1/288,'℈'},{1/1728,'»'},
}
localfractionalLetters=getLetters(num,fractionalRomans)

ret=ret..fractionalLetters
end

returnret
end

functionp.main(frame)
-- If called via #invoke, use the args passed into the invoking
-- template, or the args passed to #invoke if any exist. Otherwise
-- assume args are being passed directly in from the debug console
-- or from another Lua module.
localorigArgs
ifframe==mw.getCurrentFrame()then
origArgs=frame:getParent().args
fork,vinpairs(frame.args)do
origArgs=frame.args
break
end
else
origArgs=frame
end
-- Trim whitespace and remove blank arguments.
localargs={}
fork,vinpairs(origArgs)do
iftype(v)=='string'then
v=mw.text.trim(v)
end
ifv~=''then
args[k]=v
end
end

-- exit if not given anything
ifargs==nilorargs=={}thenreturnend
-- Given mathematical expression, simplify to a number
iftype(args[1])=='string'then
args[1]=mw.ext.ParserFunctions.expr(args[1])
end
return_main(args)
end

returnp