Ngôn ngữ lập trình: VB.NET
Tác giả: Sưu tầm
Chức năng: Cái tên nói lên tất cả

Mã: Chọn tất cả
Private Function ConvertToRoman(ByVal pstrDecimalNumber As String) As String '-------------------------------------------------------------------------------------- Const strPOS_VAL As String = "IXCM" Const strFIVE_VAL As String = "VLD" Dim strRoman As String Dim strCurrRomanPos As String Dim strLetter1 As String Dim strLetter2 As String Dim intCurrPos As Integer Dim intDigit As Integer Dim intDigitPos As Integer intCurrPos = 1 strRoman = "" For intDigitPos = Len(pstrDecimalNumber) To 1 Step -1 intDigit = Val(Mid$(pstrDecimalNumber, intDigitPos, 1)) strCurrRomanPos = Mid$(strPOS_VAL, intCurrPos, 1) Select Case intDigit Case 9 strLetter1 = strCurrRomanPos strLetter2 = Mid$(strPOS_VAL, intCurrPos + 1, 1) Case Is > 4 strLetter1 = Mid$(strFIVE_VAL, intCurrPos, 1) strLetter2 = New String(strCurrRomanPos, intDigit - 5) Case 4 strLetter1 = strCurrRomanPos strLetter2 = Mid$(strFIVE_VAL, intCurrPos, 1) Case Else strLetter1 = New String(strCurrRomanPos, intDigit) strLetter2 = "" End Select strRoman = strLetter1 & strLetter2 & strRoman intCurrPos = intCurrPos + 1 Next ConvertToRoman = strRoman End Function
Mã: Chọn tất cả
Private Function ConvertToDecimal(ByVal pstrRomanNumeral As String) As String '-------------------------------------------------------------------------------------- Dim aintRomanValues() As Integer Dim intInputLen As Integer Dim intX As Integer Dim intSum As Integer intInputLen = Len(pstrRomanNumeral) If intInputLen = 0 Then ConvertToDecimal = 0 Exit Function End If ReDim aintRomanValues(intInputLen) For intX = 1 To intInputLen Select Case Mid$(pstrRomanNumeral, intX, 1) Case "M" : aintRomanValues(intX) = 1000 Case "D" : aintRomanValues(intX) = 500 Case "C" : aintRomanValues(intX) = 100 Case "L" : aintRomanValues(intX) = 50 Case "X" : aintRomanValues(intX) = 10 Case "V" : aintRomanValues(intX) = 5 Case "I" : aintRomanValues(intX) = 1 End Select Next For intX = 1 To intInputLen If intX = intInputLen Then intSum = intSum + aintRomanValues(intX) Else If aintRomanValues(intX) >= aintRomanValues(intX + 1) Then intSum = intSum + aintRomanValues(intX) Else intSum = intSum - aintRomanValues(intX) End If End If Next ConvertToDecimal = CStr(intSum) End Function