Computing.Net > Forums > Programming > visual basic calculator

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

visual basic calculator

Reply to Message Icon

Original Message
Name: mahd
Date: March 4, 2004 at 19:14:34 Pacific
Subject: visual basic calculator
OS: XP
CPU/Ram: 933
Comment:

hello there..
i am supposed to write a program in visual basic that allows a user to input roman numbers and the program converts them to eng numbers
the roman numbers are:
I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000

the letters must be in descending order.
the value of the number is the sum of all the letters
each letter must be used a minimum number of time

allow the user to enter the roman numbers up to a value of 7000 (MMMMMMM)
prevent the user entering an invalid number
display the number entered using roman numbers
update this display as each roman number is entered
display the number entered using eng numbers
allow the user to clear both displays, romans and english.

my problem is i dont have any background of visual basic .. if anyone could help me with this problem it would be nice from him..

and thanx :)


Report Offensive Message For Removal


Response Number 1
Name: StuartS
Date: March 4, 2004 at 20:00:20 Pacific
Reply: (edit)

You want to write a programme in VB but have no background in VB, in which case you have a lot of reading to do. Do you have any programming experience? If not then you have even more reading to do.

If I were to tell you to put two text boxes on a form and use the change event of one text box to modify the other text box it probably won't mean a lot to you.

I don't mind helping somebody with a problem, but writing the whole thing is another matter. At least make an attempt at the problem and then post back when you get stuck.

>> display the number entered using eng numbers <<

There is no such thing a English numbers. The numbers 1,2,3,4 etc you and I are familiar with are Arabic.

Stuart


Report Offensive Follow Up For Removal

Response Number 2
Name: mahd
Date: March 4, 2004 at 22:59:28 Pacific
Reply: (edit)

well..yes i have no background in visual basic..but i know a little in C..and i tried doing the reading but i figured out that i'm running out of time..and it's not going to work easily

so i started asking for commands and codes and i think it worked a little with me..and i completed the program..however i have some errors in the output when i test it for a wide range of data

i've uploaded the program and if anybody can have a look on it and fix the error for me i'm going to be thankfull for him :)

this is the URL:
http://www.geocities.com/mahd_2d/vb.zip


Report Offensive Follow Up For Removal

Response Number 3
Name: wizard-fred
Date: March 5, 2004 at 00:05:04 Pacific
Reply: (edit)

In post 9602, Feb 19, 2004, you were a beginning C programmer when you posted the same problem. Now your little C experience of 2 weeks is not enough so you are trying VB. I outlined what I thought were the things you had to test in the previous post.

Note for observers: They are not forming Roman numerals the standard way. Numbers are formed by values decending.

The first thing observed that is an error is the test ---- len(txt.Text) > 7 ----. My previous post gave a Roman numeral for 6999 that is consierably longer than 7 characters.
MMMMMMDCCCCLXXXXVIIII. The smallest Roman number to fail the length test is XXXVIIII (39).

I have not run your example but some of the other comparisons appear in error.

Back to the drawing board.


Report Offensive Follow Up For Removal

Response Number 4
Name: StuartS
Date: March 5, 2004 at 07:18:16 Pacific
Reply: (edit)

Try this:

Private Sub Text1_Change()

Dim a As Integer
Dim b As Integer
Dim total As Integer
Dim RomanNumber As String
Dim RomanDigit As String

a = Len(Text1.Text)
RomanNumber = Text1.Text


For b = a To 1 Step -1
RomanDigit = Mid$(RomanNumber, b, 1)
Select Case RomanDigit
Case "X"
total = total + 10
Case "I"
total = total + 1
Case "V"
total = total + 5
Case Else
MsgBox ("Invalid Character")
End Select
Next

Text2.Text = total

I will leave it to you to enter the rest of the Roman Numerals and devise error traps. You will also need to ensure that characters entered are in upper case. A lower case X can cause havoc. Do a search in VB Help for Ucase.

As Wizard-Fred says, when doing any kind of numerical conversion, whether is be Roman to Arabic or Binary to Decimal, you start with the lowest values first and work you way up.

As written your code and this assumes that the classic Roman notation is used. e.g. 24 would be written as XXIIII as the Romans would be familiar with. However modern notation means 24 would be written as XXIV. Twenty + five - one. Something else to consider.

Stuart


Report Offensive Follow Up For Removal

Response Number 5
Name: wizard-fred
Date: March 5, 2004 at 10:39:54 Pacific
Reply: (edit)

