PDA

Click to See Complete Forum and Search --> : Can someone check this code to see If I could make it smaller


SandmanSamR
Jan 19th, 2004, 11:56 AM
I was told to make a program that makes the user input a numerical value of 1-999 and the computer prints out that number in letters (i.e "123" = "One hundred and twenty three"). This is the solution I came up with: Dim valu As Integer
Option Explicit


Private Sub Command1_Click()
lblConverted.Caption = ""
valu = val(Text1.Text)

If valu >= 900 Then
valu = valu - 900
lblConverted.Caption = "Nine Hundred "
ElseIf valu >= 800 Then
valu = valu - 800
lblConverted.Caption = "Eight Hundred "
ElseIf valu >= 700 Then
valu = valu - 700
lblConverted.Caption = "Seven Hundred "
ElseIf valu >= 600 Then
valu = valu - 600
lblConverted.Caption = "Six Hundred "
ElseIf valu >= 500 Then
valu = valu - 500
lblConverted.Caption = "Five Hundred "
ElseIf valu >= 400 Then
valu = valu - 400
lblConverted.Caption = "Four Hundred "
ElseIf valu >= 300 Then
valu = valu - 300
lblConverted.Caption = "Three Hundred "
ElseIf valu >= 200 Then
valu = valu - 200
lblConverted.Caption = "Two Hundred "
ElseIf valu >= 100 Then
valu = valu - 100
lblConverted.Caption = "One Hundred "
End If

If valu >= 90 Then
valu = valu - 90
lblConverted.Caption = lblConverted.Caption & "and Ninty-"
ElseIf valu >= 80 Then
valu = valu - 80
lblConverted.Caption = lblConverted.Caption & "and Eighty-"
ElseIf valu >= 70 Then
valu = valu - 70
lblConverted.Caption = lblConverted.Caption & "and Seventy-"
ElseIf valu >= 60 Then
valu = valu - 60
lblConverted.Caption = lblConverted.Caption & "and Sixty-"
ElseIf valu >= 50 Then
valu = valu - 50
lblConverted.Caption = lblConverted.Caption & "and Fifty-"
ElseIf valu >= 40 Then
valu = valu - 40
lblConverted.Caption = lblConverted.Caption & "and Fourty-"
ElseIf valu >= 30 Then
valu = valu - 30
lblConverted.Caption = lblConverted.Caption & "and Thirty-"
ElseIf valu >= 20 Then
valu = valu - 20
lblConverted.Caption = lblConverted.Caption & "and Twenty-"
End If

If valu >= 19 Then
lblConverted.Caption = lblConverted.Caption & "and Ninteen"

ElseIf valu >= 18 Then
lblConverted.Caption = lblConverted.Caption & "and Eighteen"

ElseIf valu >= 17 Then
lblConverted.Caption = lblConverted.Caption & "and Seventeen"

ElseIf valu >= 16 Then
lblConverted.Caption = lblConverted.Caption & "and Sixteen"

ElseIf valu >= 15 Then
lblConverted.Caption = lblConverted.Caption & "and Fifteen"

ElseIf valu >= 14 Then
lblConverted.Caption = lblConverted.Caption & "and Fourteen"

ElseIf valu >= 13 Then
lblConverted.Caption = lblConverted.Caption & "and Thirteen"

ElseIf valu >= 12 Then
lblConverted.Caption = lblConverted.Caption & "and Twelve"

ElseIf valu >= 11 Then
lblConverted.Caption = lblConverted.Caption & "and Eleven"

ElseIf valu >= 10 Then
lblConverted.Caption = lblConverted.Caption & "Ten"

ElseIf valu >= 9 Then
lblConverted.Caption = lblConverted.Caption & "Nine"

ElseIf valu >= 8 Then
lblConverted.Caption = lblConverted.Caption & "Eight"

ElseIf valu >= 7 Then
lblConverted.Caption = lblConverted.Caption & "Seven"

ElseIf valu >= 6 Then
lblConverted.Caption = lblConverted.Caption & "Six"

ElseIf valu >= 5 Then
lblConverted.Caption = lblConverted.Caption & "Five"

ElseIf valu >= 4 Then
lblConverted.Caption = lblConverted.Caption & "Four"

