 Tuesday, November 01, 2005
I am reviewing the chapter on execution flow in Visual Basic 2005, where I cover recursion - among the many things. In most programming books I've read, recusion is explained with the "classic" factorial example (which can be implemented more efficiently with a simple For loop) or as a means to visit tree structures. It looks like recursion isn't useful in the "average" business application, which of course isn't the case. As most programming techniques, it's mostly a matter of knowing when and where to exploit it.
Here's an example of recursion that you might find quite useful: a method that converts an integer into its textual representation, e.g. 1234 into "One Thousand Two Hundreds Thirty Four", taken from my forthcoming Microsoft Press book Programming Microsoft Visual Basic 2005.
Public Shared Function NumberToText(ByVal n As Integer) As String Select Case n Case Is < 0 Return "Minus " & NumberToText(-n) Case 0 Return "" Case 1 To 19 Dim arr() As String = {"One", "Two", "Three", "Four", "Five", "Six", _ "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", _ "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"} Return arr(n - 1) & " " Case 20 To 99 Dim arr() As String = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", _ "Seventy", "Eighty", "Ninety"} Return arr(n \ 10 - 2) & " " & NumberToText(n Mod 10) Case 100 To 199 Return "One Hundred " & NumberToText(n Mod 100) Case 200 To 999 Return NumberToText(n \ 100) & "Hundreds " & NumberToText(n Mod 100) Case 1000 To 1999 Return "One Thousand " & NumberToText(n Mod 1000) Case 2000 To 999999 Return NumberToText(n \ 1000) & "Thousands " & NumberToText(n Mod 1000) Case 1000000 To 1999999 Return "One Million " & NumberToText(n Mod 1000000) Case 1000000 To 999999999 Return NumberToText(n \ 1000000) & "Millions " & NumberToText(n Mod 1000000) Case 1000000000 To 1999999999 Return "One Billion " & NumberToText(n Mod 1000000000) Case Else Return NumberToText(n \ 1000000000) & "Billions " _ & NumberToText(n Mod 1000000000) End Select End Function
Here's the version for curly braces' lovers. C# switch keyword doesn't support ranges, thus I had to change the code to use a series of elseif blocks:
public static string NumberToText( int n) { if ( n < 0 ) return "Minus " + NumberToText(-n); else if ( n == 0 ) return ""; else if ( n <= 19 ) return new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}[n-1] + " "; else if ( n <= 99 ) return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}[n / 10 - 2] + " " + NumberToText(n % 10); else if ( n <= 199 ) return "One Hundred " + NumberToText(n % 100); else if ( n <= 999 ) return NumberToText(n / 100) + "Hundreds " + NumberToText(n % 100); else if ( n <= 1999 ) return "One Thousand " + NumberToText(n % 1000); else if ( n <= 999999 ) return NumberToText(n / 1000) + "Thousands " + NumberToText(n % 1000); else if ( n <= 1999999 ) return "One Million " + NumberToText(n % 1000000); else if ( n <= 999999999) return NumberToText(n / 1000000) + "Millions " + NumberToText(n % 1000000); else if ( n <= 1999999999 ) return "One Billion " + NumberToText(n % 1000000000); else return NumberToText(n / 1000000000) + "Billions " + NumberToText(n % 1000000000); }
These methods are much simpler than any similar code I've found on the Internet, thanks to recursion. I really love OOP, generics, attributes, regular expressions, and other advanced language features, but I also like to reming that you can often write elegant, compact, and efficient code just leveraging the features that mainstream languages have offered for decades.
|
|
Get RSS/Atom Feed
Search in the blog
Archive
| | Sun | Mon | Tue | Wed | Thu | Fri | Sat | | 29 | 30 | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 10 | 11 | 12 | | 13 | 14 | 15 | 16 | 17 | 18 | 19 | | 20 | 21 | 22 | 23 | 24 | 25 | 26 | | 27 | 28 | 29 | 30 | 31 | 1 | 2 | | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Categories
Powered by: newtelligence dasBlog 1.8.5223.1
|