|
-
Dec 18th, 2002, 01:28 AM
#1
Thread Starter
Junior Member
Sorry... still need help!!
Yeah i asked this question a while ago, and i thought i had the answer but it just didnt work out right. I believ i didnt specify what i needed so dont blame yourselves here it is:
Create a Reduce Fraction application that takes an integer numerator and denominator of a fraction and then displays the fraction reduced or a message stating the fraction cannot be reduced. A fraction may be reduced by finding the largets common factor and dividing both the numerator and denominator by this factor.
SO please, if u can just give me a jump i can use!
-
Dec 18th, 2002, 01:31 AM
#2
Rather than starting all over, why not post the code you already have and let us know what the problem(s) is/are.
-
Dec 18th, 2002, 11:29 AM
#3
I can never remember which is the numerator and which is the denominator. This simple function should get you started.
VB Code:
Private Function GetCommonFactor(Numerator As Long, Denominator As Long, CommonFactor As Long) As Boolean
Dim lngIdx As Long
For lngIdx = 2 To Numerator
If Numerator Mod lngIdx = 0 Then
If Denominator Mod lngIdx = 0 Then
CommonFactor = lngIdx
End If
End If
Next
If CommonFactor > 0 Then
Numerator = Numerator / CommonFactor
Denominator = Denominator / CommonFactor
GetCommonFactor = True
End If
Debug.Print Numerator, Denominator, CommonFactor
End Function
-
Dec 19th, 2002, 11:30 AM
#4
Thread Starter
Junior Member
ok... heres the code im kind of useing right now...
Private Sub Cmdreduce_Click()
Dim numer As Integer
Dim deno As Integer
Dim gcf As Integer
numer = txtNumer.Text
deno = txtDeno.Text
gcf = deno / numer
Do While gcf Mod 0
gcf = gcf - 1
Loop
End Sub
i know theres something wron, so please help me with that!?
-
Dec 19th, 2002, 12:21 PM
#5
Thread Starter
Junior Member
here is what is updated so far... i pretty much have the stuff but i just need to find out one thing:
[code]
Dim numer As Integer
Dim deno As Integer
Dim gcf As Integer
numer = txtNumer.Text
deno = txtDeno.Text
gcf = numer
Do While gcf Mod
gcf = gcf - 1
Loop
lblFract.Caption = numer & "/" & deno & " is reduced to " & _
numer / gcf & "/" & deno / gcf
[code]
i need to find out how to make the mod on the do... loop statement be greater then 0.
-
Dec 19th, 2002, 12:59 PM
#6
Are you having trouble with the function that I wrote or did I misunderstand your problem?
Here is an updated more efficient version.
VB Code:
Private Function GetCommonFactor(Numerator As Long, Denominator As Long, CommonFactor As Long) As Boolean
Dim lngIdx As Long
CommonFactor = 1
For lngIdx = Numerator To 2 Step -1
If Numerator Mod lngIdx = 0 Then
If Denominator Mod lngIdx = 0 Then
CommonFactor = lngIdx
Numerator = Numerator / CommonFactor
Denominator = Denominator / CommonFactor
GetCommonFactor = True
Exit For
End If
End If
Next
End Function
Call the function using something like the following.
VB Code:
Dim lngNumerator As Long
Dim lngDenominator As Long
Dim lngCommonFactor As Long
lngNumerator = CLng(Text1.Text)
lngDenominator = CLng(Text2.Text)
If GetCommonFactor(lngNumerator, lngDenominator, lngCommonFactor) Then
Label1.Caption = "The Fraction " & Text1.Text & "/" & Text2.Text & " has a common factor of " & lngCommonFactor & _
" and can be reduced to " & lngNumerator & "/" & lngDenominator
Else
Label1.Caption = "The Fraction " & lngNumerator & "/" & lngDenominator & " cannot be reduced."
End If
Last edited by brucevde; Dec 19th, 2002 at 01:02 PM.
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
|