The power of recursion: Number-to-Text conversion

Origin:http://www.dotnet2themax.com/blogs/fbalena/PermaLink,guid,cdceca73-08cd-4c15-aef7-0f9c8096e20a.aspx

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值