HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

I2Roman

07-11-2008, 01:56 AM#1
Vexorian
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.

Collapse 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=n-1000
    endloop
    loop
        exitwhen n < 900
        set r=r+"CM"
        set n=n-900
    endloop
    loop
        exitwhen n<500
        set r=r+"D"
        set n=n-500
    endloop
    loop
        exitwhen n < 400
        set r=r+"CD"
        set n=n-400
    endloop
    loop
        exitwhen n<100
        set r=r+"C"
        set n=n-100
    endloop
    loop
        exitwhen n < 90
        set r=r+"XC"
        set n=n-90
    endloop
    loop
        exitwhen n<50
        set r=r+"L"
        set n=n-50
    endloop
    loop
        exitwhen n < 40
        set r=r+"XL"
        set n=n-40
    endloop
    loop
        exitwhen n<10
        set r=r+"X"
        set n=n-10
    endloop
    loop
        exitwhen n < 9
        set r=r+"IX"
        set n=n-9
    endloop
    loop
        exitwhen n<5
        set r=r+"V"
        set n=n-5
    endloop
    loop
        exitwhen n < 4
        set r=r+"IV"
        set n=n-4
    endloop
    loop
        exitwhen n<1
        set r=r+"I"
        set n=n-1
    endloop
 return r
endfunction
07-11-2008, 02:34 AM#2
Gorman
ha, thats pretty cool.

I might make one of them just coz its so cool.

Smooth work man!
07-11-2008, 06:53 AM#3
Strilanc
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.

Collapse 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
07-12-2008, 05:40 PM#4
Rising_Dusk
Whatever, this does what it says it does. Approved.
07-12-2008, 07:10 PM#5
Vexorian
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 lock-in to this.