apr2505
asked on
Visual Basic creating code for Roman Numerals
I do not know anything about Roman Numerals and I have a problem that request code for it. I'm sure what this is asking me to do.
Consider Roman numerals: different letters have numeric values, which are added or subtracted depending on their position. Remember that I, V, X, L, C, D, and M represent, respective, 1, 5, 10, 50, 100, 500, and 1000. (One mnemonic device for remembering this is the sentence "If Victor's Xray Looks Clear, Don't Medicate.") Thus, in Roman numerals, "LIX" would be 59, because you add 50 to (10 - 1).
We're going to do a simplified version of this: in our version, instead of position indicating whether you add or subtract, we're going to use case. In our system, UPPER-CASE LETTERS are positive, and lower-case letters are negative. Thus, in our system, to write 59, you could use "LiX", or "LXi", or any other combination of L, X, and i.
Any letter or symbol not in the set {I, V, X, L, C, D, M, i, v, x, l, c, d, m} is simply ignored. Thus, "Mommy" gives the value -1000, because there is one positive thousand (M), and two negative thousands (mm). The o, and the y, have no value and are ignored. "Midway" gives 499, because there is one positive thousand (M), one negative 500 (d), and one negative 1 (i). The other letters are ignored. `villi' gives -107. `MCMLXXI' is 2171, because we add all the values. (In actual Roman numerals, that would give 1971, because the `CM' means to subtract 100 from the 1000.) You may or may not find it useful to be reminded that Mid(foo, n, 1) returns the nth character of the string foo.
Consider Roman numerals: different letters have numeric values, which are added or subtracted depending on their position. Remember that I, V, X, L, C, D, and M represent, respective, 1, 5, 10, 50, 100, 500, and 1000. (One mnemonic device for remembering this is the sentence "If Victor's Xray Looks Clear, Don't Medicate.") Thus, in Roman numerals, "LIX" would be 59, because you add 50 to (10 - 1).
We're going to do a simplified version of this: in our version, instead of position indicating whether you add or subtract, we're going to use case. In our system, UPPER-CASE LETTERS are positive, and lower-case letters are negative. Thus, in our system, to write 59, you could use "LiX", or "LXi", or any other combination of L, X, and i.
Any letter or symbol not in the set {I, V, X, L, C, D, M, i, v, x, l, c, d, m} is simply ignored. Thus, "Mommy" gives the value -1000, because there is one positive thousand (M), and two negative thousands (mm). The o, and the y, have no value and are ignored. "Midway" gives 499, because there is one positive thousand (M), one negative 500 (d), and one negative 1 (i). The other letters are ignored. `villi' gives -107. `MCMLXXI' is 2171, because we add all the values. (In actual Roman numerals, that would give 1971, because the `CM' means to subtract 100 from the 1000.) You may or may not find it useful to be reminded that Mid(foo, n, 1) returns the nth character of the string foo.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, I was in a hurry. so i just coded for decimal values to roman conversion.
ASKER
Ok when the program is ran an error pops up highlighting NumberToRoman=tmp stating Compile Error; Argument not Optional. Should put NumberToRoman=tmp+NumberTo Roman? Now I've split the code in half because there is a cmdCompute and cmdConvert. I put the Dim etc put under cmdCompute and put the IF statements under cmdConvert. was that the right way? Here is what it looks like
Dim nThousands As Long
Dim nFiveHundreds As Long
Dim nHundreds As Long
Dim nFifties As Long
Dim nTens As Long
Dim nFives As Long
Dim nOnes As Long
Dim tmp As String
nOnes = decimalvalue
nThousands = nOnes \ 1000
nOnes = nOnes - nThousands * 1000
nFiveHundreds = nOnes \ 500
nOnes = nOnes - nFiveHundreds * 500
nHundreds = nOnes \ 100
nOnes = nOnes - nHundreds * 100
nFifties = nOnes \ 50
nOnes = nOnes - nFifties * 50
nTens = nOnes \ 10
nOnes = nOnes - nTens * 10
nFives = nOnes \ 5
nOnes = nOnes - nFives * 5
tmp = String(nThousands, "M")
End Sub
Private Sub cmdConvert_Click()
If nHundreds = 4 Then
If nFiveHundreds = 1 Then
tmp = tmp & "CM"
Else
tmp = tmp & "CD"
End If
Else
'not a 4, so create the string
tmp = tmp & String(nFiveHundreds, "D") & String(nHundreds, "C")
End If
If nTens = 4 Then
If nFifties = 1 Then
tmp = tmp & "XC"
Else
tmp = tmp & "XL"
End If
Else
tmp = tmp & String(nFifties, "L") & String(nTens, "X")
End If
If nOnes = 4 Then
If nFives = 1 Then
tmp = tmp & "IX"
Else
tmp = tmp & "IV"
End If
Else
tmp = tmp & String(nFives, "V") & String(nOnes, "I")
End If
NumberToRoman = tmp
Debug.Print NumberToRoman
End Function
Private Sub Command1_Click()
NumberToRoman (Text1.Text)
End Sub
End Sub
Dim nThousands As Long
Dim nFiveHundreds As Long
Dim nHundreds As Long
Dim nFifties As Long
Dim nTens As Long
Dim nFives As Long
Dim nOnes As Long
Dim tmp As String
nOnes = decimalvalue
nThousands = nOnes \ 1000
nOnes = nOnes - nThousands * 1000
nFiveHundreds = nOnes \ 500
nOnes = nOnes - nFiveHundreds * 500
nHundreds = nOnes \ 100
nOnes = nOnes - nHundreds * 100
nFifties = nOnes \ 50
nOnes = nOnes - nFifties * 50
nTens = nOnes \ 10
nOnes = nOnes - nTens * 10
nFives = nOnes \ 5
nOnes = nOnes - nFives * 5
tmp = String(nThousands, "M")
End Sub
Private Sub cmdConvert_Click()
If nHundreds = 4 Then
If nFiveHundreds = 1 Then
tmp = tmp & "CM"
Else
tmp = tmp & "CD"
End If
Else
'not a 4, so create the string
tmp = tmp & String(nFiveHundreds, "D") & String(nHundreds, "C")
End If
If nTens = 4 Then
If nFifties = 1 Then
tmp = tmp & "XC"
Else
tmp = tmp & "XL"
End If
Else
tmp = tmp & String(nFifties, "L") & String(nTens, "X")
End If
If nOnes = 4 Then
If nFives = 1 Then
tmp = tmp & "IX"
Else
tmp = tmp & "IV"
End If
Else
tmp = tmp & String(nFives, "V") & String(nOnes, "I")
End If
NumberToRoman = tmp
Debug.Print NumberToRoman
End Function
Private Sub Command1_Click()
NumberToRoman (Text1.Text)
End Sub
End Sub
https://www.experts-exchange.com/help.jsp#hi105
If this isn't homework, you really need to come here with a more specific question, rather than a description of the project. Then the experts will HELP you with your own work.