As I understand the statement of the problem, you have to test the validity of the Roman numeral character by character as it is entered, just making the inputing and testing a little more difficult.


Report Offensive Follow Up For Removal


Response Number 6
Name: StuartS
Date: March 5, 2004 at 11:31:17 Pacific
Reply: (edit)

You cannot just evaluate each character as it is entered in isolation, especially if you are using modern notation. You need to test the whole string each time a character is added. That's where the text change event comes in handy.

It looks like each character is be evaluated in isolation but is in fact evaluating the whole string. It's fast enough not to be noticed by the user.

Stuart


Report Offensive Follow Up For Removal

Response Number 7
Name: wizard-fred
Date: March 5, 2004 at 11:50:08 Pacific
Reply: (edit)

Stuart: Sorry to have misinterpeted your code. I'm still in the Dark Ages (pre-VB). Since modern notation is not being used, you could evaluate left to right. My solution in PowerBasic for DOS took 90 odd lines. Used INKEY$ to build number, checking and updating display after each character is entered.


Report Offensive Follow Up For Removal

Response Number 8
Name: mahd
Date: March 5, 2004 at 13:30:46 Pacific
Reply: (edit)

thanx all for your help..but as i mentioned i have no background using visual basic..

i'm copying the whole program here..and can u modify it and post it here as a full program plz

Private Sub Command1_Click()

If Len(txt.Text) > 7 Then
MsgBox "Length should be 7 Numbers At Maximum", vbCritical
txt.SetFocus
Exit Sub
End If

If IsNumeric(txt.Text) Then
MsgBox "You Should Enter Roman Numbers Only", vbCritical
Exit Sub
End If

Dim arr(7) As Integer
Dim namex(6) As String
namex(0) = "I"
namex(1) = "V"
namex(2) = "X"
namex(3) = "L"
namex(4) = "C"
namex(5) = "D"
namex(6) = "M"

Dim d, i, l, j As Integer
l = Len(txt.Text)

Dim pos As Integer
pos = 0

Dim count As Integer
count = 0

'checking for the first letter
For i = 1 To l
pos = InStr(i, txt.Text, namex(0))
If pos > 0 Then
arr(pos - 1) = 1
count = count + 1
End If
Next

'check for repeats of the letter
If count > 4 Then
MsgBox "A Roman Number Is Repeated More Than Enough", vbCritical
Exit Sub
End If
count = 0

'checking for the 2nd letter
pos = 1
For i = 1 To l
pos = InStr(i, txt.Text, namex(1))
If pos > 0 Then
arr(pos - 1) = 5
count = count + 1
End If
Next

'check for repeats of the letter
If count > 1 Then
MsgBox "A Roman Number Is Repeated More Than Enough", vbCritical
Exit Sub
End If
count = 0

'checking for the 3rd letter
pos = 1
For i = 1 To l
pos = InStr(i, txt.Text, namex(2))
If pos > 0 Then
arr(pos - 1) = 10
count = count + 1
End If
Next

'check for repeats of the letter
If count > 4 Then
MsgBox "A Roman Number Is Repeated More Than Enough", vbCritical
Exit Sub
End If
count = 0

'checking for the 4th letter
pos = 1
For i = 1 To l
pos = InStr(i, txt.Text, namex(3))
If pos > 0 Then
arr(pos - 1) = 50
count = count + 1
End If
Next

'check for repeats of the letter
If count > 1 Then
MsgBox "A Roman Number Is Repeated More Than Enough", vbCritical
Exit Sub
End If
count = 0


'checking for the 5th letter
pos = 1
For i = 1 To l
pos = InStr(i, txt.Text, namex(4))
If pos > 0 Then
arr(pos - 1) = 100
count = count + 1
End If
Next

'check for repeats of the letter
If count > 4 Then
MsgBox "A Roman Number Is Repeated More Than Enough", vbCritical
Exit Sub
End If
count = 0


'checking for the 6th letter
pos = 1
For i = 1 To l
pos = InStr(i, txt.Text, namex(5))
If pos > 0 Then
arr(pos - 1) = 500
count = count + 1
End If
Next

'check for repeats of the letter
If count > 2 Then
MsgBox "A Roman Number Is Repeated More Than Enough", vbCritical
Exit Sub
End If
count = 0


'checking for the 7th letter
pos = 1
For i = 1 To l
pos = InStr(i, txt.Text, namex(6))
If pos > 0 Then
arr(pos - 1) = 1000
count = count + 1
End If
Next

