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
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
Comment