„Modul:CASRN“ – Versionsunterschied
Erscheinungsbild
[gesichtete Version] | [gesichtete Version] |
Inhalt gelöscht Inhalt hinzugefügt
Rjh (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Rjh (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
Zeile 13: | Zeile 13: | ||
local hyphen2 = string.byte(CAS,len-4,len-4) |
local hyphen2 = string.byte(CAS,len-4,len-4) |
||
-- io.write('proof=', proof, ' ') |
-- io.write('proof=', proof, ' ') |
||
if (proof > 0 and proof < 9 and hyphen1 == 45 and hyphen2 == 45) then |
if (proof >= 0 and proof <= 9 and hyphen1 == 45 and hyphen2 == 45) then |
||
-- io.write('ok ') |
-- io.write('ok ') |
||
for i = 2,len-1 do |
for i = 2,len-1 do |
Version vom 9. September 2021, 19:26 Uhr
Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | |||
Modul | Deutsch | English
|
Modul: | Dokumentation |
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
-- Export
local p = {}
function checkCASInternal(CAS)
local result = false
if (CAS ~= nil) then
local len = string.len(CAS)
local numbercount = 1
local sum = 0
if (len > 5) then
local proof = string.byte(CAS,len,len)-48
local hyphen1 = string.byte(CAS,len-1,len-1)
local hyphen2 = string.byte(CAS,len-4,len-4)
-- io.write('proof=', proof, ' ')
if (proof >= 0 and proof <= 9 and hyphen1 == 45 and hyphen2 == 45) then
-- io.write('ok ')
for i = 2,len-1 do
local pos = len - i
local b = string.byte(CAS,pos,pos+1)-48
if (b >= 0 and b <= 9) then
sum = sum + b * numbercount
-- io.write(b,'*',numbercount,'=',b*numbercount, ' ' )
numbercount = numbercount + 1
else
if (i ~= 4) then
-- io.write('break at ',i, ' ')
break
end
end
end
-- io.write(sum, ' ', sum % 10)
result = proof == sum % 10
end
end
end
return result
end
-- if (checkCAS(CAS)) then
-- io.write("\nok")
-- else
-- io.write("\nnok")
-- end
function p.checkCAS( frame )
local ret = checkCASInternal( frame.args[1] )
return ret
end
return p