'check for repeats of the letter
If count > 7 Then
MsgBox "A Roman Number Is Repeated More Than Enough", vbCritical
Exit Sub
End If
count = 0


'check for invalid chars
For j = 0 To l - 1
If arr(j) = 20 Then
MsgBox "Invalid characheters", vbCritical
Exit Sub
End If
Next

'check for decreasing order in the array
For i = 0 To l - 1
If Int(arr(i)) < Int(arr(i + 1)) And Not i >= l - 1 Then
MsgBox "Roman Numbers Should Be In Decreasing Order"
'Exit Sub
End If
Next

'print the array to the list
For i = 0 To l - 1
List1.AddItem arr(i)
Next

' print the total
Dim total As Integer
total = 0
For i = 0 To l - 1
total = total + arr(i)
Next
Label1.Caption = total


End Sub


Report Offensive Follow Up For Removal

Response Number 9
Name: StuartS
Date: March 5, 2004 at 14:08:49 Pacific
Reply: (edit)

>> I'm copying the whole program here..and can u modify it and post it here as a full program Pl <<

Afraid not, for two reasons. You code is so convulated and cumbersome as to be beyond redemption. An abundance of If/Then construct and For/Next loops makes for code hard to understand and slow to run. Work on the code I gave you. It needs a bit of work on it, but that the os the way to learn. All the code required is inside one For/Next loop. Using VBs excellent debugging aids, it should be easy to expand it to do the job and spot where errors might arise.

Second reason. Because of the convoluted nature of your code it would take me half the night to work out exactly what it is your are doing. I cannot understand why you thing more than one instance of a number is an error. Besides if I posted the entire code in working order, you wouldn't learn anything.

Programming is one problem after another. If you don't like problem solving or would sooner have someone else solve them for you, then give up programming.

Stuart


Report Offensive Follow Up For Removal

Response Number 10
Name: mahd
Date: March 5, 2004 at 14:15:19 Pacific
Reply: (edit)

i know it is my mistake..but i really dont have time to read, learn and program..

i couldnt attend my classes for some personal reasons and here i am asking for ur help..can u plz?


Report Offensive Follow Up For Removal

Response Number 11
Name: StuartS
Date: March 5, 2004 at 20:53:39 Pacific
Reply: (edit)

If you failed to attend classes through sickness or bereavement or some other unavoidable mishap, I am sure any reasonable tutor would give you some extra time.

However, if you failed to attend classes because you though you had better things to do, then thats a problem for you to solve. A couple of hours work on the code I have posted should give you a working programme.

If I did do the entire programme for you, your tutor would look at it and realise straight away that it was not your work. Unavoidable as styles, techniques and methods would be different. Different even than what you have been taught. Not wrong, just different. Tutors tend to take a dim view of students submitting work done by someone else.

You would do better submitting what you can and get some marks for trying, then submit someone else's work and get nothing.

Stuart


Report Offensive Follow Up For Removal

Response Number 12
Name: ahmed_211
Date: March 9, 2004 at 06:40:04 Pacific
Reply: (edit)

i only have one problem in the whole calculator.. i want to know how to convert from arabic to roman... if i do this part then am fisnhed with the whole calculator..
anyhelp somone

?


Report Offensive Follow Up For Removal

Response Number 13
Name: StuartS
Date: March 9, 2004 at 06:57:41 Pacific
Reply: (edit)

Start with the highest Roman numeral M and divide the Arabic number by 1000. The result will be the number of Ms. Discard the result and divide the remainder by each descending Roman numeral in turn till your are left with < 5.


Stuart


Report Offensive Follow Up For Removal

Response Number 14
Name: kkkk
Date: March 13, 2004 at 06:09:01 Pacific
Reply: (edit)

ahmed
Are you sure you are doing the right thing? As far as i know this is the May/June Cambridge Computing task paper question.You suppose to do it by youself,but not asking for the code from others. It is CHEATING!


Report Offensive Follow Up For Removal

Response Number 15
Name: Aureo
Date: March 22, 2004 at 23:53:09 Pacific
Reply: (edit)

Well i really would love to see the interface so that i can do as it is, i have an IT exam and that is the firts visual basic project i ma suposed to present, i found the codes but i wont be able to use it if i do not know how does the interface looks like.
I really would be very gratefull to you if you be kind enough to sen it to me.

SleepyEye


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software