ElseIf valu >= 3 Then
lblConverted.Caption = lblConverted.Caption & "Three"

ElseIf valu >= 2 Then
lblConverted.Caption = lblConverted.Caption & "Two"

ElseIf valu >= 1 Then
lblConverted.Caption = lblConverted.Caption & "One"

End If



End Sub

randem
Jan 19th, 2004, 12:27 PM
SandmanSamR

Yeah Man, Put those things in an array and only reference them once.I will just give you the concept.
Arr1 = Array("","One","Two","Three",...,"Ninteen")
Arr10 = Array("","Ten","Twenty","Thirty",...,"Ninety")

x100 = Int(value / 100)
x10 = Int((value - (x100 * 100)))
x1 = (value - (x100 * 100) - (x10 * 10))

Arr1(x100) "Hundred and " & Arr10 (x10) & " Dollars"
You get the idea?

SandmanSamR
Jan 19th, 2004, 12:31 PM
I just started vb this year. We have yet to get to arrays. Looking at that code though I say I have some understanding of whats going on. Would you mind summerizing so I can assure myself of what exaclty is going on so that I can use this if and when I need it in the future

randem
Jan 19th, 2004, 12:48 PM
SandmanSamR

OK, backing up...An array is just a place to hold a series of like data types where you can reference them by an index.

From the code I gave you:

Arr1(1) = "One"
Arr1(2) = "Two" ....

Arr10(1) = "Ten"
Ar10(2) = "Twenty" ...

So we can reference any item in the array by giving it's index to it. Now when taking the statement:

x100 = Int(value / 100)

with value equal to 525

x100 = Int(525 / 100) which equals 5

Now going to the array we get:

Arr1(5) = "Five"

And we do that for each value and array. Getting clearer?

Kasracer
Jan 19th, 2004, 12:49 PM
Wrong forum

Maldrid
Jan 19th, 2004, 12:49 PM
Randem has the right idea, but you have to do a little more checking than that. Or else you would be printing "hundred and" even though you just entered 20 in the text box. Here is some code that works.


Private Sub Command1_Click()
Dim sNum(1 To 9) As String
Dim sOnes(1 To 9) As String

Dim Hundred As Integer
Dim Ten As Integer
Dim One As Integer

sNum(1) = "Eleven"
sNum(2) = "Twelve"
sNum(3) = "Thir"
sNum(4) = "Four"
sNum(5) = "Fif"
sNum(6) = "Six"
sNum(7) = "Seven"
sNum(8) = "Eigh"
sNum(9) = "Nine"

sOnes(1) = "One"
sOnes(2) = "Two"
sOnes(3) = "Three"
sOnes(4) = "Four"
sOnes(5) = "Five"
sOnes(6) = "Six"
sOnes(7) = "Seven"
sOnes(8) = "Eight"
sOnes(9) = "Nine"


lblConverted.Caption = ""

If Len(Text1) = 3 Then
Hundred = CInt(Left$(Text1, 1))
Ten = CInt(Mid$(Text1, 2, 1))
One = CInt(Right$(Text1, 1))
ElseIf Len(Text1) = 2 Then
Hundred = 0
Ten = CInt(Left$(Text1, 1))
One = CInt(Right$(Text1, 1))
ElseIf Len(Text1) = 1 Then
Hundred = 0
Ten = 0
One = Text1
Else
Hundred = 0
Ten = 0
One = 0
End If

If Hundred > 0 Then
lblConverted = sOnes(Hundred) & " Hundred "
If Ten > 0 Or One > 0 Then
lblConverted = lblConverted & " and "
End If
End If

If Ten > 0 Then
If Ten = 1 Then
Select Case One
Case 1, 2
lblConverted = lblConverted & sNum(One)
Case Else
lblConverted = lblConverted & sNum(One) & "teen"
End Select
Else
Select Case Ten
Case 2
lblConverted = lblConverted & "Twenty"
Case Else
lblConverted = lblConverted & sNum(Ten) & "ty"
End Select

If One > 0 Then
lblConverted = lblConverted & "-"
End If
End If
End If

'This was the missing code. I don't know how that didnlt copy
If Ten <> 1 Then
If One > 0 Then
lblConverted = lblConverted & sOnes(One)
End If
End If
End Sub


