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.
Expand|Select|Wrap|Line Numbers
- ' 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 DenominatorTemp1 As Integer
- Dim WholenumberTemp As Integer
- ' Gets a value of any spaces or slashs entered into either string
- Dim Slash1 As Integer = TextBox1.Text.IndexOf("/")
- Dim Space1 As Integer = TextBox1.Text.IndexOf(" ")
- Dim Space2 As Integer = TextBox1.Text.IndexOf(" ")
- Dim Slash2 As Integer = TextBox1.Text.IndexOf("/")
Expand|Select|Wrap|Line Numbers
- ' 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
Expand|Select|Wrap|Line Numbers
- Private Sub AddFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.Substring(0, Slash1)
- Denominator1 = TextBox1.Text.Substring(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.Substring(0, Space1)
- Numerator1 = TextBox1.Text.Substring((Space1 + 1), (Slash1 - (Space1 + 1)))
- Denominator1 = TextBox1.Text.Substring(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.Substring(0, Slash2)
- Denominator2 = TextBox2.Text.Substring(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.Substring(0, Space2)
- Numerator2 = TextBox2.Text.Substring((Space2 + 1), (Slash2 - (Space2 + 1)))
- Denominator2 = TextBox2.Text.Substring(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
- DenominatorTemp1 = (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
- DenominatorTemp1 = (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
- DenominatorTemp1 = (Denominator1 * Denominator2)
- ' If denominators match run this
- Else
- ' Adds the 2 numerators to create an answer
- NumeratorTemp1 = (Numerator1 + Numerator2)
- ' Keeps the matching denominators
- DenominatorTemp1 = (Denominator2)
- End If
- ' If the numerator is higher then the denominator_
- ' and not a negitive #
- If NumeratorTemp1 >= DenominatorTemp1 Then
- ' Setting variables
- Dim LoopCountadd As Integer
- Dim TimesLoopedadd As Integer
- TimesLoopedadd = 0
- LoopCountadd = 0
- Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
- ' Subtracts the numerator from the denominator
- TimesLoopedadd = (DenominatorTemp1 - 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 - DenominatorTemp1)
- Loop
- ElseIf (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1 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(NumeratorTemp1)) >= DenominatorTemp1
- ' Subtracts the numerator from the denominator
- TimesLoopedadd = ((Math.Abs(NumeratorTemp1)) - DenominatorTemp1)
- ' 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(NumeratorTemp1) - DenominatorTemp1)
- ' Goes back to see if the numerator is higher than the denominator
- Loop
- End If
- 'Reduces the fraction
- Dim reduce = GCD(NumeratorTemp1, DenominatorTemp1)
- NumeratorTemp1 = (NumeratorTemp1 / reduce)
- DenominatorTemp1 = (DenominatorTemp1 / 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 = "" & (WholenumberTemp)
- 'If the answer is fractional only
- ElseIf (WholenumberTemp) = Nothing Or (WholenumberTemp = 0) Then
- TextBox3.Text = "" & (NumeratorTemp1) & "/" & (DenominatorTemp1)
- ' If both a whole # and a Fraction
- Else
- TextBox3.Text = "" & (WholenumberTemp) & " " & (NumeratorTemp1) & "/" & (DenominatorTemp1)
- End If
- End Sub
Expand|Select|Wrap|Line Numbers
- Private Sub SubFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.Substring(0, Slash1)
- Denominator1 = TextBox1.Text.Substring(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.Substring(0, Space1)
- Numerator1 = TextBox1.Text.Substring((Space1 + 1), (Slash1 - (Space1 + 1)))
- Denominator1 = TextBox1.Text.Substring(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.Substring(0, Slash2)
- Denominator2 = TextBox2.Text.Substring(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.Substring(0, Space2)
- Numerator2 = TextBox2.Text.Substring((Space2 + 1), (Slash2 - (Space2 + 1)))
- Denominator2 = TextBox2.Text.Substring(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
- DenominatorTemp1 = (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
- DenominatorTemp1 = (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
- DenominatorTemp1 = (Denominator1 * Denominator2)
- ' If denominators match run this
- Else
- ' Adds the 2 numerators to create an answer
- NumeratorTemp1 = (Numerator1 - Numerator2)
- ' Keeps the matching denominators
- DenominatorTemp1 = (Denominator2)
- End If
- ' If the numerator is higher then the denominator_
- ' and not a negitive #
- If NumeratorTemp1 >= DenominatorTemp1 Then
- ' Setting variables
- Dim LoopCountadd As Integer
- Dim TimesLoopedadd As Integer
- TimesLoopedadd = 0
- LoopCountadd = 0
- Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
- ' Subtracts the numerator from the denominator
- TimesLoopedadd = (DenominatorTemp1 - 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 - DenominatorTemp1)
- Loop
- ElseIf (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1 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(NumeratorTemp1)) >= DenominatorTemp1
- ' Subtracts the numerator from the denominator
- TimesLoopedadd = ((Math.Abs(NumeratorTemp1)) - DenominatorTemp1)
- ' 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(NumeratorTemp1) - DenominatorTemp1)
- ' Goes back to see if the numerator is higher than the denominator
- Loop
- End If
- 'Reduces the fraction
- Dim reduce = GCD(NumeratorTemp1, DenominatorTemp1)
- NumeratorTemp1 = (NumeratorTemp1 / reduce)
- DenominatorTemp1 = (DenominatorTemp1 / 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 = "" & (WholenumberTemp)
- 'If the answer is fractional only
- ElseIf (WholenumberTemp) = Nothing Or (WholenumberTemp = 0) Then
- TextBox3.Text = "" & (NumeratorTemp1) & "/" & (DenominatorTemp1)
- ' If both a whole # and a Fraction
- Else
- TextBox3.Text = "" & (WholenumberTemp) & " " & (NumeratorTemp1) & "/" & (DenominatorTemp1)
- End If
- End Sub