Results 1 to 4 of 4

Thread: help with simplifying a fraction

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    6

    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

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: help with simplifying a fraction

    Look at these 2 lines more closely
    Code:
    innum = CInt(Val(txtNumer.Text))
    inden = CInt(Val(txtDenom.Text))
    You are changing the variables after you read them and before you get to the "Loop" statement, but then each time you loop, you reset those values to what's in the textboxes. You are undoing your calculations.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: help with simplifying a fraction

    Ok so i took both those lines out in my simplify function, however above where it is in the btn simplify event, those two lines need to remain because thats what the instructor added. even with both lines removed, i still recieve the same results as prior. what else is wrong.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: help with simplifying a fraction

    I guess I didn't make myself clear. You actually have 2 issues

    1. You are passing inNum and inDen to your Simplify method ByVal. That means what you passed will never be changed when the method exits. Change ByVal to ByRef.

    2. Then you will have to figure out how to make Simplify work because, as is, inDen will always be zero on return else your loop would run infinitely.

    Edited: P.S. This is .Net code vs VB6.
    Last edited by LaVolpe; Oct 5th, 2009 at 03:53 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width