Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Roman numeral encoder in Windows batch

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> General Discussions
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Feb 4th, 2014, 5:01am    Post subject: Roman numeral encoder in Windows batch Reply with quote

Every now and then I like to challenge myself by writing a program in Windows batch just to see if it can be done. This is a task from Rosetta Code to convert a number to Roman numeral.
Code:
@Echo Off
SetLocal EnableDelayedExpansion

For %%n In (1666 1888 1999 2000) Do (
  Call :toRoman %%n
  Echo %%n = !roman!
)

GoTo :EOF

:toRoman value
:: Converts an integer value to a Roman numeral. Result is in %roman%.
  Set value=%1
  Set "Roman="
  If Not Defined arabic.0 (
    Call :SetArray arabic 0=1000 1=900 2=500 3=400 4=100 5=90 6=50 7=40 8=10 9=9 10=5 11=4 12=1
    Call :SetArray roman 0=M 1=CM 2=D 3=CD 4=C 5=XC 6=L 7=XL 8=X 9=IX 10=V 11=IV 12=I
  )
  For /L %%i In (0,1,12) Do Call :toRoman_Loop %%i
  GoTo :EOF

:toRoman_Loop 1
:: Called by :toRoman to do much of the work. No other code should call this.
  If !value! LSS !arabic.%1! GoTo :EOF
  Set "Roman=!Roman!!roman.%1!"
  Set /A value-=!arabic.%1!
  GoTo toRoman_Loop

:SetArray name index1=value1 index2=value2 etc
:: Creates a pseudo-array of variables of the form "name.index"
  If "%2" EQU "" GoTo :EOF
  Set "%1.%2=%3"
  Shift /2
  Shift /2
  GoTo SetArray

_________________
Brent


Last edited by Brent on Feb 5th, 2014, 5:28pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Feb 4th, 2014, 7:21pm    Post subject: Re: Roman numeral encoder in Windows batch Reply with quote

This is a nice showcase of working around the shortcomings of Windows command scripts Wink

I really like this approach Wink

_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Feb 5th, 2014, 5:36pm    Post subject: Re: Roman numeral encoder in Windows batch Reply with quote

Thanks, Stefan.

BTW, I edited the code to eliminate any chance of accidental spaces that might be introduced by the forum software.

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> General Discussions All times are GMT
Page 1 of 1
Jump to:  
Quick Reply
Username:
Message:
   Shortcut keys: Alt+Q to activate, Alt+P to preview, Alt+S to submit
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group