|
-
Mar 8th, 2006, 05:42 AM
#1
Thread Starter
New Member
Roman Numeral Calculator
Hey I'm doing coursework in VB 6 and the task is to:
- allow a user to enter numbers in Roman numerals, according to the additive rule, each up to a maximum value of 4000 (MMMM)
- prevent the user entering an invalid Roman numeral
- display the numbers entered in both Roman numerals and their Arabic decimal equivalent
- allow the user to enter an operator for addition or subtraction
- prevent the result of a subtraction being zero or a negative number
- display the result of the calculation in both Roman and Arabic decimal forms
- allow the user to clear all displays
The additive rule in this case is to make sure that you enter numerals in descending order of value and a letter may not be repeated if it can be replaced (e.g. IIIII can be replaced with V). In this question 4 is classed as being IIII and NOT IV.
I've created an inteface that allows the user to enter an already valid Roman numeral (i.e. not validated by the program itself), it displays the numbers and answers in both numeral and decimal forms, the user can add or subtract the numerals and there is an option to clear all displays.
But I'm having problems trying to write the validation rules for:
- making sure the numeral that is entered is in the correct order
- the additive rule (explained above)
If anyone can help it would be much appreciated! Thanks!
xtishx
-
Mar 8th, 2006, 05:55 AM
#2
Frenzied Member
Re: Roman Numeral Calculator
well, i didnt go thru all your specifications but what u need to do is do a code check in the change event. what i mean basically is to check what has been entered into a textbox everytime something is typed in.
-
Mar 8th, 2006, 07:47 AM
#3
Re: Roman Numeral Calculator
Sorry it took so long to reply, but tada . Btw Welcome to the forums 
For this you'll need one textbox and one label
VB Code:
Const RomanNumerals As String = "IVXLCDM"
Dim MaskedString As String
Dim NumUsed(0 To 7) As Long
Private Sub Form_Load()
MaskedString = String(7 * 4, "#")
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
pos = InStr(1, RomanNumerals, UCase(Chr(KeyAscii)))
If pos And NumUsed(pos) < 4 Then
strpart = Mid(MaskedString, (pos * 4) - 3, 4)
strpart = Replace(strpart, "#", UCase(Chr(KeyAscii)), , 1)
If UCase(Chr(KeyAscii)) = "I" Then
MaskedString = strpart & Mid(MaskedString, (pos * 4) + 1)
Else
MaskedString = Mid(MaskedString, 1, (pos * 4) - 4) & strpart & Mid(MaskedString, pos * 4 + 1)
End If
NumUsed(pos) = NumUsed(pos) + 1
KeyAscii = 0
ElseIf KeyAscii = 8 And Text1.SelStart > 0 Then
Letter = Mid(Text1.Text, Text1.SelStart, 1)
pos = InStr(1, RomanNumerals, UCase(Letter))
If NumUsed(pos) > 0 Then NumUsed(pos) = NumUsed(pos) - 1
strpart = Mid(MaskedString, (pos * 4) - 3, 4)
strpart = Replace(strpart, Letter, "#", , 1)
If Letter = "I" Then
MaskedString = strpart & Mid(MaskedString, pos * 4 + 1)
Else
MaskedString = Mid(MaskedString, 1, (pos * 4) - 4) & strpart & Mid(MaskedString, pos * 4 + 1)
End If
Else
KeyAscii = 0
End If
Text1.Text = Replace(MaskedString, "#", "")
Label1.Caption = (NumUsed(1) * 1) + (NumUsed(2) * 5) + (NumUsed(3) * 10) + _
(NumUsed(4) * 50) + (NumUsed(5) * 100) + (NumUsed(6) * 500) + _
(NumUsed(7) * 1000)
End Sub
-
Mar 8th, 2006, 04:14 PM
#4
New Member
Re: Roman Numeral Calculator
hello, im doing the same course as xtishx
we tried your code Andrew G (thanks by the way) and there is just one small problem, the roman numeral is in the opposite order to what is required. I cant seem to the solution to this problem myself. I think only a simple alteration is needed though id be grateful if somebody would tell us what!
Thanks again
the emily
-
Mar 8th, 2006, 11:10 PM
#5
Re: Roman Numeral Calculator
simply reverse the order in
VB Code:
Const RomanNumerals As String = "IVXLCDM"
to
VB Code:
Const RomanNumerals As String = "MDCLXVI"
-
Mar 9th, 2006, 03:41 PM
#6
Thread Starter
New Member
Re: Roman Numeral Calculator
Hey
I tried reversing the string and now it comes up with the wrong decimal equivalents, i.e. I now equals 1000 and M equals 1...so what do we have to change in the rest of it so that it comes up with the right decimal equivalent?
xtishx
Needle naddle noo!
Where's my specitacles??
-
Mar 10th, 2006, 07:46 AM
#7
Re: Roman Numeral Calculator
Oops
VB Code:
Const RomanNumerals As String = "MDCLXVI"
Dim MaskedString As String
Dim NumUsed(0 To 7) As Long
Private Sub Form_Load()
MaskedString = String(7 * 4, "#")
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
pos = InStr(1, RomanNumerals, UCase(Chr(KeyAscii)))
If pos And NumUsed(pos) < 4 Then
strpart = Mid(MaskedString, (pos * 4) - 3, 4)
strpart = Replace(strpart, "#", UCase(Chr(KeyAscii)), , 1)
If UCase(Chr(KeyAscii)) = Left(RomanNumerals, 1) Then
MaskedString = strpart & Mid(MaskedString, (pos * 4) + 1)
Else
MaskedString = Mid(MaskedString, 1, (pos * 4) - 4) & strpart & Mid(MaskedString, pos * 4 + 1)
End If
NumUsed(pos) = NumUsed(pos) + 1
KeyAscii = 0
ElseIf KeyAscii = 8 And Text1.SelStart > 0 Then
Letter = Mid(Text1.Text, Text1.SelStart, 1)
pos = InStr(1, RomanNumerals, UCase(Letter))
If NumUsed(pos) > 0 Then NumUsed(pos) = NumUsed(pos) - 1
strpart = Mid(MaskedString, (pos * 4) - 3, 4)
strpart = Replace(strpart, Letter, "#", , 1)
If Letter = Left(RomanNumerals, 1) Then
MaskedString = strpart & Mid(MaskedString, pos * 4 + 1)
Else
MaskedString = Mid(MaskedString, 1, (pos * 4) - 4) & strpart & Mid(MaskedString, pos * 4 + 1)
End If
Else
KeyAscii = 0
End If
Text1.Text = Replace(MaskedString, "#", "")
Label1.Caption = (NumUsed(1) * 1000) + (NumUsed(2) * 500) + (NumUsed(3) * 100) + _
(NumUsed(4) * 50) + (NumUsed(5) * 10) + (NumUsed(6) * 5) + _
(NumUsed(7) * 1)
End Sub
-
Mar 10th, 2006, 11:04 AM
#8
Re: Roman Numeral Calculator
I have sample code on my site that may assist you.
On this page:
http://www.thevbprogrammer.com/tutorials.html
Under "8. Control Basics", see the Programming Exercise for Roman Numerals.
"It's cold gin time again ..."
Check out my website here.
-
Mar 23rd, 2006, 05:18 AM
#9
New Member
Re: Roman Numeral Calculator
Im also doing this course!
Man its hard!
Im still in design stage, i dont ven knoe how to use Visual Basic, Ill prolly fail this course, got U in ma test!
Anyways can any of you who have done it help me out a little, like code where?
or maybe send me it , lol
Just this is so confusing, and my teacher doesn't even help me, i dont think he likes me!
-
Mar 23rd, 2006, 07:35 AM
#10
Re: Roman Numeral Calculator
Here's a solution... hopefully the correct one, not a MMMM or IIII or XXXX solution. I don't have VB here so I can't test... this is all theoretical.
Translate the character string into an addition formula. Replace the "left char less than right char" combinations first using string manipulation, eg Replace() function. Then replace the rest with their proper equivalents.
CMLXXIV --> 900 + LXX + 4 --> 900 + 50 + 10 + 10 + 4
locate and replace CM with 900...
replace CD with 400...
replace XC with 90...
replace XL = 40...
replace IX = 9...
replace IV = 4...
Validity check for the previously mentioned replacements, the respective character should not appear again. Example for a CM replacement there should be no other C's, IV no other I's, etc.
Insert here a character count check... there should be no more than 3 instances of selected characters such as X. Other characters such as V, there should only be one instance.
continue with replacements of remaining roman characters
replace M = 1000...
replace D = 500...
reaplce C = 100...
and so on and so forth
An additional check that the original roman numeral was valid, the decimal values should be decrementing (in some cases equal, but not in the other direction). If you check the previous equation, you will notice that 900 > 50 > 10 >= 10 > 4. This step might not be necessary, I can't say for certain right now.
Lastly to get the decimal equivalent of the euqation there's a special function (or control??) to equate that. I forgot the name of the function and the project reference necessary. Ask around for that function... the function that gets the math result of a string equation.
There are minor details, depending on how you use the replace function... such as a trailing operand in which case you'll have to add zero to get a valid equation. I'll let you figure out the minor details.
Last edited by leinad31; Mar 23rd, 2006 at 07:57 AM.
-
Mar 23rd, 2006, 07:37 AM
#11
New Member
Re: Roman Numeral Calculator
-
Mar 23rd, 2006, 07:53 AM
#12
Re: Roman Numeral Calculator
Programming is not solely coding... there's also analysis involved.
In real life, don't expect your boss or systems analyst to give you source code. You will have to learn to understand a narrative explanation (often not written) given to you by a systems analyst. Your job then is to implement the program as specified by the requirements and the design/flow indicated by the analyst then do some debugging..
Last edited by leinad31; Mar 23rd, 2006 at 08:00 AM.
-
Mar 23rd, 2006, 07:54 AM
#13
New Member
Re: Roman Numeral Calculator
i still carnt do it! baffled beyond belief
-
Mar 23rd, 2006, 07:57 AM
#14
Re: Roman Numeral Calculator
Read up on the Replace() function, the Microsoft Script Control and the Eval() method of the script control.
Make your own source code implementation and post that here, we can then continue the discussion based on your implementation.
Last edited by leinad31; Mar 23rd, 2006 at 08:51 AM.
-
Mar 23rd, 2006, 08:58 AM
#15
Re: Roman Numeral Calculator
Have a look at my post here.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Mar 23rd, 2006, 09:36 AM
#16
New Member
Re: Roman Numeral Calculator
DKenney!
What does that code do?
-
Mar 23rd, 2006, 09:48 AM
#17
Re: Roman Numeral Calculator
As stated in the commets for the function
This function returns a string that is the Roman Numeral version of a passed Arabic number. The function will only work if the value is less than 4000.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Mar 23rd, 2006, 09:49 AM
#18
New Member
Re: Roman Numeral Calculator
Ok, ya see i dunt understand im not VB NERD!
lol, just need a this calculator doing!
-
Mar 23rd, 2006, 09:54 AM
#19
Re: Roman Numeral Calculator
You may also check out Converting Numbers to Roman Numerals (and Back) sample written by Randy Birch.
-
Mar 23rd, 2006, 10:33 AM
#20
New Member
Re: Roman Numeral Calculator
anyone help me more then giving me code, and weird informtion!?
-
Mar 25th, 2006, 02:30 AM
#21
Re: Roman Numeral Calculator
 Originally Posted by xtishx
