Solved

Visual Basic creating code for Roman Numerals

Posted on 2007-11-16
6
1,478 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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, I was in a hurry. so i just coded for decimal values to roman conversion.
0
 

Author Comment

by:apr2505
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now