07112008, 02:56 AM  #1 
Free Software Terrorist
Technical Director

I2Roman
Perhaps for some reason you want to use roman numbers for some stuff? I don't know. The code is ugly if anyone can improve it, go ahead.
__________________ JASS: function I2Roman takes integer n returns string local string r="" if n>3999 or n < 1 then return I2S(n) endif loop exitwhen n<1000 set r=r+"M" set n=n1000 endloop loop exitwhen n < 900 set r=r+"CM" set n=n900 endloop loop exitwhen n<500 set r=r+"D" set n=n500 endloop loop exitwhen n < 400 set r=r+"CD" set n=n400 endloop loop exitwhen n<100 set r=r+"C" set n=n100 endloop loop exitwhen n < 90 set r=r+"XC" set n=n90 endloop loop exitwhen n<50 set r=r+"L" set n=n50 endloop loop exitwhen n < 40 set r=r+"XL" set n=n40 endloop loop exitwhen n<10 set r=r+"X" set n=n10 endloop loop exitwhen n < 9 set r=r+"IX" set n=n9 endloop loop exitwhen n<5 set r=r+"V" set n=n5 endloop loop exitwhen n < 4 set r=r+"IV" set n=n4 endloop loop exitwhen n<1 set r=r+"I" set n=n1 endloop return r endfunction 
Sponsored Links  Login to hide this ad! 

07112008, 03:34 AM  #2 
.▲▼▲	

ha, thats pretty cool.
__________________I might make one of them just coz its so cool. Smooth work man! 
07112008, 07:53 AM  #3 
User

This isn't quite perfect, but it's close. I have no idea why roman numerals are still used; ever. It's a horrible representation for numbers. It would be half decent if they didn't have the crazy 'reverse for subtract' rule.
__________________ JASS: globals string chars = "IVXLCDM" integer array periods = {5,2,5,2,5,2,5} //obviously initialize elsewhere constant integer numChars = 7 integer array nums //workspace endglobals function I2Roman takes integer n returns string local string prefix = "" local string suffix = "" local string char local integer i = 0 loop exitwhen i >= numChars //measure character count set char = SubString(chars,i,i+1) set nums[i] = nums[i] + ModuloInteger(n, periods[i]) set n = n / periods[i] //output character if nums[i] == periods[i]1 and nums[i] > 1 then //needs to be subtracted, so go in prefix set nums[i+1] = 1 set prefix = prefix + char else //needs to be added, so go in suffix if nums[i] == periods[i] then //carry from a subtraction, pass the carry set nums[i] = 0 set nums[i+1] = 1 endif //add to suffix loop exitwhen nums[i] <= 0 set suffix = char + suffix set nums[i] = nums[i]  1 endloop endif set i = i + 1 endloop return prefix + suffix endfunction Last edited by Strilanc : 07112008 at 08:05 AM. 
07122008, 08:10 PM  #5 
Free Software Terrorist
Technical Director

Strilanc. I am quite sure your version is much slower than mine. Try benchmarks. Sure it is shorter , but ...
__________________I guess it is possible to save a lot using a textmacro. I didn't want to add a vJass lockin to this. 
Thread Tools  Search this Thread 

