Fraction code completed Part 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Semajthewise
    New Member
    • Nov 2007
    • 38

    Fraction code completed Part 1

    Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve basic frational arithmatic. using only 3 textboxes. Allowing the user to enter any type of fraction and get a proper reduced fraction answer. Feel Free to use it if you like it. Had to do 2 posts sorry was too long I guess.

    Parameters:
    VB.Net code

    single form with 3 textboxes named:
    textbox1
    textbox2
    textbox3

    4 buttons named:
    Addfract
    Subfract
    Multifract
    DivFract

    (Clear button optional)

    and function using Euclid's algorithm (included in post)

    Uses Try/Catch to handle user imput errors
    [CODE=vb]
    ' Returns the greatest common divisor using Euclid's algorithm
    Private Function GCD(ByVal x As Integer, ByVal y As Integer) As Integer
    Dim temp As Integer

    x = Math.Abs(x)
    y = Math.Abs(y)

    Do While (y <> 0)
    temp = x Mod y
    x = y
    y = temp
    Loop

    Return x
    End Function
    [/CODE]
    [CODE=vb]
    Private Sub AddFract_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles AddFract.Click
    ' Declare my variables
    Dim numadd1 As Integer
    Dim numadd2 As Integer
    Dim denadd1 As Integer
    Dim denadd2 As Integer
    Dim slash1 As Integer
    Dim space1 As Integer
    Dim space2 As Integer
    Dim slash2 As Integer
    Dim wholenum1 As Integer
    Dim wholenum2 As Integer
    Dim numtemp1 As Integer
    Dim dentemp1 As Integer
    Dim wholenumtemp As Integer
    Try

    ' Gets a value of any spaces or slashs entered into either string
    ' And checks to see if there is a negitive #
    space1 = TextBox1.Text.I ndexOf(" ")
    slash1 = TextBox1.Text.I ndexOf("/")
    space2 = TextBox2.Text.I ndexOf(" ")
    slash2 = TextBox2.Text.I ndexOf("/")

    ' If there is only a fraction entered in box1
    If (space1 <= 0) Then
    numadd1 = TextBox1.Text.S ubstring(0, slash1)
    denadd1 = TextBox1.Text.S ubstring(slash1 + 1)

    ' If only a whole number is entered in box1
    ElseIf (space1 <= 0) And (slash1 <= 0) Then
    wholenum1 = TextBox1.Text
    numadd1 = (wholenum1 * 2)
    denadd1 = 2

    ' If both a whole number and a fraction are entered in box1
    Else
    wholenum1 = TextBox1.Text.S ubstring(0, space1)
    numadd1 = TextBox1.Text.S ubstring((space 1 + 1), (slash1 - (space1 + 1)))
    denadd1 = TextBox1.Text.S ubstring(slash1 + 1)
    If wholenum1 < 0 Then
    numadd1 = ((wholenum1 * denadd1) - numadd1)
    Else
    numadd1 = numadd1 + (wholenum1 * denadd1)
    End If

    End If

    ' If there is only a fraction entered in box2
    If (space2 <= 0) And (slash2 > 0) Then
    numadd2 = TextBox2.Text.S ubstring(0, slash2)
    denadd2 = TextBox2.Text.S ubstring(slash2 + 1)
    wholenum2 = 0

    ' If only a whole number is entered in box2
    ElseIf (space2 <= 0) And (slash2 <= 0) Then
    wholenum2 = TextBox2.Text
    numadd2 = (wholenum2 * 2)
    denadd2 = 2

    ' If both a whole number and a fraction are entered in box2
    Else
    wholenum2 = TextBox2.Text.S ubstring(0, space2)
    numadd2 = TextBox2.Text.S ubstring((space 2 + 1), (slash2 - (space2 + 1)))
    denadd2 = TextBox2.Text.S ubstring(slash2 + 1)
    If wholenum2 < 0 Then
    numadd2 = ((wholenum2 * denadd2) - numadd2)
    Else
    numadd2 = numadd2 + (wholenum2 * denadd2)
    End If


    End If


    'If denominators are different and denominator 1 can be divided into denominator 2 with_
    ' a result of 2 and with no remainder.
    ' This is needed to fix errors that occur in the math formula_
    ' "numtemp1 = ((numsub1 * densub2) - (numsub2 * densub1))"
    If denadd1 < denadd2 And denadd2 \ denadd1 = 2 And denadd2 Mod denadd1 = 0 Then

    'Creates the numerator of the answer
    numadd1 = (numadd1 * 2)
    denadd1 = (denadd1 * 2)
    numtemp1 = (numadd1 + numadd2)
    'Creates the denominator of the answer
    dentemp1 = (denadd2)

    ' Does the same as above except for when denominator 2 can be divided into denominator_
    ' 1 with a result of 2
    ElseIf denadd1 > denadd2 And denadd2 \ denadd1 = 2 And denadd1 Mod denadd2 = 0 Then

    'Creates the numerator of the answer
    numadd2 = (numadd2 * 2)
    denadd2 = (denadd2 * 2)
    numtemp1 = (numadd1 + numadd2)
    'Creates the denominator of the answer
    dentemp1 = (denadd1)

    ' This formula works for unevenly divisible denominators or when the numerator_
    ' can be evenly divided multiple times.
    ElseIf denadd1 <> denadd2 Then

    'Creates the numerator of the answer
    numtemp1 = ((numadd1 * denadd2) + (numadd2 * denadd1))
    'Creates the denominator of the answer
    dentemp1 = (denadd1 * denadd2)

    ' If denominators match run this
    Else
    ' Adds the 2 numerators to create an answer
    numtemp1 = (numadd1 + numadd2)
    ' Keeps the matching denominators
    dentemp1 = (denadd2)

    End If

    ' If the numerator is higher then the denominator_
    ' and not a negitive #
    If numtemp1 >= dentemp1 Then

    ' Setting variables
    Dim LoopCountadd As Integer
    Dim TimesLoopedadd As Integer
    TimesLoopedadd = 0
    LoopCountadd = 0
    Do While (Math.Abs(numte mp1)) >= dentemp1
    ' Subtracts the numerator from the denominator
    TimesLoopedadd = (dentemp1 - numtemp1)
    ' Counts up 1 for every time the numerator is subtracted from the denominator
    LoopCountadd += 1
    wholenumtemp = (LoopCountadd)
    ' Makes the remainder = the new numerator answer
    numtemp1 = (numtemp1 - dentemp1)
    Loop

    ElseIf (Math.Abs(numte mp1)) >= dentemp1 Then

    ' Setting variables
    Dim LoopCountadd As Integer
    Dim TimesLoopedadd As Integer
    TimesLoopedadd = 0
    LoopCountadd = 0

    ' A loop to subtract the denominator from the numerator until the numerator_
    ' is smaller than the denominator
    Do While (Math.Abs(numte mp1)) >= dentemp1
    ' Subtracts the numerator from the denominator
    TimesLoopedadd = ((Math.Abs(numt emp1)) - dentemp1)
    ' Counts up 1 for every time the numerator is subtracted from the denominator
    LoopCountadd -= 1
    ' Makes WholeNumber = the total of times the loop occurred
    wholenumtemp = (LoopCountadd)
    ' Makes the remainder = the new numerator answer
    numtemp1 = (Math.Abs(numte mp1) - dentemp1)

    ' Goes back to see if the numerator is higher than the denominator
    Loop

    End If

    'Reduces the fraction
    Dim reduce = GCD(numtemp1, dentemp1)
    numtemp1 = (numtemp1 / reduce)
    dentemp1 = (dentemp1 / reduce)

    Catch ex As Exception
    MessageBox.Show ("Please enter Fractions in the following formats only" _
    & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
    "#" & vbCr & "-#", "Error")
    TextBox1.Clear( )
    TextBox2.Clear( )
    TextBox3.Clear( )

    End Try
    ' These display the results
    ' If the anwser is a whole # only
    If numtemp1 = 0 Then
    TextBox3.Text = "" & (wholenumtemp)

    'If the answer is fractional only
    ElseIf (wholenumtemp) = Nothing Or (wholenumtemp = 0) Then
    TextBox3.Text = "" & (numtemp1) & "/" & (dentemp1)

    ' If both a whole # and a Fraction
    Else
    TextBox3.Text = "" & (wholenumtemp) & " " & (numtemp1) & "/" & (dentemp1)

    End If

    End Sub
    [/CODE]
    [CODE=vb]
    Private Sub SubFract_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles SubFract.Click
    ' Declare my variables
    Dim numsub1 As Integer
    Dim numsub2 As Integer
    Dim densub1 As Integer
    Dim densub2 As Integer
    Dim slash1 As Integer
    Dim space1 As Integer
    Dim space2 As Integer
    Dim slash2 As Integer
    Dim wholenum1 As Integer
    Dim wholenum2 As Integer
    Dim numtemp1 As Integer
    Dim dentemp1 As Integer
    Dim wholenumtemp As Integer
    Try
    ' Gets a value of any spaces or slashs entered into either string
    ' And checks to see if there is a negitive #
    space1 = TextBox1.Text.I ndexOf(" ")
    slash1 = TextBox1.Text.I ndexOf("/")
    space2 = TextBox2.Text.I ndexOf(" ")
    slash2 = TextBox2.Text.I ndexOf("/")

    ' If there is only a fraction entered in box1
    If (space1 <= 0) Then
    numsub1 = TextBox1.Text.S ubstring(0, slash1)
    densub1 = TextBox1.Text.S ubstring(slash1 + 1)

    ' If only a whole number is entered in box1
    ElseIf (space1 <= 0) And (slash1 <= 0) Then
    wholenum1 = TextBox1.Text
    numsub1 = (wholenum1 * 2)
    densub1 = 2

    ' If both a whole number and a fraction are entered in box1
    Else
    wholenum1 = TextBox1.Text.S ubstring(0, space1)
    numsub1 = TextBox1.Text.S ubstring((space 1 + 1), (slash1 - (space1 + 1)))
    densub1 = TextBox1.Text.S ubstring(slash1 + 1)
    If wholenum1 < 0 Then
    numsub1 = ((wholenum1 * densub1) - numsub1)
    Else
    numsub1 = (wholenum1 * densub1) + numsub1
    End If

    End If

    ' If there is only a fraction entered in box2
    If (space2 <= 0) And (slash2 > 0) Then
    numsub2 = TextBox2.Text.S ubstring(0, slash2)
    densub2 = TextBox2.Text.S ubstring(slash2 + 1)
    wholenum2 = 0

    ' If only a whole number is entered in box2
    ElseIf (space2 <= 0) And (slash2 <= 0) Then
    wholenum2 = TextBox2.Text
    numsub2 = (wholenum2 * 2)
    densub2 = 2

    ' If both a whole number and a fraction are entered in box2
    Else
    wholenum2 = TextBox2.Text.S ubstring(0, space2)
    numsub2 = TextBox2.Text.S ubstring((space 2 + 1), (slash2 - (space2 + 1)))
    densub2 = TextBox2.Text.S ubstring(slash2 + 1)
    If wholenum2 < 0 Then
    numsub2 = ((wholenum2 * densub2) - numsub2)
    Else
    numsub2 = (wholenum2 * densub2) + numsub2
    End If


    End If


    'If denominators are different and denominator 1 can be divided into denominator 2 with_
    ' a result of 2 and with no remainder.
    ' This is needed to fix errors that occur in the math formula_
    ' "numtemp1 = ((numsub1 * densub2) - (numsub2 * densub1))"
    If densub1 < densub2 And densub2 \ densub1 = 2 And densub2 Mod densub1 = 0 Then

    'Creates the numerator of the answer
    numsub1 = (numsub1 * 2)
    densub1 = (densub1 * 2)
    numtemp1 = (numsub1 - numsub2)
    'Creates the denominator of the answer
    dentemp1 = (densub2)

    ' Does the same as above except for when denominator 2 can be divided into denominator_
    ' 1 with a result of 2
    ElseIf densub1 > densub2 And densub2 \ densub1 = 2 And densub1 Mod densub2 = 0 Then

    'Creates the numerator of the answer
    numsub2 = (numsub2 * 2)
    densub2 = (densub2 * 2)
    numtemp1 = (numsub1 - numsub2)
    'Creates the denominator of the answer
    dentemp1 = (densub1)

    ' This formula works for unevenly divisible denominators or when the numerator_
    ' can be evenly divided multiple times.
    ElseIf densub1 <> densub2 Then

    'Creates the numerator of the answer
    numtemp1 = ((numsub1 * densub2) - (numsub2 * densub1))
    'Creates the denominator of the answer
    dentemp1 = (densub1 * densub2)

    ' If denominators match run this
    Else
    ' Adds the 2 numerators to create an answer
    numtemp1 = (numsub1 - numsub2)
    ' Keeps the matching denominators
    dentemp1 = (densub2)

    End If

    ' If the numerator is higher then the denominator_
    ' and not a negitive #
    If numtemp1 >= dentemp1 Then

    ' Setting variables
    Dim LoopCountadd As Integer
    Dim TimesLoopedadd As Integer
    TimesLoopedadd = 0
    LoopCountadd = 0
    Do While (Math.Abs(numte mp1)) >= dentemp1
    ' Subtracts the numerator from the denominator
    TimesLoopedadd = (dentemp1 - numtemp1)
    ' Counts up 1 for every time the numerator is subtracted from the denominator
    LoopCountadd += 1
    wholenumtemp = (LoopCountadd)
    ' Makes the remainder = the new numerator answer
    numtemp1 = (numtemp1 - dentemp1)
    Loop

    ElseIf (Math.Abs(numte mp1)) >= dentemp1 Then

    ' Setting variables
    Dim LoopCountadd As Integer
    Dim TimesLoopedadd As Integer
    TimesLoopedadd = 0
    LoopCountadd = 0

    ' A loop to subtract the denominator from the numerator until the numerator_
    ' is smaller than the denominator
    Do While (Math.Abs(numte mp1)) >= dentemp1
    ' Subtracts the numerator from the denominator
    TimesLoopedadd = ((Math.Abs(numt emp1)) - dentemp1)
    ' Counts up 1 for every time the numerator is subtracted from the denominator
    LoopCountadd -= 1
    ' Makes WholeNumber = the total of times the loop occurred
    wholenumtemp = (LoopCountadd)
    ' Makes the remainder = the new numerator answer
    numtemp1 = (Math.Abs(numte mp1) - dentemp1)

    ' Goes back to see if the numerator is higher than the denominator
    Loop

    End If

    'Reduces the fraction
    Dim reduce = GCD(numtemp1, dentemp1)
    numtemp1 = (numtemp1 / reduce)
    dentemp1 = (dentemp1 / reduce)

    Catch ex As Exception
    MessageBox.Show ("Please enter Fractions in the following formats only" _
    & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
    "#" & vbCr & "-#", "Error")
    TextBox1.Clear( )
    TextBox2.Clear( )
    TextBox3.Clear( )

    End Try
    ' These display the results
    ' If the anwser is a whole # only
    If numtemp1 = 0 Then
    TextBox3.Text = "" & (wholenumtemp)

    'If the answer is fractional only
    ElseIf (wholenumtemp) = Nothing Or (wholenumtemp = 0) Then
    TextBox3.Text = "" & (numtemp1) & "/" & (dentemp1)

    ' If both a whole # and a Fraction
    Else
    TextBox3.Text = "" & (wholenumtemp) & " " & (numtemp1) & "/" & (dentemp1)

    End If

    End Sub
    [/CODE]


    Hope you find it usefull,
    James
    Last edited by RedSon; Jan 10 '08, 10:00 PM. Reason: Removing CODE tags 0o!
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Adding CODE tags makes this post disappear.

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      I'm an old DOS programmer, and as I've stated before, don't know diddly about programming websites, but I just had to fiddle with this thing! What I found out was that, indeed, if you hilite all the code and apply code tags to it, the post disappears. If, however, you hilite one sub/function at a time and apply code tags to it, everything worked just fine!

      Linq ;0)>

      BTW: Where I come from, Dim statements are supposed to be at the beginning of a sub/function, not interspersed throughout. If this is going to be an article, we really should stick to best practises, shouldn't we?

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by missinglinq
        BTW: Where I come from, Dim statements are supposed to be at the beginning of a sub/function, not interspersed throughout. If this is going to be an article, we really should stick to best practises, shouldn't we?
        If you say so, I've never written a line of VB code in my life.

        Comment

        • Semajthewise
          New Member
          • Nov 2007
          • 38

          #5
          Ok guys thanks well heres the rest of the code.

          [code=vbnet]

          Private Sub MultiFract_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MultiFract.Clic k

          ' Declare my variables
          Dim nummulti1 As Integer
          Dim nummulti2 As Integer
          Dim denmulti1 As Integer
          Dim denmulti2 As Integer
          Dim slash1 As Integer
          Dim space1 As Integer
          Dim space2 As Integer
          Dim slash2 As Integer
          Dim wholenum1 As Integer
          Dim wholenum2 As Integer
          Dim numtemp1 As Integer
          Dim dentemp1 As Integer
          Dim wholenumtemp As Integer
          Try
          ' Gets a value of any spaces or slashs entered into either string
          ' And checks to see if there is a negitive #
          space1 = TextBox1.Text.I ndexOf(" ")
          slash1 = TextBox1.Text.I ndexOf("/")
          space2 = TextBox2.Text.I ndexOf(" ")
          slash2 = TextBox2.Text.I ndexOf("/")

          ' If there is only a fraction entered in box1
          If (space1 <= 0) Then
          nummulti1 = TextBox1.Text.S ubstring(0, slash1)
          denmulti1 = TextBox1.Text.S ubstring(slash1 + 1)

          ' If only a whole number is entered in box1
          ElseIf (space1 <= 0) And (slash1 <= 0) Then
          wholenum1 = TextBox1.Text
          nummulti1 = (wholenum1 * 2)
          denmulti1 = 2

          ' If both a whole number and a fraction are entered in box1
          Else
          wholenum1 = TextBox1.Text.S ubstring(0, space1)
          nummulti1 = TextBox1.Text.S ubstring((space 1 + 1), (slash1 - (space1 + 1)))
          denmulti1 = TextBox1.Text.S ubstring(slash1 + 1)
          If wholenum1 < 0 Then
          nummulti1 = ((wholenum1 * denmulti1) - nummulti1)
          Else
          nummulti1 = (wholenum1 * denmulti1) + nummulti1
          End If

          End If

          ' If there is only a fraction entered in box2
          If (space2 <= 0) And (slash2 > 0) Then
          nummulti2 = TextBox2.Text.S ubstring(0, slash2)
          denmulti2 = TextBox2.Text.S ubstring(slash2 + 1)
          wholenum2 = 0

          ' If only a whole number is entered in box2
          ElseIf (space2 <= 0) And (slash2 <= 0) Then
          wholenum2 = TextBox2.Text
          nummulti2 = (wholenum2 * 2)
          denmulti2 = 2

          ' If both a whole number and a fraction are entered in box2
          Else
          wholenum2 = TextBox2.Text.S ubstring(0, space2)
          nummulti2 = TextBox2.Text.S ubstring((space 2 + 1), (slash2 - (space2 + 1)))
          denmulti2 = TextBox2.Text.S ubstring(slash2 + 1)
          If wholenum2 < 0 Then
          nummulti2 = ((wholenum2 * denmulti2) - nummulti2)
          Else
          nummulti2 = (wholenum2 * denmulti2) + nummulti2
          End If


          End If

          ' Multiplies the 2 numerators
          numtemp1 = (nummulti1 * nummulti2)
          ' Multiplies the denominators
          dentemp1 = (denmulti2 * denmulti1)

          ' If the numerator is higher then the denominator_
          ' and not a negitive #
          If numtemp1 >= dentemp1 Then

          ' Setting variables
          Dim LoopCountadd As Integer
          Dim TimesLoopedadd As Integer
          TimesLoopedadd = 0
          LoopCountadd = 0
          Do While (Math.Abs(numte mp1)) >= dentemp1
          ' Subtracts the numerator from the denominator
          TimesLoopedadd = (dentemp1 - numtemp1)
          ' Counts up 1 for every time the numerator is subtracted from the denominator
          LoopCountadd += 1
          wholenumtemp = (LoopCountadd)
          ' Makes the remainder = the new numerator answer
          numtemp1 = (numtemp1 - dentemp1)
          Loop
          ' Otherwise this loop is used
          ElseIf (Math.Abs(numte mp1)) >= dentemp1 Then

          ' Setting variables
          Dim LoopCountadd As Integer
          Dim TimesLoopedadd As Integer
          TimesLoopedadd = 0
          LoopCountadd = 0

          ' A loop to subtract the denominator from the numerator until the numerator_
          ' is smaller than the denominator
          Do While (Math.Abs(numte mp1)) >= dentemp1
          ' Subtracts the numerator from the denominator
          TimesLoopedadd = ((Math.Abs(numt emp1)) - dentemp1)
          ' Counts up 1 for every time the numerator is subtracted from the denominator
          LoopCountadd -= 1
          ' Makes WholeNumber = the total of times the loop occurred
          wholenumtemp = (LoopCountadd)
          ' Makes the remainder = the new numerator answer
          numtemp1 = (Math.Abs(numte mp1) - dentemp1)

          ' Goes back to see if the numerator is higher than the denominator
          Loop

          End If

          'Reduces the fraction
          Dim reduce = GCD(numtemp1, dentemp1)
          numtemp1 = (numtemp1 / reduce)
          dentemp1 = (dentemp1 / reduce)

          Catch ex As Exception
          MessageBox.Show ("Please enter Fractions in the following formats only" _
          & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
          "#" & vbCr & "-#", "Error")
          TextBox1.Clear( )
          TextBox2.Clear( )
          TextBox3.Clear( )

          End Try
          ' These display the results
          ' If the anwser is a whole # only
          If numtemp1 = 0 Then
          TextBox3.Text = "" & (wholenumtemp)

          'If the answer is fractional only
          ElseIf (wholenumtemp) = Nothing Or (wholenumtemp = 0) Then
          TextBox3.Text = "" & (numtemp1) & "/" & (dentemp1)

          ' If both a whole # and a Fraction
          Else
          TextBox3.Text = "" & (wholenumtemp) & " " & (numtemp1) & "/" & (dentemp1)

          End If
          End Sub
          [/code]
          [code=vbnet]
          Private Sub DivFract_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles DivFract.Click

          ' Declare my variables
          Dim numdivide1 As Integer
          Dim numdivide2 As Integer
          Dim dendivide1 As Integer
          Dim dendivide2 As Integer
          Dim slash1 As Integer
          Dim space1 As Integer
          Dim space2 As Integer
          Dim slash2 As Integer
          Dim wholenum1 As Integer
          Dim wholenum2 As Integer
          Dim numtemp1 As Integer
          Dim dentemp1 As Integer
          Dim wholenumtemp As Integer
          Try
          ' Gets a value of any spaces or slashs entered into either string
          ' And checks to see if there is a negitive #
          space1 = TextBox1.Text.I ndexOf(" ")
          slash1 = TextBox1.Text.I ndexOf("/")

          space2 = TextBox2.Text.I ndexOf(" ")
          slash2 = TextBox2.Text.I ndexOf("/")


          ' If there is only a fraction entered in box1
          If (space1 <= 0) Then
          numdivide1 = TextBox1.Text.S ubstring(0, slash1)
          dendivide1 = TextBox1.Text.S ubstring(slash1 + 1)

          ' If only a whole number is entered in box1
          ElseIf (space1 <= 0) And (slash1 <= 0) Then
          wholenum1 = TextBox1.Text
          numdivide1 = (wholenum1 * 2)
          dendivide1 = 2

          ' If both a whole number and a fraction are entered in box1
          Else
          wholenum1 = TextBox1.Text.S ubstring(0, space1)
          numdivide1 = TextBox1.Text.S ubstring((space 1 + 1), (slash1 - (space1 + 1)))
          dendivide1 = TextBox1.Text.S ubstring(slash1 + 1)
          If wholenum1 < 0 Then
          numdivide1 = ((wholenum1 * dendivide1) - numdivide1)
          Else
          numdivide1 = (wholenum1 * dendivide1) + numdivide1
          End If

          End If

          ' If there is only a fraction entered in box2
          If (space2 <= 0) And (slash2 > 0) Then
          numdivide2 = TextBox2.Text.S ubstring(0, slash2)
          dendivide2 = TextBox2.Text.S ubstring(slash2 + 1)
          wholenum2 = 0

          ' If only a whole number is entered in box2
          ElseIf (space2 <= 0) And (slash2 <= 0) Then
          wholenum2 = TextBox2.Text
          numdivide2 = (wholenum2 * 2)
          dendivide2 = 2

          ' If both a whole number and a fraction are entered in box2
          Else
          wholenum2 = TextBox2.Text.S ubstring(0, space2)
          numdivide2 = TextBox2.Text.S ubstring((space 2 + 1), (slash2 - (space2 + 1)))
          dendivide2 = TextBox2.Text.S ubstring(slash2 + 1)
          If wholenum2 < 0 Then
          numdivide2 = ((wholenum2 * dendivide2) - numdivide2)
          Else
          numdivide2 = (wholenum2 * dendivide2) + numdivide2
          End If


          End If

          ' Multiplies the 2 numerators
          numtemp1 = (numdivide1 * dendivide2)
          ' Multiplies the denominators
          dentemp1 = (dendivide1 * numdivide2)

          ' If the numerator is higher then the denominator_
          ' and not a negitive #
          If numtemp1 >= dentemp1 And dentemp1 > 0 Then

          ' Setting variables
          Dim LoopCountadd As Integer
          Dim TimesLoopedadd As Integer
          TimesLoopedadd = 0
          LoopCountadd = 0
          Do While (Math.Abs(numte mp1)) >= dentemp1
          ' Subtracts the numerator from the denominator
          TimesLoopedadd = (dentemp1 - numtemp1)
          ' Counts up 1 for every time the numerator is subtracted from the denominator
          LoopCountadd += 1
          wholenumtemp = (LoopCountadd)
          ' Makes the remainder = the new numerator answer
          numtemp1 = (numtemp1 - dentemp1)
          Loop
          ' Otherwise this loop is used
          ElseIf (Math.Abs(numte mp1)) >= dentemp1 Then

          ' Setting variables
          Dim LoopCountadd As Integer
          Dim TimesLoopedadd As Integer
          TimesLoopedadd = 0
          LoopCountadd = 0
          dentemp1 = (Math.Abs(dente mp1))
          ' A loop to subtract the denominator from the numerator until the numerator_
          ' is smaller than the denominator
          Do While (Math.Abs(numte mp1)) >= (dentemp1)
          ' Subtracts the numerator from the denominator
          TimesLoopedadd = ((Math.Abs(numt emp1)) - dentemp1)
          ' Counts up 1 for every time the numerator is subtracted from the denominator
          LoopCountadd -= 1
          ' Makes WholeNumber = the total of times the loop occurred
          wholenumtemp = (LoopCountadd)
          ' Makes the remainder = the new numerator answer
          numtemp1 = (Math.Abs(numte mp1) - dentemp1)

          ' Goes back to see if the numerator is higher than the denominator
          Loop

          End If

          'Reduces the fraction
          Dim reduce = GCD(numtemp1, dentemp1)
          numtemp1 = (numtemp1 / reduce)
          dentemp1 = (dentemp1 / reduce)

          Catch ex As Exception
          MessageBox.Show ("Please enter Fractions in the following formats only" _
          & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
          "#" & vbCr & "-#", "Error")
          TextBox1.Clear( )
          TextBox2.Clear( )
          TextBox3.Clear( )

          End Try
          ' These display the results
          ' If the anwser is a whole # only
          If numtemp1 = 0 Then
          TextBox3.Text = "" & (wholenumtemp)

          'If the answer is fractional only
          ElseIf (wholenumtemp) = Nothing Or (wholenumtemp = 0) Then
          TextBox3.Text = "" & (numtemp1) & "/" & (dentemp1)

          ' If both a whole # and a Fraction
          Else
          TextBox3.Text = "" & (wholenumtemp) & " " & (numtemp1) & "/" & (dentemp1)

          End If

          End Sub
          [/code]


          The moderators made a good point about the dims So I will try to clean up and shorten the code and post it in part 2
          Last edited by Semajthewise; Jan 11 '08, 05:30 PM. Reason: Commenting

          Comment

          Working...