Solved

Visual Basic creating code for Roman Numerals

Posted on 2007-11-16
6
1,490 Views
Last Modified: 2013-11-26
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.

0
Comment
Question by:apr2505
  • 2
6 Comments
 
LVL 13

Expert Comment

by:jmundsack
ID: 20301595
This sounds a lot like a homework assignment.  EE is a resource to help you resolve specific problems with the efforts you're already making--not to write code for you.

http://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.
0
 
LVL 17

Accepted Solution

by:
Shanmuga Sundaram earned 500 total points
ID: 20303636
Private Function NumberToRoman(decimalvalue As Long) As String

   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

  'take the value passed and split it out
  'to values representing the number of
  'ones, tens, hundreds, etc
   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
   
  'using VB's String function, create
  'a series of strings representing
  'the number of each respective denomination
   tmp = String(nThousands, "M")

  'handle those cases where the denominator
  'value is on either side of a roman numeral
   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
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20303638
Sorry, I was in a hurry. so i just coded for decimal values to roman conversion.
0
 

Author Comment

by:apr2505
ID: 20306378
Ok when the program is ran an error pops up highlighting NumberToRoman=tmp stating Compile Error; Argument not Optional.  Should put NumberToRoman=tmp+NumberToRoman? 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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question