|
-
Oct 5th, 2009, 02:16 PM
#1
Thread Starter
New Member
help with simplifying a fraction
hello everyone, I have a program here and basically its got several subs and functions, and a for thats got two textboxes for user input. the person inputs a numerator and denominator. when i click two buttons, one checks for valid input(that the number is an integer) and the simplify button simplifies the fraction using the gcf. I have code written, but for some reason, it keeps displaying the original fraction that i entered in the textboxes when i click simplifiy. can someone explain whats wrong with my code? code is listed below and yes it is homework, but the i have struggled on this part only for days and im seeking help now. the rest of it i was able to do fine. the red bold part is the trouble area
Option Strict On
Public Class frmGCD
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvertFun.Click
Dim Numer As Integer
Dim Denom As Integer
Dim singleResult As Single
Dim doubleResult As Double
Dim decimalResult As Decimal
If ValidInput(txtNumer.Text) And ValidInput(txtDenom.Text) Then
'convert input to integers
Numer = CInt(txtNumer.Text)
Denom = CInt(txtDenom.Text)
' convert input to results
singleResult = CSng(Numer) / CSng(Denom)
doubleResult = CDbl(Numer) / CSng(Denom)
decimalResult = CDec(Numer) / CDec(Denom)
'display results
txtSngResult.Text = CStr(singleResult)
txtDblResult.Text = CStr(doubleResult)
txtDecResult.Text = CStr(decimalResult)
Else
MessageBox.Show("invalid input")
End If
End Sub
Private Sub btnSimplify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimplify.Click
Dim inNum As Integer
Dim inDen As Integer
If Not ValidInput(txtNumer.Text) Or _
Not ValidInput(txtDenom.Text) Then
MessageBox.Show("invalid input")
Exit Sub
End If
'convert input to integers
inNum = CInt(txtNumer.Text)
inDen = CInt(txtDenom.Text)
Call Simplify(inNum, inDen)
txtSimple.Text = CStr(inNum) & " / " & CStr(inDen)
End Sub
Private Function ValidInput(ByVal result As String) As Boolean
'=======DO NOT CHANGE ANY OF THE CODE ABOVE THIS LINE =======
'=======ValidInput Procedure======================
' task - check for valid numeric input
' given - a string of input characters
' return - true if string represents valid input, otherwise return false
'add your code here for the ValidInput procedure
Integer.TryParse(txtNumer.Text, CInt(result))
If CDbl(result) >= 1 Then
Return True
Else : Return False
End If
Integer.TryParse(txtDenom.Text, CInt(result))
If CDbl(result) >= 1 Then
Return True
Else : Return False
End If
End Function
'======Simplify Procedure==================
'task - reduce a fraction to its simplest form
'given - a valid numerator and denominator
'return - simplified numerator and denominator
'add your code here for the Simplify procedure
Private Function simplify(ByVal innum As Integer, ByVal inden As Integer) As Integer
Dim temp As Long
Dim cfac As Integer
Do While inden > 0
innum = CInt(Val(txtNumer.Text))
inden = CInt(Val(txtDenom.Text))
temp = inden
inden = innum Mod inden
innum = CInt(temp)
cfac = innum
innum = (innum \ cfac)
inden = (inden \ cfac)
Loop
End Function
Private Sub txtnumer_TextChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles txtNumer.TextChanged
Call clearresults()
End Sub
Private Sub txtDenom_Textchanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles txtDenom.TextChanged
Call clearresults()
End Sub
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
|