[edit]
Oops somehow I lost some of the code. Hold on.

SandmanSamR
Jan 19th, 2004, 12:53 PM
cool i get it. Quick question though, instead of x100 = Int(525 / 100) which equals 5 could you have done x100 = (525 \ 100) which equals 5?

randem
Jan 19th, 2004, 12:54 PM
Maldrid

That is what a concept is correct? I did not plan to do what seems like his homework for him.

SandmanSamR
Jan 19th, 2004, 12:55 PM
Wrong forum

How you figure?

randem
Jan 19th, 2004, 12:56 PM
SandmanSamR

Well sort of. It depends on how the x100 was defined. I like to take that out of the equation and alway return an integer. That way it always works no matter what numeric data type was defined.

525 / 100 = 5.25

or as an integer

525 / 100 = 5

Kasracer
Jan 19th, 2004, 12:57 PM
Originally posted by SandmanSamR
Wrong forum

How you figure? Uh.... look at your title, then look at the forums... you can figure it out.

SandmanSamR
Jan 19th, 2004, 12:58 PM
I did not plan to do what seems like his homework for him.

The original program I wrote was complete. I didnt need someone to finish it for me. I wanted to know if I could cut down on the repetitivness of it. Thanks for originally not showing me hard code and concept instead though, I would rather that then just shown what to copy and paste.

randem
Jan 19th, 2004, 12:59 PM
SandmanSamR

No offense, but lots of people attempt to get their homework done by unsuspecting members here. I saw what you were doing and using all those IF's is something a beginner would do.

So, I thought it might be a homework assignment.

SandmanSamR
Jan 19th, 2004, 01:00 PM
525 / 100 = 5.25

or as an integer

525 / 100 = 5


thats why I wrote 525\100 not 525/100. I wrote \ instead of /

Maldrid
Jan 19th, 2004, 01:03 PM
Randem-

You are absolutely right. Sorry if it sounded like I was trying to say you were wrong. I knew you were just giving him the concept not the exact answer.



My previous code did not consider Ten. Here is the revised code.


Private Sub Command1_Click()
Dim sNum(1 To 9) As String
Dim sOnes(1 To 9) As String

Dim Hundred As Integer
Dim Ten As Integer
Dim One As Integer

sNum(1) = "Eleven"
sNum(2) = "Twelve"
sNum(3) = "Thir"
sNum(4) = "Four"
sNum(5) = "Fif"
sNum(6) = "Six"
sNum(7) = "Seven"
sNum(8) = "Eigh"
sNum(9) = "Nine"

sOnes(1) = "One"
sOnes(2) = "Two"
sOnes(3) = "Three"
sOnes(4) = "Four"
sOnes(5) = "Five"
sOnes(6) = "Six"
sOnes(7) = "Seven"
sOnes(8) = "Eight"
sOnes(9) = "Nine"


lblConverted.Caption = ""

If Len(Text1) = 3 Then
Hundred = CInt(Left$(Text1, 1))
Ten = CInt(Mid$(Text1, 2, 1))
One = CInt(Right$(Text1, 1))
ElseIf Len(Text1) = 2 Then
Hundred = 0
Ten = CInt(Left$(Text1, 1))
One = CInt(Right$(Text1, 1))
ElseIf Len(Text1) = 1 Then
Hundred = 0
Ten = 0
One = Text1
Else
Hundred = 0
Ten = 0
One = 0
End If

If Hundred > 0 Then
lblConverted = sOnes(Hundred) & " Hundred "
If Ten > 0 Or One > 0 Then
lblConverted = lblConverted & " and "
End If
End If

If Ten > 0 Then
If Ten = 1 Then
If One = 0 Then
lblConverted = lblConverted & "Ten"
Else
Select Case One
Case 1, 2
lblConverted = lblConverted & sNum(One)
Case Else
lblConverted = lblConverted & sNum(One) & "teen"
End Select
End If
Else
Select Case Ten
Case 2
lblConverted = lblConverted & "Twenty"
Case Else
lblConverted = lblConverted & sNum(Ten) & "ty"
End Select

If One > 0 Then
lblConverted = lblConverted & "-"
End If
End If
End If

If Ten <> 1 Then
If One > 0 Then
lblConverted = lblConverted & sOnes(One)
End If
End If
End Sub

