Wc3C.net I2Roman
 Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar

07-11-2008, 02:56 AM   #1
Vexorian
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

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=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
```
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

07-11-2008, 03:34 AM   #2
Gorman
.▲▼▲&#9

Join Date: Apr 2007
Posts: 1,087

Submissions (2)

ha, thats pretty cool.

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

Smooth work man!
__________________
 . . My sig is fully interactive.

 07-11-2008, 07:53 AM #3 Strilanc User     Join Date: Jun 2007 Posts: 917 Submissions (4) 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 ``` __________________ Don't pay attention to this signature, it's self-contradictory. Last edited by Strilanc : 07-11-2008 at 08:05 AM.
 07-12-2008, 06:40 PM #4 Rising_Dusk Obscurity, the Art Projects DirectorProject Leader: OD   Join Date: Feb 2006 Posts: 9,729 Submissions (27) Whatever, this does what it says it does. Approved. __________________
07-12-2008, 08:10 PM   #5
Vexorian
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

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.
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off

All times are GMT. The time now is 12:01 AM.

 -- Night Elf -- Tauren -- Troll -- Pandaren -- Underworld -- Blood Elf -- Northrend -- Lightning ---- Light-ning -- Arcane -- Diablo III -- Protoss -- Mobile Version Wc3campaigns - Archive - Top

 Affiliates