Here it is cleaned up a little more.
Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two textboxes. These can be entered as a whole number and a proper fraction, whole number and an improper fraction, just a proper/inproper fraction, or just a whole number.
Form build Requirements:
using Microsoft Visual Studio.NET 2003
single form with...
3 textboxes named;
textbox1
textbox2
textbox3
4 buttons maned;
Addfract
Subfract
Multifract
Divfract
Place this code in the class area. This allows them to be used by all of the subs.
[code=vbnet]
' Declare my variables
Dim Numerator1 As Integer
Dim Numerator2 As Integer
Dim Denominator1 As Integer
Dim Denominator2 As Integer
Dim Wholenumber1 As Integer
Dim Wholenumber2 As Integer
Dim NumeratorTemp1 As Integer
Dim DenominatorTemp 1 As Integer
Dim WholenumberTemp As Integer
' Gets a value of any spaces or slashs entered into either string
Dim Slash1 As Integer = TextBox1.Text.I ndexOf("/")
Dim Space1 As Integer = TextBox1.Text.I ndexOf(" ")
Dim Space2 As Integer = TextBox1.Text.I ndexOf(" ")
Dim Slash2 As Integer = TextBox1.Text.I ndexOf("/")
[/code]
Creat a function to get the greatest common divisor with this code
[code=vbnet]
' 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]
This code will run when the Addfract button is pressed
[code=vbnet]
Private Sub AddFract_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles AddFract.Click
' Attempts to run the code below
Try
' If there is only a fraction entered in box1
If (Space1 <= 0) Then
Numerator1 = TextBox1.Text.S ubstring(0, Slash1)
Denominator1 = TextBox1.Text.S ubstring(Slash1 + 1)
' If only a whole number is entered in box1
ElseIf (Space1 <= 0) And (Slash1 <= 0) Then
Wholenumber1 = TextBox1.Text
Numerator1 = (Wholenumber1 * 2)
Denominator1 = 2
' If both a whole number and a fraction are entered in box1
Else
Wholenumber1 = TextBox1.Text.S ubstring(0, Space1)
Numerator1 = TextBox1.Text.S ubstring((Space 1 + 1), (Slash1 - (Space1 + 1)))
Denominator1 = TextBox1.Text.S ubstring(Slash1 + 1)
If Wholenumber1 < 0 Then
Numerator1 = ((Wholenumber1 * Denominator1) - Numerator1)
Else
Numerator1 = Numerator1 + (Wholenumber1 * Denominator1)
End If
End If
' If there is only a fraction entered in box2
If (Space2 <= 0) And (Slash2 > 0) Then
Numerator2 = TextBox2.Text.S ubstring(0, Slash2)
Denominator2 = TextBox2.Text.S ubstring(Slash2 + 1)
Wholenumber2 = 0
' If only a whole number is entered in box2
ElseIf (Space2 <= 0) And (Slash2 <= 0) Then
Wholenumber2 = TextBox2.Text
Numerator2 = (Wholenumber2 * 2)
Denominator2 = 2
' If both a whole number and a fraction are entered in box2
Else
Wholenumber2 = TextBox2.Text.S ubstring(0, Space2)
Numerator2 = TextBox2.Text.S ubstring((Space 2 + 1), (Slash2 - (Space2 + 1)))
Denominator2 = TextBox2.Text.S ubstring(Slash2 + 1)
If Wholenumber2 < 0 Then
Numerator2 = ((Wholenumber2 * Denominator2) - Numerator2)
Else
Numerator2 = Numerator2 + (Wholenumber2 * Denominator2)
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 Denominator1 < Denominator2 And Denominator2 \ _
Denominator1 = 2 And Denominator2 Mod Denominator1 = 0 Then
'Creates the numerator of the answer
Numerator1 = (Numerator1 * 2)
Denominator1 = (Denominator1 * 2)
numeratortemp1 = (Numerator1 + Numerator2)
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator2)
' Does the same as above except for when denominator 2 can be divided into denominator_
' 1 with a result of 2
ElseIf Denominator1 > Denominator2 And Denominator2 \ Denominator1 = 2 And Denominator1 Mod Denominator2 = 0 Then
'Creates the numerator of the answer
Numerator2 = (Numerator2 * 2)
Denominator2 = (Denominator2 * 2)
NumeratorTemp1 = (Numerator1 + Numerator2)
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator1)
' This formula works for unevenly divisible denominators or when the numerator_
' can be evenly divided multiple times.
ElseIf Denominator1 <> Denominator2 Then
'Creates the numerator of the answer
NumeratorTemp1 = ((Numerator1 * Denominator2) + (Numerator2 * Denominator1))
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator1 * Denominator2)
' If denominators match run this
Else
' Adds the 2 numerators to create an answer
NumeratorTemp1 = (Numerator1 + Numerator2)
' Keeps the matching denominators
DenominatorTemp 1 = (Denominator2)
End If
' If the numerator is higher then the denominator_
' and not a negitive #
If NumeratorTemp1 >= DenominatorTemp 1 Then
' Setting variables
Dim LoopCountadd As Integer
Dim TimesLoopedadd As Integer
TimesLoopedadd = 0
LoopCountadd = 0
Do While (Math.Abs(Numer atorTemp1)) >= DenominatorTemp 1
' Subtracts the numerator from the denominator
TimesLoopedadd = (DenominatorTem p1 - NumeratorTemp1)
' Counts up 1 for every time the numerator is subtracted from the denominator
LoopCountadd += 1
WholenumberTemp = (LoopCountadd)
' Makes the remainder = the new numerator answer
NumeratorTemp1 = (NumeratorTemp1 - DenominatorTemp 1)
Loop
ElseIf (Math.Abs(Numer atorTemp1)) >= DenominatorTemp 1 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(Numer atorTemp1)) >= DenominatorTemp 1
' Subtracts the numerator from the denominator
TimesLoopedadd = ((Math.Abs(Nume ratorTemp1)) - DenominatorTemp 1)
' Counts up 1 for every time the numerator is subtracted from the denominator
LoopCountadd -= 1
' Makes WholeNumber = the total of times the loop occurred
WholenumberTemp = (LoopCountadd)
' Makes the remainder = the new numerator answer
NumeratorTemp1 = (Math.Abs(Numer atorTemp1) - DenominatorTemp 1)
' Goes back to see if the numerator is higher than the denominator
Loop
End If
'Reduces the fraction
Dim reduce = GCD(NumeratorTe mp1, DenominatorTemp 1)
NumeratorTemp1 = (NumeratorTemp1 / reduce)
DenominatorTemp 1 = (DenominatorTem p1 / reduce)
' If something goes wrong then display a message box informing the user to re-enter
' Thier fractions in a proper format. Clear all textboxes and focus on textbox1
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( )
TextBox1.Focus( )
End Try
' These display the results
' If the anwser is a whole # only
If NumeratorTemp1 = 0 Then
TextBox3.Text = "" & (WholenumberTem p)
'If the answer is fractional only
ElseIf (WholenumberTem p) = Nothing Or (WholenumberTem p = 0) Then
TextBox3.Text = "" & (NumeratorTemp1 ) & "/" & (DenominatorTem p1)
' If both a whole # and a Fraction
Else
TextBox3.Text = "" & (WholenumberTem p) & " " & (NumeratorTemp1 ) & "/" & (DenominatorTem p1)
End If
End Sub
[/code]
This one is for SubFract Button
[code=vbnet]
Private Sub SubFract_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles SubFract.Click
' Attempt to run the following code
Try
' If there is only a fraction entered in box1
If (space1 <= 0) Then
Numerator1 = TextBox1.Text.S ubstring(0, Slash1)
Denominator1 = TextBox1.Text.S ubstring(Slash1 + 1)
' If only a whole number is entered in box1
ElseIf (space1 <= 0) And (slash1 <= 0) Then
Wholenumber1 = TextBox1.Text
Numerator1 = (Wholenumber1 * 2)
Denominator1 = 2
' If both a whole number and a fraction are entered in box1
Else
Wholenumber1 = TextBox1.Text.S ubstring(0, Space1)
Numerator1 = TextBox1.Text.S ubstring((Space 1 + 1), (Slash1 - (Space1 + 1)))
Denominator1 = TextBox1.Text.S ubstring(Slash1 + 1)
If Wholenumber1 < 0 Then
Numerator1 = ((Wholenumber1 * Denominator1) - Numerator1)
Else
Numerator1 = (Wholenumber1 * Denominator1) + Numerator1
End If
End If
' If there is only a fraction entered in box2
If (space2 <= 0) And (slash2 > 0) Then
Numerator2 = TextBox2.Text.S ubstring(0, Slash2)
Denominator2 = TextBox2.Text.S ubstring(Slash2 + 1)
Wholenumber2 = 0
' If only a whole number is entered in box2
ElseIf (space2 <= 0) And (slash2 <= 0) Then
Wholenumber2 = TextBox2.Text
Numerator2 = (Wholenumber2 * 2)
Denominator2 = 2
' If both a whole number and a fraction are entered in box2
Else
Wholenumber2 = TextBox2.Text.S ubstring(0, Space2)
Numerator2 = TextBox2.Text.S ubstring((Space 2 + 1), (Slash2 - (Space2 + 1)))
Denominator2 = TextBox2.Text.S ubstring(Slash2 + 1)
If Wholenumber2 < 0 Then
Numerator2 = ((Wholenumber2 * Denominator2) - Numerator2)
Else
Numerator2 = (Wholenumber2 * Denominator2) + Numerator2
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 Denominator1 < Denominator2 And Denominator2 \ Denominator1 = 2 _
And Denominator2 Mod Denominator1 = 0 Then
'Creates the numerator of the answer
Numerator1 = (Numerator1 * 2)
Denominator1 = (Denominator1 * 2)
NumeratorTemp1 = (Numerator1 - Numerator2)
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator2)
' Does the same as above except for when denominator 2 can be divided into denominator_
' 1 with a result of 2
ElseIf Denominator1 > Denominator2 And Denominator2 \ Denominator1 = 2 _
And Denominator1 Mod Denominator2 = 0 Then
'Creates the numerator of the answer
Numerator2 = (Numerator2 * 2)
Denominator2 = (Denominator2 * 2)
NumeratorTemp1 = (Numerator1 - Numerator2)
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator1)
' This formula works for unevenly divisible denominators or when the numerator_
' can be evenly divided multiple times.
ElseIf Denominator1 <> Denominator2 Then
'Creates the numerator of the answer
NumeratorTemp1 = ((Numerator1 * Denominator2) - (Numerator2 * Denominator1))
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator1 * Denominator2)
' If denominators match run this
Else
' Adds the 2 numerators to create an answer
NumeratorTemp1 = (Numerator1 - Numerator2)
' Keeps the matching denominators
DenominatorTemp 1 = (Denominator2)
End If
' If the numerator is higher then the denominator_
' and not a negitive #
If NumeratorTemp1 >= DenominatorTemp 1 Then
' Setting variables
Dim LoopCountadd As Integer
Dim TimesLoopedadd As Integer
TimesLoopedadd = 0
LoopCountadd = 0
Do While (Math.Abs(Numer atorTemp1)) >= DenominatorTemp 1
' Subtracts the numerator from the denominator
TimesLoopedadd = (DenominatorTem p1 - NumeratorTemp1)
' Counts up 1 for every time the numerator is subtracted from the denominator
LoopCountadd += 1
WholenumberTemp = (LoopCountadd)
' Makes the remainder = the new numerator answer
NumeratorTemp1 = (NumeratorTemp1 - DenominatorTemp 1)
Loop
ElseIf (Math.Abs(Numer atorTemp1)) >= DenominatorTemp 1 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(Numer atorTemp1)) >= DenominatorTemp 1
' Subtracts the numerator from the denominator
TimesLoopedadd = ((Math.Abs(Nume ratorTemp1)) - DenominatorTemp 1)
' Counts up 1 for every time the numerator is subtracted from the denominator
LoopCountadd -= 1
' Makes WholeNumber = the total of times the loop occurred
WholenumberTemp = (LoopCountadd)
' Makes the remainder = the new numerator answer
NumeratorTemp1 = (Math.Abs(Numer atorTemp1) - DenominatorTemp 1)
' Goes back to see if the numerator is higher than the denominator
Loop
End If
'Reduces the fraction
Dim reduce = GCD(NumeratorTe mp1, DenominatorTemp 1)
NumeratorTemp1 = (NumeratorTemp1 / reduce)
DenominatorTemp 1 = (DenominatorTem p1 / reduce)
' If something goes wrong then display a message box informing the user to re-enter
' Thier fractions in a proper format. Clear all textboxes and focus on textbox1
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( )
TextBox1.focus( )
End Try
' These display the results
' If the anwser is a whole # only
If NumeratorTemp1 = 0 Then
TextBox3.Text = "" & (WholenumberTem p)
'If the answer is fractional only
ElseIf (WholenumberTem p) = Nothing Or (WholenumberTem p = 0) Then
TextBox3.Text = "" & (NumeratorTemp1 ) & "/" & (DenominatorTem p1)
' If both a whole # and a Fraction
Else
TextBox3.Text = "" & (WholenumberTem p) & " " & (NumeratorTemp1 ) & "/" & (DenominatorTem p1)
End If
End Sub
[/code]
Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two textboxes. These can be entered as a whole number and a proper fraction, whole number and an improper fraction, just a proper/inproper fraction, or just a whole number.
Form build Requirements:
using Microsoft Visual Studio.NET 2003
single form with...
3 textboxes named;
textbox1
textbox2
textbox3
4 buttons maned;
Addfract
Subfract
Multifract
Divfract
Place this code in the class area. This allows them to be used by all of the subs.
[code=vbnet]
' Declare my variables
Dim Numerator1 As Integer
Dim Numerator2 As Integer
Dim Denominator1 As Integer
Dim Denominator2 As Integer
Dim Wholenumber1 As Integer
Dim Wholenumber2 As Integer
Dim NumeratorTemp1 As Integer
Dim DenominatorTemp 1 As Integer
Dim WholenumberTemp As Integer
' Gets a value of any spaces or slashs entered into either string
Dim Slash1 As Integer = TextBox1.Text.I ndexOf("/")
Dim Space1 As Integer = TextBox1.Text.I ndexOf(" ")
Dim Space2 As Integer = TextBox1.Text.I ndexOf(" ")
Dim Slash2 As Integer = TextBox1.Text.I ndexOf("/")
[/code]
Creat a function to get the greatest common divisor with this code
[code=vbnet]
' 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]
This code will run when the Addfract button is pressed
[code=vbnet]
Private Sub AddFract_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles AddFract.Click
' Attempts to run the code below
Try
' If there is only a fraction entered in box1
If (Space1 <= 0) Then
Numerator1 = TextBox1.Text.S ubstring(0, Slash1)
Denominator1 = TextBox1.Text.S ubstring(Slash1 + 1)
' If only a whole number is entered in box1
ElseIf (Space1 <= 0) And (Slash1 <= 0) Then
Wholenumber1 = TextBox1.Text
Numerator1 = (Wholenumber1 * 2)
Denominator1 = 2
' If both a whole number and a fraction are entered in box1
Else
Wholenumber1 = TextBox1.Text.S ubstring(0, Space1)
Numerator1 = TextBox1.Text.S ubstring((Space 1 + 1), (Slash1 - (Space1 + 1)))
Denominator1 = TextBox1.Text.S ubstring(Slash1 + 1)
If Wholenumber1 < 0 Then
Numerator1 = ((Wholenumber1 * Denominator1) - Numerator1)
Else
Numerator1 = Numerator1 + (Wholenumber1 * Denominator1)
End If
End If
' If there is only a fraction entered in box2
If (Space2 <= 0) And (Slash2 > 0) Then
Numerator2 = TextBox2.Text.S ubstring(0, Slash2)
Denominator2 = TextBox2.Text.S ubstring(Slash2 + 1)
Wholenumber2 = 0
' If only a whole number is entered in box2
ElseIf (Space2 <= 0) And (Slash2 <= 0) Then
Wholenumber2 = TextBox2.Text
Numerator2 = (Wholenumber2 * 2)
Denominator2 = 2
' If both a whole number and a fraction are entered in box2
Else
Wholenumber2 = TextBox2.Text.S ubstring(0, Space2)
Numerator2 = TextBox2.Text.S ubstring((Space 2 + 1), (Slash2 - (Space2 + 1)))
Denominator2 = TextBox2.Text.S ubstring(Slash2 + 1)
If Wholenumber2 < 0 Then
Numerator2 = ((Wholenumber2 * Denominator2) - Numerator2)
Else
Numerator2 = Numerator2 + (Wholenumber2 * Denominator2)
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 Denominator1 < Denominator2 And Denominator2 \ _
Denominator1 = 2 And Denominator2 Mod Denominator1 = 0 Then
'Creates the numerator of the answer
Numerator1 = (Numerator1 * 2)
Denominator1 = (Denominator1 * 2)
numeratortemp1 = (Numerator1 + Numerator2)
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator2)
' Does the same as above except for when denominator 2 can be divided into denominator_
' 1 with a result of 2
ElseIf Denominator1 > Denominator2 And Denominator2 \ Denominator1 = 2 And Denominator1 Mod Denominator2 = 0 Then
'Creates the numerator of the answer
Numerator2 = (Numerator2 * 2)
Denominator2 = (Denominator2 * 2)
NumeratorTemp1 = (Numerator1 + Numerator2)
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator1)
' This formula works for unevenly divisible denominators or when the numerator_
' can be evenly divided multiple times.
ElseIf Denominator1 <> Denominator2 Then
'Creates the numerator of the answer
NumeratorTemp1 = ((Numerator1 * Denominator2) + (Numerator2 * Denominator1))
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator1 * Denominator2)
' If denominators match run this
Else
' Adds the 2 numerators to create an answer
NumeratorTemp1 = (Numerator1 + Numerator2)
' Keeps the matching denominators
DenominatorTemp 1 = (Denominator2)
End If
' If the numerator is higher then the denominator_
' and not a negitive #
If NumeratorTemp1 >= DenominatorTemp 1 Then
' Setting variables
Dim LoopCountadd As Integer
Dim TimesLoopedadd As Integer
TimesLoopedadd = 0
LoopCountadd = 0
Do While (Math.Abs(Numer atorTemp1)) >= DenominatorTemp 1
' Subtracts the numerator from the denominator
TimesLoopedadd = (DenominatorTem p1 - NumeratorTemp1)
' Counts up 1 for every time the numerator is subtracted from the denominator
LoopCountadd += 1
WholenumberTemp = (LoopCountadd)
' Makes the remainder = the new numerator answer
NumeratorTemp1 = (NumeratorTemp1 - DenominatorTemp 1)
Loop
ElseIf (Math.Abs(Numer atorTemp1)) >= DenominatorTemp 1 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(Numer atorTemp1)) >= DenominatorTemp 1
' Subtracts the numerator from the denominator
TimesLoopedadd = ((Math.Abs(Nume ratorTemp1)) - DenominatorTemp 1)
' Counts up 1 for every time the numerator is subtracted from the denominator
LoopCountadd -= 1
' Makes WholeNumber = the total of times the loop occurred
WholenumberTemp = (LoopCountadd)
' Makes the remainder = the new numerator answer
NumeratorTemp1 = (Math.Abs(Numer atorTemp1) - DenominatorTemp 1)
' Goes back to see if the numerator is higher than the denominator
Loop
End If
'Reduces the fraction
Dim reduce = GCD(NumeratorTe mp1, DenominatorTemp 1)
NumeratorTemp1 = (NumeratorTemp1 / reduce)
DenominatorTemp 1 = (DenominatorTem p1 / reduce)
' If something goes wrong then display a message box informing the user to re-enter
' Thier fractions in a proper format. Clear all textboxes and focus on textbox1
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( )
TextBox1.Focus( )
End Try
' These display the results
' If the anwser is a whole # only
If NumeratorTemp1 = 0 Then
TextBox3.Text = "" & (WholenumberTem p)
'If the answer is fractional only
ElseIf (WholenumberTem p) = Nothing Or (WholenumberTem p = 0) Then
TextBox3.Text = "" & (NumeratorTemp1 ) & "/" & (DenominatorTem p1)
' If both a whole # and a Fraction
Else
TextBox3.Text = "" & (WholenumberTem p) & " " & (NumeratorTemp1 ) & "/" & (DenominatorTem p1)
End If
End Sub
[/code]
This one is for SubFract Button
[code=vbnet]
Private Sub SubFract_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles SubFract.Click
' Attempt to run the following code
Try
' If there is only a fraction entered in box1
If (space1 <= 0) Then
Numerator1 = TextBox1.Text.S ubstring(0, Slash1)
Denominator1 = TextBox1.Text.S ubstring(Slash1 + 1)
' If only a whole number is entered in box1
ElseIf (space1 <= 0) And (slash1 <= 0) Then
Wholenumber1 = TextBox1.Text
Numerator1 = (Wholenumber1 * 2)
Denominator1 = 2
' If both a whole number and a fraction are entered in box1
Else
Wholenumber1 = TextBox1.Text.S ubstring(0, Space1)
Numerator1 = TextBox1.Text.S ubstring((Space 1 + 1), (Slash1 - (Space1 + 1)))
Denominator1 = TextBox1.Text.S ubstring(Slash1 + 1)
If Wholenumber1 < 0 Then
Numerator1 = ((Wholenumber1 * Denominator1) - Numerator1)
Else
Numerator1 = (Wholenumber1 * Denominator1) + Numerator1
End If
End If
' If there is only a fraction entered in box2
If (space2 <= 0) And (slash2 > 0) Then
Numerator2 = TextBox2.Text.S ubstring(0, Slash2)
Denominator2 = TextBox2.Text.S ubstring(Slash2 + 1)
Wholenumber2 = 0
' If only a whole number is entered in box2
ElseIf (space2 <= 0) And (slash2 <= 0) Then
Wholenumber2 = TextBox2.Text
Numerator2 = (Wholenumber2 * 2)
Denominator2 = 2
' If both a whole number and a fraction are entered in box2
Else
Wholenumber2 = TextBox2.Text.S ubstring(0, Space2)
Numerator2 = TextBox2.Text.S ubstring((Space 2 + 1), (Slash2 - (Space2 + 1)))
Denominator2 = TextBox2.Text.S ubstring(Slash2 + 1)
If Wholenumber2 < 0 Then
Numerator2 = ((Wholenumber2 * Denominator2) - Numerator2)
Else
Numerator2 = (Wholenumber2 * Denominator2) + Numerator2
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 Denominator1 < Denominator2 And Denominator2 \ Denominator1 = 2 _
And Denominator2 Mod Denominator1 = 0 Then
'Creates the numerator of the answer
Numerator1 = (Numerator1 * 2)
Denominator1 = (Denominator1 * 2)
NumeratorTemp1 = (Numerator1 - Numerator2)
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator2)
' Does the same as above except for when denominator 2 can be divided into denominator_
' 1 with a result of 2
ElseIf Denominator1 > Denominator2 And Denominator2 \ Denominator1 = 2 _
And Denominator1 Mod Denominator2 = 0 Then
'Creates the numerator of the answer
Numerator2 = (Numerator2 * 2)
Denominator2 = (Denominator2 * 2)
NumeratorTemp1 = (Numerator1 - Numerator2)
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator1)
' This formula works for unevenly divisible denominators or when the numerator_
' can be evenly divided multiple times.
ElseIf Denominator1 <> Denominator2 Then
'Creates the numerator of the answer
NumeratorTemp1 = ((Numerator1 * Denominator2) - (Numerator2 * Denominator1))
'Creates the denominator of the answer
DenominatorTemp 1 = (Denominator1 * Denominator2)
' If denominators match run this
Else
' Adds the 2 numerators to create an answer
NumeratorTemp1 = (Numerator1 - Numerator2)
' Keeps the matching denominators
DenominatorTemp 1 = (Denominator2)
End If
' If the numerator is higher then the denominator_
' and not a negitive #
If NumeratorTemp1 >= DenominatorTemp 1 Then
' Setting variables
Dim LoopCountadd As Integer
Dim TimesLoopedadd As Integer
TimesLoopedadd = 0
LoopCountadd = 0
Do While (Math.Abs(Numer atorTemp1)) >= DenominatorTemp 1
' Subtracts the numerator from the denominator
TimesLoopedadd = (DenominatorTem p1 - NumeratorTemp1)
' Counts up 1 for every time the numerator is subtracted from the denominator
LoopCountadd += 1
WholenumberTemp = (LoopCountadd)
' Makes the remainder = the new numerator answer
NumeratorTemp1 = (NumeratorTemp1 - DenominatorTemp 1)
Loop
ElseIf (Math.Abs(Numer atorTemp1)) >= DenominatorTemp 1 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(Numer atorTemp1)) >= DenominatorTemp 1
' Subtracts the numerator from the denominator
TimesLoopedadd = ((Math.Abs(Nume ratorTemp1)) - DenominatorTemp 1)
' Counts up 1 for every time the numerator is subtracted from the denominator
LoopCountadd -= 1
' Makes WholeNumber = the total of times the loop occurred
WholenumberTemp = (LoopCountadd)
' Makes the remainder = the new numerator answer
NumeratorTemp1 = (Math.Abs(Numer atorTemp1) - DenominatorTemp 1)
' Goes back to see if the numerator is higher than the denominator
Loop
End If
'Reduces the fraction
Dim reduce = GCD(NumeratorTe mp1, DenominatorTemp 1)
NumeratorTemp1 = (NumeratorTemp1 / reduce)
DenominatorTemp 1 = (DenominatorTem p1 / reduce)
' If something goes wrong then display a message box informing the user to re-enter
' Thier fractions in a proper format. Clear all textboxes and focus on textbox1
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( )
TextBox1.focus( )
End Try
' These display the results
' If the anwser is a whole # only
If NumeratorTemp1 = 0 Then
TextBox3.Text = "" & (WholenumberTem p)
'If the answer is fractional only
ElseIf (WholenumberTem p) = Nothing Or (WholenumberTem p = 0) Then
TextBox3.Text = "" & (NumeratorTemp1 ) & "/" & (DenominatorTem p1)
' If both a whole # and a Fraction
Else
TextBox3.Text = "" & (WholenumberTem p) & " " & (NumeratorTemp1 ) & "/" & (DenominatorTem p1)
End If
End Sub
[/code]
Comment