But I'm having problems trying to write the validation rules for:
- making sure the numeral that is entered is in the correct order
- the additive rule (explained above)
My previous post was hasty so...
As with any string manipulation routine, you will have to prepare your string to facilitate manipulation, eg. no spaces, UCase(), and so on. Use the validate event of the textbox. I used only one textbox in my tests.
You will then have to parse the roman expression and place it in a structure to facilitate iteration through the operands and operators. I found inserting vbCrLf works.
VB Code:
Public Sub ExpressionToArray(ByVal Expression As String, RetArray() As String) As Long
ExpressionToArray = -1 'init value
'It is assumed that spaces have already been eliminated
Expression = Replace(Expression, "+", vbCrLf & "+" & vbCrLf)
Expression = Replace(Expression, "-", vbCrLf & "-" & vbCrLf)
Expression = Replace(Expression, "*", vbCrLf & "*" & vbCrLf)
Expression = Replace(Expression, "/", vbCrLf & "/" & vbCrLf)
'You can insert a check here
'If there is a leading or trailing vbCrLf, or there are two consecutive vbCrLf's
' in the expression then you are lacking operands or have to many operators
RetArray = Split(Expression, vbCrLf)
ExpressionToArray = 0
End Sub
'You can then iterate on the returned array, eg:
'
'Dim sArray() As String
'
' Call ExpressionToArray(txtInput.Text, sArray)
' For x=0 To Ubound(sArray)
' Select Case sArray(x)
' Case "+", "-", "*", "/"
' 'do nothing, skip operators
' Case Else
' Call RomanToDecimal(sArray(x)) 'pass the roman operand and update array value
' End Select
' Next
'
' txtOutput.Text = EvaluateExpression(Join(sArray, ""))
You can then do 3 levels of validation on each roman operand:
- An initial check to screen out invalid characters (A, B, E...) and the more obvious errors such as IL or VV. This can easily be accomplished with InStr() > 0
- Validity checks during the conversion into an expression based on places (thousands, hundreds, tens, and ones as mentioned in Rhino's link).
- Rechecking the expression to make sure that the values progress from thousands place to ones correctly as I explained in an earlier post.
Conversion based on digit place is a lot easier than nested conditional statements to process the string from left to right. You will also need a sub to count the number of instances of your roman character.
VB Code:
Public Function CharCount(RomanChar As String, Expression As String) As Long
Dim sTemp As String
sTemp = Replace(Expression, RomanChar, "")
CharCount = (Len(Expression) - Len(sTemp)) / Len(RomanChar)
End Function
Public Function RomanToDecimal(ByRef RomanNum As String) As Long
'note that I passed by reference so I can update the value of RomanNum in the called procedure
RomanToDecimal = -1
If SimpleCheck(RomanNum) <> ) Then error occured 'check for common errors and invalid letters
If ReplaceOnes(RomanNum) <> 0 Then error occured
If ReplaceTens(RomanNum) <> 0 Then error occured
If ReplaceHundreds(RomanNum) <> 0 Then error occured
If ReplaceThousands(RomanNum) <> 0 Then error occured
'you will need to correct the leading + sign in the final expression
If PlacesCheck(RomanNum) <> 0 Then error occured
'You will reuse ExpressionToArray() in placescheck() to
'get an array to check by iteration to ensure that
'the decimal values progress in order from thousands to ones
romanNum = EvaluateExpression(RomanNum) & ""
RomanToDecimal = 0 'completed successfully
End Function
'Pseudocode for ones place, do the same for the other places
' note validity checks during conversion
Public Function ReplaceOnes(ByRef Expression As String) As Long
Dim IVcount As Long
Dim IXcount As Long
ReplaceOnes = -1
IVcount = CharCount("IV", Expression)
IXcount = CharCount("IX", Expression)
If IVcount > 0 and IXcount > 0 Then
'error, IX and IV cannot exist at the same time
ElseIf IVcount > 1 Then
'error, only one IV in a number
ElseIf IXcount > 1
'error only one instance of IX is valid
ElseIf IVcount = 1
'notice no need to check IXcount = 0,
'it is implied cause first case already handles IVcount > 0 and IXcount > 0
Expression = Replace(Expression, "IV", "+4")
If CharCount("I", Expression) > 0 Or CharCount("V", Expression) > 0 Then
'error, I's or V exist when IV already used
End If
ElseIf IXcount = 1
'almost same format as IVcount = 1, test I's and X
ElseIf IVcount = 0 and IXcount = 0
If CharCount("V", Expression) > 1 Then
'error, only one V is valid
Else
Expression = Replace(Expression, "V", "+5")
End If
If CharCount("I", Expression) > 3 Then
'error, only up to 3 I's valid
Else
Expression = Replace(Expression, "I", "+1")
End If
End If
ReplaceOnes = 0 'completed with no errors
End Sub
Public Function EvaluateExporession()
Dim oSCRCNT As New ScriptControl
'sorry have to go... but this will have only about 3 to 5 lines
'anyone for the save??
End Function
And I mentioned the script control and the eval method. Thread starter, this is what you will use to perform arithmetic operations on your roman numerals.
You enter an expression: "X + VII"
You get an expression array and call romantodecimal:
Array("10","+","5 + 1 + 1") --> Array("10","+", "7") after romantodecimal call
RomanToDecimal also performs the Eval method of the script control to get the result of 5+1+1 = 7
You then perform another Eval on the final expression "10+7"
You will need to set the project reference of the script control
You will need to create a new instance of the object in EvaluateExpression()
you will need to set the lagnuage property of the object to "vbscript"
Then finally call the Eval method and get the result of your arithmetic expression.
Last edited by leinad31; Mar 25th, 2006 at 02:37 AM.
-
Mar 26th, 2006, 05:03 AM
#22
New Member
Re: Roman Numeral Calculator
This should be useful.
This is AS level Computing Coursework. The Course specifically states:
 Originally Posted by OCR Structured Practical Computing Tasks