randem
Jan 19th, 2004, 01:05 PM
SandmanSamR

Wasn't even aware that you could do that. But it would just seem to me that the notation used could confuse someone who overlooked or did not know that was possible.

SandmanSamR
Jan 19th, 2004, 01:17 PM
lol, Maldrids code is almost as long as mine. I would sacrifice having 20 = "and twenty" for a smaller code, should I just go with randems? (Plus I'm getting confused reading maldrids code lol)

Maldrid
Jan 19th, 2004, 01:23 PM
Yea you can go with randems suggestion since you are more comfortable with it. :)

randem
Jan 19th, 2004, 01:33 PM
SandmanSamR

Let me see your code when you are done. What you want to accomplish should take the most twenty lines.

MartinLiss
Jan 19th, 2004, 01:34 PM
Originally posted by SandmanSamR
Wrong forum

How you figure? It is probably better here where I have just moved it.

ZaidGS
Jan 24th, 2004, 08:22 PM
if i were you i'd code it like this:
[sorry my code is not neat as i wrote here,
not on vb]

num=563

dim x(9) as string
dim y(9) as string
dim z(9) as string

x(0) = ""
x(1) = "one"
x(2) = "two"
'....
'....
x(9) = "nine"

y(0) = ""
y(1) = ""
y(2) = "twen"
y(3) = "thir"
'....
'....
y(9) = "nine"

'dont need them :D
'dim i as long
'for i = 1 to 9
'zz(i) = xx(i) & " hundred"
'next i

'now a 563 should be segmented to 5 / 6 / 3
dim xx as byte, yy as byte, zz as byte
xx = num mod 10
yy = (num \ 10) mod 10
zz = num \ 100

dim str as string

str = y(yy) & "ty " & x(xx)

'special case treatment for yy=1, eg. 11, 12, 215
if yy=1 then
select case xx
case 0: str="ten"
case 1: str="eleven"
case 2: str="twelve"
case else: str = y(xx) & "teen" 'lines saved here ;)
end select
end if

if str = "ty " then 'special cases like: 100 , 200 , 300 ...etc
str = x(zz) & " hundred"
else
str= x(zz) & " hundred and " & str
end if

randem
Jan 24th, 2004, 08:52 PM
ZaidGS


Trade 10 lines of code for all of that???? Why????

ZaidGS
Jan 24th, 2004, 10:14 PM
Originally posted by randem
ZaidGS


Trade 10 lines of code for all of that???? Why????

so u got a 10 line code ?! ha ?!

my code is cleanest so far (i guess)

randem
Jan 24th, 2004, 10:39 PM
ZaidGS


You wish, You should really actually read the posts.

ZaidGS
Jan 25th, 2004, 09:55 AM
sorry your posts are too
brief so that they become un-understandable,
will you please tell me whats wrong ??!

randem
Jan 25th, 2004, 12:30 PM
ZaidGS

Wrong???, Nothing is wrong. It's just way too much code for a simple task (expecially when you claim it's the shortest). The post is looking for short code, not just code that works. You code will turn out longer than the original posted code.

The reason my post are short is they get to the point quicker.

ZaidGS
Jan 26th, 2004, 07:55 AM
ok, i'll take off some lines ;)

if you dont believe me count lines :P
u'll know mine is shorter, even without the
lines i removed!!

randem
Jan 26th, 2004, 08:12 AM
ZaidGS

Take a look here: Convert Dollar Amount into Words (http://www.***********/cgi-bin/countdown.cgi?DollarstoWords.zip)

A simple project that does it all.

ZaidGS
Jan 26th, 2004, 08:27 AM
wow, y its not posted here, i dont see it :confused: :blush:

randem
Jan 26th, 2004, 08:44 AM
ZaidGS

Click on the link.

ZaidGS
Jan 26th, 2004, 12:56 PM
i already clicked on the link, i just meant that
its not been posted so i didnt know of its
existance.
sorry, if that was because of ignorance from me !!!:confused:

randem
Jan 26th, 2004, 03:14 PM
ZaidGS

Oh, Sorry, I thought you meant you could not download it. I don't want to post everything I did here, unless asked for I guess. I could put it in the codebank, but this was the first request for something like this from the forum.