|
-
Sep 20th, 2000, 08:45 AM
#1
Thread Starter
Hyperactive Member
Hi there,
thanks to a bit of help and some adjustments, my simplify function is getting better - but not quite perfect.
I think it's a problem with that stupid Mod operator - tell me if I'm wrong.
Anyway, it works in the sense that things like:
6/4 will be given as 3/2
4/1 will be given as 4
10/2 will be given as 5
-3/9 will be given as -1/3
0/5 will be given as 0
But for fractions like -9/1, -1/-5, -3/-3, 1/-8....nothing is returned, no answer is given. Could someone please take a look at it. I've been at it for days.
Thanks for any help!
------------
Dim a As Integer
Dim b As Integer
Private Sub Command1_Click()
a = Int(Rnd * 20) - 10
b = Int(Rnd * 20) - 10
If b = 0 Then
b = 1
End If
Picture1.Cls
Picture1.Print a & "/" & b
Picture2.Cls
Picture2.Print Fraction(a, b)
End Sub
''this function reduces fractions
Function Fraction(numerator As Integer, denominator As Integer) As String
Dim i As Integer
For i = CInt(IIf(denominator >= numerator, denominator, numerator) / 2) To 1 Step -1
If numerator Mod i = 0 And denominator Mod i = 0 Then
Fraction = (numerator / i) & "/" & (denominator / i)
'the number is one
If numerator = denominator Then
Fraction = 1
End If
'whole number answer
If numerator Mod denominator = 0 And numerator <> denominator Then
Fraction = numerator / denominator
End If
Exit For
End If
Next:
End Function
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
|