The work which you submit for assessment must be your own.
If you copy from someone else or allow another candidate to copy from you, or if you cheat in
any other way, you may be disqualified from at least the subject concerned.
I am in the same boat as you, I had to to (and have already finished) this same coursework; go ask the one person you're allowed to: your teacher.
Last edited by savara; Mar 26th, 2006 at 07:48 AM.
-
Mar 27th, 2006, 10:32 AM
#23
New Member
Re: Roman Numeral Calculator
Still confused,
Thanks for all your help with it,
I have code, just need to assign the code to a button, if ya get wot i mena
-
Mar 27th, 2006, 11:44 AM
#24
Re: Roman Numeral Calculator
__________________
________________0îîî___
___îîî0________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
-
Apr 3rd, 2006, 10:28 AM
#25
New Member
Re: Roman Numeral Calculator
im still no further on with it
-
Apr 3rd, 2006, 10:30 AM
#26
Re: Roman Numeral Calculator
 Originally Posted by superleeds27
im still no further on with it
What is the current stumbling block?
-
Apr 3rd, 2006, 10:32 AM
#27
New Member
Re: Roman Numeral Calculator
well, i have my design and the boxes labelled
I just have no idea what to do in Vb, no idea of code or anything
-
Apr 3rd, 2006, 10:38 AM
#28
Re: Roman Numeral Calculator
 Originally Posted by superleeds27
