AppleScript名:ローマ数字エンコーダー、デコーダー v2 |
— Created 2015-11-16 by Takaaki Naganoya — Modified 2017-05-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://stackoverflow.com/questions/14762282/how-to-convert-a-roman-numeral-to-an-nsnumer repeat with i from 1 to 3999 set aRes to numberToRomanNumerals(i) of me set bRes to romanNumeralsToNumber(aRes) of me log {i, aRes, bRes} end repeat –Roman Number String Encoder on numberToRomanNumerals(aNum) if (aNum < 0 or aNum > 3999) then return "" –条件が間違っていたので直した(2017/5/18) set r_ones to {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"} set r_tens to {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"} set r_hunds to {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"} set r_thouds to {"M", "MM", "MMM", "MMMM", "MMMMM", "MMMMMM", "MMMMMMM", "MMMMMMMM", "MMMMMMMMM"} set ones to aNum mod 10 set tens to (aNum – ones) mod 100 set hundreds to (aNum – tens – ones) mod 1000 set thou to (aNum – hundreds – tens – ones) mod 10000 set tens to tens / 10 set hundreds to hundreds / 100 set thou to thou / 1000 set rNum to current application’s NSString’s stringWithString:"" if thou > 0 then set rNum to rNum’s stringByAppendingString:(item thou of r_thouds) if hundreds > 0 then set rNum to rNum’s stringByAppendingString:(item hundreds of r_hunds) if tens > 0 then set rNum to rNum’s stringByAppendingString:(item tens of r_tens) if ones > 0 then set rNum to rNum’s stringByAppendingString:(item ones of r_ones) return rNum as text end numberToRomanNumerals –Roman Number String Decoder on romanNumeralsToNumber(str) — Piero Garzotto (http://scriptbuilders.net/files/romannumerals1.0.html) local str, r, i set r to 0 repeat with i from 1 to count str set r to r + (item (offset of (item i of str) in "mdclxvi") of ¬ {1000, 500, 100, 50, 10, 5, 1}) * (((((offset of ¬ (item (i + 1) of (str & "@")) in "mdclxvi@") ≥ (offset of ¬ (item i of str) in "mdclxvi")) as integer) * 2) – 1) end repeat return r end romanNumeralsToNumber |
More from my site
(Visited 38 times, 1 visits today)