well, i have my design and the boxes labelled
I just have no idea what to do in Vb, no idea of code or anything 
No code 
There is a whole thread full of code here.
-
Apr 3rd, 2006, 11:24 AM
#29
New Member
Re: Roman Numeral Calculator
yer no idea where to put it, or anything, totally new to VB
-
Apr 3rd, 2006, 07:16 PM
#30
New Member
Re: Roman Numeral Calculator
@superleeds27 : try to ask xtishx (Thread Starter) if both of u are taking the same course. Ask him/her, how to start VB, design the form and how to put the code. Then try to read this thread form the begining. Becouse we don't know in which level of VB's Programmer u are.
- jude7 -
NB: Have u done a "Hello World" project ?
-
Apr 13th, 2006, 06:00 AM
#31
Re: Roman Numeral Calculator
Superleeds,
If what your doing is based on my code then in the code I posted, the one place in the command button's click event is...
BTW, Its in pseudocode
VB Code:
'You can then iterate on the returned array, eg:
'
'Dim sArray() As String
'
' Call ExpressionToArray(txtInput.Text, sArray)
' For x=0 To Ubound(sArray)
' Select Case sArray(x)
' Case "+", "-", "*", "/"
' 'do nothing, skip operators
' Case Else
' Call RomanToDecimal(sArray(x)) 'pass the roman operand and update array value
' End Select
' Next
'
' txtOutput.Text = EvaluateExpression(Join(sArray, ""))
Note:
1) The textbox.text value is parsed (string manipulation) with ExpressionToArray() to isolate the operands (or the roman numerals as strings) as a member of an array
2) You then iterate through the array/operands (skipping the operators) and replacing each roman numeral string with its equivalent decimal value.
3) The modified array (decimal operands) is then converted into a single string expression through Join()
4) This 'concatenated' expression is then passed to the Eval method of the script control and the result (a decimal) is shown in another textbox.
I used the script control approach due to the thread starters other requirements as I quoted in my previous post. Code I left out are SimpleCheck(), PlacesCheck() and the convertion for the tens, hundreds and thousands place. I had assumed the thread starter could build up on the concept and do the other code himself.
Last edited by leinad31; Apr 13th, 2006 at 06:05 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|