Connecting Tech Pros Worldwide Forums | Help | Site Map

Fraction code completed Part 1

Member
 
Join Date: Nov 2007
Location: Iowa
Posts: 38
#1: Jan 10 '08
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
Expand|Select|Wrap|Line Numbers
  1. ' Returns the greatest common divisor using Euclid's algorithm
  2.     Private Function GCD(ByVal x As Integer, ByVal y As Integer) As Integer
  3.         Dim temp As Integer
  4.  
  5.         x = Math.Abs(x)
  6.         y = Math.Abs(y)
  7.  
  8.         Do While (y <> 0)
  9.             temp = x Mod y
  10.             x = y
  11.             y = temp
  12.         Loop
  13.  
  14.         Return x
  15.     End Function
  16.  
Expand|Select|Wrap|Line Numbers
  1.     Private Sub AddFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFract.Click
  2.         ' Declare my variables
  3.         Dim numadd1 As Integer
  4.         Dim numadd2 As Integer
  5.         Dim denadd1 As Integer
  6.         Dim denadd2 As Integer
  7.         Dim slash1 As Integer
  8.         Dim space1 As Integer
  9.         Dim space2 As Integer
  10.         Dim slash2 As Integer
  11.         Dim wholenum1 As Integer
  12.         Dim wholenum2 As Integer
  13.         Dim numtemp1 As Integer
  14.         Dim dentemp1 As Integer
  15.         Dim wholenumtemp As Integer
  16.         Try
  17.  
  18.         ' Gets a value of any spaces or slashs entered into either string
  19.         ' And checks to see if there is a negitive #
  20.         space1 = TextBox1.Text.IndexOf(" ")
  21.         slash1 = TextBox1.Text.IndexOf("/")
  22.         space2 = TextBox2.Text.IndexOf(" ")
  23.         slash2 = TextBox2.Text.IndexOf("/")
  24.  
  25.         ' If there is only a fraction entered in box1
  26.         If (space1 <= 0) Then
  27.             numadd1 = TextBox1.Text.Substring(0, slash1)
  28.             denadd1 = TextBox1.Text.Substring(slash1 + 1)
  29.  
  30.             ' If only a whole number is entered in box1
  31.         ElseIf (space1 <= 0) And (slash1 <= 0) Then
  32.             wholenum1 = TextBox1.Text
  33.             numadd1 = (wholenum1 * 2)
  34.             denadd1 = 2
  35.  
  36.             ' If both a whole number and a fraction are entered in box1
  37.         Else
  38.             wholenum1 = TextBox1.Text.Substring(0, space1)
  39.             numadd1 = TextBox1.Text.Substring((space1 + 1), (slash1 - (space1 + 1)))
  40.             denadd1 = TextBox1.Text.Substring(slash1 + 1)
  41.             If wholenum1 < 0 Then
  42.                 numadd1 = ((wholenum1 * denadd1) - numadd1)
  43.             Else
  44.                 numadd1 = numadd1 + (wholenum1 * denadd1)
  45.             End If
  46.  
  47.         End If
  48.  
  49.         ' If there is only a fraction entered in box2
  50.         If (space2 <= 0) And (slash2 > 0) Then
  51.             numadd2 = TextBox2.Text.Substring(0, slash2)
  52.             denadd2 = TextBox2.Text.Substring(slash2 + 1)
  53.             wholenum2 = 0
  54.  
  55.             ' If only a whole number is entered in box2
  56.         ElseIf (space2 <= 0) And (slash2 <= 0) Then
  57.             wholenum2 = TextBox2.Text
  58.             numadd2 = (wholenum2 * 2)
  59.             denadd2 = 2
  60.  
  61.             ' If both a whole number and a fraction are entered in box2
  62.         Else
  63.             wholenum2 = TextBox2.Text.Substring(0, space2)
  64.             numadd2 = TextBox2.Text.Substring((space2 + 1), (slash2 - (space2 + 1)))
  65.             denadd2 = TextBox2.Text.Substring(slash2 + 1)
  66.             If wholenum2 < 0 Then
  67.                 numadd2 = ((wholenum2 * denadd2) - numadd2)
  68.             Else
  69.                 numadd2 = numadd2 + (wholenum2 * denadd2)
  70.             End If
  71.  
  72.  
  73.         End If
  74.  
  75.  
  76.         'If denominators are different and denominator 1 can be divided into denominator 2 with_ 
  77.         ' a result of 2 and with no remainder. 
  78.         ' This is needed to fix errors that occur in the math formula_
  79.         ' "numtemp1 = ((numsub1 * densub2) - (numsub2 * densub1))" 
  80.         If denadd1 < denadd2 And denadd2 \ denadd1 = 2 And denadd2 Mod denadd1 = 0 Then
  81.  
  82.             'Creates the numerator of the answer
  83.             numadd1 = (numadd1 * 2)
  84.             denadd1 = (denadd1 * 2)
  85.             numtemp1 = (numadd1 + numadd2)
  86.             'Creates the denominator of the answer
  87.             dentemp1 = (denadd2)
  88.  
  89.             ' Does the same as above except for when denominator 2 can be divided into denominator_
  90.             ' 1 with a result of 2
  91.         ElseIf denadd1 > denadd2 And denadd2 \ denadd1 = 2 And denadd1 Mod denadd2 = 0 Then
  92.  
  93.             'Creates the numerator of the answer
  94.             numadd2 = (numadd2 * 2)
  95.             denadd2 = (denadd2 * 2)
  96.             numtemp1 = (numadd1 + numadd2)
  97.             'Creates the denominator of the answer
  98.             dentemp1 = (denadd1)
  99.  
  100.             ' This formula works for unevenly divisible denominators or when the numerator_
  101.             ' can be evenly divided multiple times.
  102.         ElseIf denadd1 <> denadd2 Then
  103.  
  104.             'Creates the numerator of the answer
  105.             numtemp1 = ((numadd1 * denadd2) + (numadd2 * denadd1))
  106.             'Creates the denominator of the answer
  107.             dentemp1 = (denadd1 * denadd2)
  108.  
  109.             ' If denominators match run this
  110.         Else
  111.             ' Adds the 2 numerators to create an answer
  112.             numtemp1 = (numadd1 + numadd2)
  113.             ' Keeps the matching denominators
  114.             dentemp1 = (denadd2)
  115.  
  116.         End If
  117.  
  118.         ' If the numerator is higher then the denominator_
  119.         ' and not a negitive #
  120.         If numtemp1 >= dentemp1 Then
  121.  
  122.             ' Setting variables
  123.             Dim LoopCountadd As Integer
  124.             Dim TimesLoopedadd As Integer
  125.             TimesLoopedadd = 0
  126.             LoopCountadd = 0
  127.             Do While (Math.Abs(numtemp1)) >= dentemp1
  128.                 ' Subtracts the numerator from the denominator
  129.                 TimesLoopedadd = (dentemp1 - numtemp1)
  130.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  131.                 LoopCountadd += 1
  132.                 wholenumtemp = (LoopCountadd)
  133.                 ' Makes the remainder = the new numerator answer
  134.                 numtemp1 = (numtemp1 - dentemp1)
  135.             Loop
  136.  
  137.         ElseIf (Math.Abs(numtemp1)) >= dentemp1 Then
  138.  
  139.             ' Setting variables
  140.             Dim LoopCountadd As Integer
  141.             Dim TimesLoopedadd As Integer
  142.             TimesLoopedadd = 0
  143.             LoopCountadd = 0
  144.  
  145.             ' A loop to subtract the denominator from the numerator until the numerator_
  146.             ' is smaller than the denominator
  147.             Do While (Math.Abs(numtemp1)) >= dentemp1
  148.                 ' Subtracts the numerator from the denominator
  149.                 TimesLoopedadd = ((Math.Abs(numtemp1)) - dentemp1)
  150.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  151.                 LoopCountadd -= 1
  152.                 ' Makes WholeNumber = the total of times the loop occurred
  153.                 wholenumtemp = (LoopCountadd)
  154.                 ' Makes the remainder = the new numerator answer
  155.                 numtemp1 = (Math.Abs(numtemp1) - dentemp1)
  156.  
  157.                 ' Goes back to see if the numerator is higher than the denominator
  158.             Loop
  159.  
  160.         End If
  161.  
  162.         'Reduces the fraction
  163.         Dim reduce = GCD(numtemp1, dentemp1)
  164.         numtemp1 = (numtemp1 / reduce)
  165.         dentemp1 = (dentemp1 / reduce)
  166.  
  167.         Catch ex As Exception
  168.             MessageBox.Show("Please enter Fractions in the following formats only" _
  169.             & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
  170.             "#" & vbCr & "-#", "Error")
  171.             TextBox1.Clear()
  172.             TextBox2.Clear()
  173.             TextBox3.Clear()
  174.  
  175.         End Try
  176.         ' These display the results
  177.         ' If the anwser is a whole # only
  178.         If numtemp1 = 0 Then
  179.             TextBox3.Text = "" & (wholenumtemp)
  180.  
  181.             'If the answer is fractional only
  182.         ElseIf (wholenumtemp) = Nothing Or (wholenumtemp = 0) Then
  183.             TextBox3.Text = "" & (numtemp1) & "/" & (dentemp1)
  184.  
  185.             ' If both a whole # and a Fraction
  186.         Else
  187.             TextBox3.Text = "" & (wholenumtemp) & " " & (numtemp1) & "/" & (dentemp1)
  188.  
  189.         End If
  190.  
  191.     End Sub
  192.  
Expand|Select|Wrap|Line Numbers
  1.     Private Sub SubFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubFract.Click
  2.         ' Declare my variables
  3.         Dim numsub1 As Integer
  4.         Dim numsub2 As Integer
  5.         Dim densub1 As Integer
  6.         Dim densub2 As Integer
  7.         Dim slash1 As Integer
  8.         Dim space1 As Integer
  9.         Dim space2 As Integer
  10.         Dim slash2 As Integer
  11.         Dim wholenum1 As Integer
  12.         Dim wholenum2 As Integer
  13.         Dim numtemp1 As Integer
  14.         Dim dentemp1 As Integer
  15.         Dim wholenumtemp As Integer
  16.         Try
  17.             ' Gets a value of any spaces or slashs entered into either string
  18.             ' And checks to see if there is a negitive #
  19.             space1 = TextBox1.Text.IndexOf(" ")
  20.             slash1 = TextBox1.Text.IndexOf("/")
  21.             space2 = TextBox2.Text.IndexOf(" ")
  22.             slash2 = TextBox2.Text.IndexOf("/")
  23.  
  24.             ' If there is only a fraction entered in box1
  25.             If (space1 <= 0) Then
  26.                 numsub1 = TextBox1.Text.Substring(0, slash1)
  27.                 densub1 = TextBox1.Text.Substring(slash1 + 1)
  28.  
  29.                 ' If only a whole number is entered in box1
  30.             ElseIf (space1 <= 0) And (slash1 <= 0) Then
  31.                 wholenum1 = TextBox1.Text
  32.                 numsub1 = (wholenum1 * 2)
  33.                 densub1 = 2
  34.  
  35.                 ' If both a whole number and a fraction are entered in box1
  36.             Else
  37.                 wholenum1 = TextBox1.Text.Substring(0, space1)
  38.                 numsub1 = TextBox1.Text.Substring((space1 + 1), (slash1 - (space1 + 1)))
  39.                 densub1 = TextBox1.Text.Substring(slash1 + 1)
  40.                 If wholenum1 < 0 Then
  41.                     numsub1 = ((wholenum1 * densub1) - numsub1)
  42.                 Else
  43.                     numsub1 = (wholenum1 * densub1) + numsub1
  44.                 End If
  45.  
  46.             End If
  47.  
  48.             ' If there is only a fraction entered in box2
  49.             If (space2 <= 0) And (slash2 > 0) Then
  50.                 numsub2 = TextBox2.Text.Substring(0, slash2)
  51.                 densub2 = TextBox2.Text.Substring(slash2 + 1)
  52.                 wholenum2 = 0
  53.  
  54.                 ' If only a whole number is entered in box2
  55.             ElseIf (space2 <= 0) And (slash2 <= 0) Then
  56.                 wholenum2 = TextBox2.Text
  57.                 numsub2 = (wholenum2 * 2)
  58.                 densub2 = 2
  59.  
  60.                 ' If both a whole number and a fraction are entered in box2
  61.             Else
  62.                 wholenum2 = TextBox2.Text.Substring(0, space2)
  63.                 numsub2 = TextBox2.Text.Substring((space2 + 1), (slash2 - (space2 + 1)))
  64.                 densub2 = TextBox2.Text.Substring(slash2 + 1)
  65.                 If wholenum2 < 0 Then
  66.                     numsub2 = ((wholenum2 * densub2) - numsub2)
  67.                 Else
  68.                     numsub2 = (wholenum2 * densub2) + numsub2
  69.                 End If
  70.  
  71.  
  72.             End If
  73.  
  74.  
  75.             'If denominators are different and denominator 1 can be divided into denominator 2 with_ 
  76.             ' a result of 2 and with no remainder. 
  77.             ' This is needed to fix errors that occur in the math formula_
  78.             ' "numtemp1 = ((numsub1 * densub2) - (numsub2 * densub1))" 
  79.             If densub1 < densub2 And densub2 \ densub1 = 2 And densub2 Mod densub1 = 0 Then
  80.  
  81.                 'Creates the numerator of the answer
  82.                 numsub1 = (numsub1 * 2)
  83.                 densub1 = (densub1 * 2)
  84.                 numtemp1 = (numsub1 - numsub2)
  85.                 'Creates the denominator of the answer
  86.                 dentemp1 = (densub2)
  87.  
  88.                 ' Does the same as above except for when denominator 2 can be divided into denominator_
  89.                 ' 1 with a result of 2
  90.             ElseIf densub1 > densub2 And densub2 \ densub1 = 2 And densub1 Mod densub2 = 0 Then
  91.  
  92.                 'Creates the numerator of the answer
  93.                 numsub2 = (numsub2 * 2)
  94.                 densub2 = (densub2 * 2)
  95.                 numtemp1 = (numsub1 - numsub2)
  96.                 'Creates the denominator of the answer
  97.                 dentemp1 = (densub1)
  98.  
  99.                 ' This formula works for unevenly divisible denominators or when the numerator_
  100.                 ' can be evenly divided multiple times.
  101.             ElseIf densub1 <> densub2 Then
  102.  
  103.                 'Creates the numerator of the answer
  104.                 numtemp1 = ((numsub1 * densub2) - (numsub2 * densub1))
  105.                 'Creates the denominator of the answer
  106.                 dentemp1 = (densub1 * densub2)
  107.  
  108.                 ' If denominators match run this
  109.             Else
  110.                 ' Adds the 2 numerators to create an answer
  111.                 numtemp1 = (numsub1 - numsub2)
  112.                 ' Keeps the matching denominators
  113.                 dentemp1 = (densub2)
  114.  
  115.             End If
  116.  
  117.             ' If the numerator is higher then the denominator_
  118.             ' and not a negitive #
  119.             If numtemp1 >= dentemp1 Then
  120.  
  121.                 ' Setting variables
  122.                 Dim LoopCountadd As Integer
  123.                 Dim TimesLoopedadd As Integer
  124.                 TimesLoopedadd = 0
  125.                 LoopCountadd = 0
  126.                 Do While (Math.Abs(numtemp1)) >= dentemp1
  127.                     ' Subtracts the numerator from the denominator
  128.                     TimesLoopedadd = (dentemp1 - numtemp1)
  129.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  130.                     LoopCountadd += 1
  131.                     wholenumtemp = (LoopCountadd)
  132.                     ' Makes the remainder = the new numerator answer
  133.                     numtemp1 = (numtemp1 - dentemp1)
  134.                 Loop
  135.  
  136.             ElseIf (Math.Abs(numtemp1)) >= dentemp1 Then
  137.  
  138.                 ' Setting variables
  139.                 Dim LoopCountadd As Integer
  140.                 Dim TimesLoopedadd As Integer
  141.                 TimesLoopedadd = 0
  142.                 LoopCountadd = 0
  143.  
  144.                 ' A loop to subtract the denominator from the numerator until the numerator_
  145.                 ' is smaller than the denominator
  146.                 Do While (Math.Abs(numtemp1)) >= dentemp1
  147.                     ' Subtracts the numerator from the denominator
  148.                     TimesLoopedadd = ((Math.Abs(numtemp1)) - dentemp1)
  149.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  150.                     LoopCountadd -= 1
  151.                     ' Makes WholeNumber = the total of times the loop occurred
  152.                     wholenumtemp = (LoopCountadd)
  153.                     ' Makes the remainder = the new numerator answer
  154.                     numtemp1 = (Math.Abs(numtemp1) - dentemp1)
  155.  
  156.                     ' Goes back to see if the numerator is higher than the denominator
  157.                 Loop
  158.  
  159.             End If
  160.  
  161.             'Reduces the fraction
  162.             Dim reduce = GCD(numtemp1, dentemp1)
  163.             numtemp1 = (numtemp1 / reduce)
  164.             dentemp1 = (dentemp1 / reduce)
  165.  
  166.         Catch ex As Exception
  167.             MessageBox.Show("Please enter Fractions in the following formats only" _
  168.             & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
  169.             "#" & vbCr & "-#", "Error")
  170.             TextBox1.Clear()
  171.             TextBox2.Clear()
  172.             TextBox3.Clear()
  173.  
  174.         End Try
  175.         ' These display the results
  176.         ' If the anwser is a whole # only
  177.         If numtemp1 = 0 Then
  178.             TextBox3.Text = "" & (wholenumtemp)
  179.  
  180.             'If the answer is fractional only
  181.         ElseIf (wholenumtemp) = Nothing Or (wholenumtemp = 0) Then
  182.             TextBox3.Text = "" & (numtemp1) & "/" & (dentemp1)
  183.  
  184.             ' If both a whole # and a Fraction
  185.         Else
  186.             TextBox3.Text = "" & (wholenumtemp) & " " & (numtemp1) & "/" & (dentemp1)
  187.  
  188.         End If
  189.  
  190.     End Sub
  191.  

Hope you find it usefull,
James

RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#2: Jan 10 '08

re: Fraction code completed Part 1


Adding CODE tags makes this post disappear.
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,002
#3: Jan 11 '08

re: Fraction code completed Part 1


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?
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#4: Jan 11 '08

re: Fraction code completed Part 1


Quote:

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.
Member
 
Join Date: Nov 2007
Location: Iowa
Posts: 38
#5: Jan 11 '08

re: Fraction code completed Part 1


Ok guys thanks well heres the rest of the code.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Private Sub MultiFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultiFract.Click
  3.  
  4.         ' Declare my variables
  5.         Dim nummulti1 As Integer
  6.         Dim nummulti2 As Integer
  7.         Dim denmulti1 As Integer
  8.         Dim denmulti2 As Integer
  9.         Dim slash1 As Integer
  10.         Dim space1 As Integer
  11.         Dim space2 As Integer
  12.         Dim slash2 As Integer
  13.         Dim wholenum1 As Integer
  14.         Dim wholenum2 As Integer
  15.         Dim numtemp1 As Integer
  16.         Dim dentemp1 As Integer
  17.         Dim wholenumtemp As Integer
  18.         Try
  19.             ' Gets a value of any spaces or slashs entered into either string
  20.             ' And checks to see if there is a negitive #
  21.             space1 = TextBox1.Text.IndexOf(" ")
  22.             slash1 = TextBox1.Text.IndexOf("/")
  23.             space2 = TextBox2.Text.IndexOf(" ")
  24.             slash2 = TextBox2.Text.IndexOf("/")
  25.  
  26.             ' If there is only a fraction entered in box1
  27.             If (space1 <= 0) Then
  28.                 nummulti1 = TextBox1.Text.Substring(0, slash1)
  29.                 denmulti1 = TextBox1.Text.Substring(slash1 + 1)
  30.  
  31.                 ' If only a whole number is entered in box1
  32.             ElseIf (space1 <= 0) And (slash1 <= 0) Then
  33.                 wholenum1 = TextBox1.Text
  34.                 nummulti1 = (wholenum1 * 2)
  35.                 denmulti1 = 2
  36.  
  37.                 ' If both a whole number and a fraction are entered in box1
  38.             Else
  39.                 wholenum1 = TextBox1.Text.Substring(0, space1)
  40.                 nummulti1 = TextBox1.Text.Substring((space1 + 1), (slash1 - (space1 + 1)))
  41.                 denmulti1 = TextBox1.Text.Substring(slash1 + 1)
  42.                 If wholenum1 < 0 Then
  43.                     nummulti1 = ((wholenum1 * denmulti1) - nummulti1)
  44.                 Else
  45.                     nummulti1 = (wholenum1 * denmulti1) + nummulti1
  46.                 End If
  47.  
  48.             End If
  49.  
  50.             ' If there is only a fraction entered in box2
  51.             If (space2 <= 0) And (slash2 > 0) Then
  52.                 nummulti2 = TextBox2.Text.Substring(0, slash2)
  53.                 denmulti2 = TextBox2.Text.Substring(slash2 + 1)
  54.                 wholenum2 = 0
  55.  
  56.                 ' If only a whole number is entered in box2
  57.             ElseIf (space2 <= 0) And (slash2 <= 0) Then
  58.                 wholenum2 = TextBox2.Text
  59.                 nummulti2 = (wholenum2 * 2)
  60.                 denmulti2 = 2
  61.  
  62.                 ' If both a whole number and a fraction are entered in box2
  63.             Else
  64.                 wholenum2 = TextBox2.Text.Substring(0, space2)
  65.                 nummulti2 = TextBox2.Text.Substring((space2 + 1), (slash2 - (space2 + 1)))
  66.                 denmulti2 = TextBox2.Text.Substring(slash2 + 1)
  67.                 If wholenum2 < 0 Then
  68.                     nummulti2 = ((wholenum2 * denmulti2) - nummulti2)
  69.                 Else
  70.                     nummulti2 = (wholenum2 * denmulti2) + nummulti2
  71.                 End If
  72.  
  73.  
  74.             End If
  75.  
  76.             ' Multiplies the 2 numerators 
  77.             numtemp1 = (nummulti1 * nummulti2)
  78.             ' Multiplies the  denominators
  79.             dentemp1 = (denmulti2 * denmulti1)
  80.  
  81.             ' If the numerator is higher then the denominator_
  82.             ' and not a negitive #
  83.             If numtemp1 >= dentemp1 Then
  84.  
  85.                 ' Setting variables
  86.                 Dim LoopCountadd As Integer
  87.                 Dim TimesLoopedadd As Integer
  88.                 TimesLoopedadd = 0
  89.                 LoopCountadd = 0
  90.                 Do While (Math.Abs(numtemp1)) >= dentemp1
  91.                     ' Subtracts the numerator from the denominator
  92.                     TimesLoopedadd = (dentemp1 - numtemp1)
  93.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  94.                     LoopCountadd += 1
  95.                     wholenumtemp = (LoopCountadd)
  96.                     ' Makes the remainder = the new numerator answer
  97.                     numtemp1 = (numtemp1 - dentemp1)
  98.                 Loop
  99.                 ' Otherwise this loop is used
  100.             ElseIf (Math.Abs(numtemp1)) >= dentemp1 Then
  101.  
  102.                 ' Setting variables
  103.                 Dim LoopCountadd As Integer
  104.                 Dim TimesLoopedadd As Integer
  105.                 TimesLoopedadd = 0
  106.                 LoopCountadd = 0
  107.  
  108.                 ' A loop to subtract the denominator from the numerator until the numerator_
  109.                 ' is smaller than the denominator
  110.                 Do While (Math.Abs(numtemp1)) >= dentemp1
  111.                     ' Subtracts the numerator from the denominator
  112.                     TimesLoopedadd = ((Math.Abs(numtemp1)) - dentemp1)
  113.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  114.                     LoopCountadd -= 1
  115.                     ' Makes WholeNumber = the total of times the loop occurred
  116.                     wholenumtemp = (LoopCountadd)
  117.                     ' Makes the remainder = the new numerator answer
  118.                     numtemp1 = (Math.Abs(numtemp1) - dentemp1)
  119.  
  120.                     ' Goes back to see if the numerator is higher than the denominator
  121.                 Loop
  122.  
  123.             End If
  124.  
  125.             'Reduces the fraction
  126.             Dim reduce = GCD(numtemp1, dentemp1)
  127.             numtemp1 = (numtemp1 / reduce)
  128.             dentemp1 = (dentemp1 / reduce)
  129.  
  130.         Catch ex As Exception
  131.             MessageBox.Show("Please enter Fractions in the following formats only" _
  132.             & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
  133.             "#" & vbCr & "-#", "Error")
  134.             TextBox1.Clear()
  135.             TextBox2.Clear()
  136.             TextBox3.Clear()
  137.  
  138.         End Try
  139.         ' These display the results
  140.         ' If the anwser is a whole # only
  141.         If numtemp1 = 0 Then
  142.             TextBox3.Text = "" & (wholenumtemp)
  143.  
  144.             'If the answer is fractional only
  145.         ElseIf (wholenumtemp) = Nothing Or (wholenumtemp = 0) Then
  146.             TextBox3.Text = "" & (numtemp1) & "/" & (dentemp1)
  147.  
  148.             ' If both a whole # and a Fraction
  149.         Else
  150.             TextBox3.Text = "" & (wholenumtemp) & " " & (numtemp1) & "/" & (dentemp1)
  151.  
  152.         End If
  153.     End Sub
  154.  
Expand|Select|Wrap|Line Numbers
  1.  Private Sub DivFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DivFract.Click
  2.  
  3.         ' Declare my variables
  4.         Dim numdivide1 As Integer
  5.         Dim numdivide2 As Integer
  6.         Dim dendivide1 As Integer
  7.         Dim dendivide2 As Integer
  8.         Dim slash1 As Integer
  9.         Dim space1 As Integer
  10.         Dim space2 As Integer
  11.         Dim slash2 As Integer
  12.         Dim wholenum1 As Integer
  13.         Dim wholenum2 As Integer
  14.         Dim numtemp1 As Integer
  15.         Dim dentemp1 As Integer
  16.         Dim wholenumtemp As Integer
  17.         Try
  18.             ' Gets a value of any spaces or slashs entered into either string
  19.             ' And checks to see if there is a negitive #
  20.             space1 = TextBox1.Text.IndexOf(" ")
  21.             slash1 = TextBox1.Text.IndexOf("/")
  22.  
  23.             space2 = TextBox2.Text.IndexOf(" ")
  24.             slash2 = TextBox2.Text.IndexOf("/")
  25.  
  26.  
  27.             ' If there is only a fraction entered in box1
  28.             If (space1 <= 0) Then
  29.                 numdivide1 = TextBox1.Text.Substring(0, slash1)
  30.                 dendivide1 = TextBox1.Text.Substring(slash1 + 1)
  31.  
  32.                 ' If only a whole number is entered in box1
  33.             ElseIf (space1 <= 0) And (slash1 <= 0) Then
  34.                 wholenum1 = TextBox1.Text
  35.                 numdivide1 = (wholenum1 * 2)
  36.                 dendivide1 = 2
  37.  
  38.                 ' If both a whole number and a fraction are entered in box1
  39.             Else
  40.                 wholenum1 = TextBox1.Text.Substring(0, space1)
  41.                 numdivide1 = TextBox1.Text.Substring((space1 + 1), (slash1 - (space1 + 1)))
  42.                 dendivide1 = TextBox1.Text.Substring(slash1 + 1)
  43.                 If wholenum1 < 0 Then
  44.                     numdivide1 = ((wholenum1 * dendivide1) - numdivide1)
  45.                 Else
  46.                     numdivide1 = (wholenum1 * dendivide1) + numdivide1
  47.                 End If
  48.  
  49.             End If
  50.  
  51.             ' If there is only a fraction entered in box2
  52.             If (space2 <= 0) And (slash2 > 0) Then
  53.                 numdivide2 = TextBox2.Text.Substring(0, slash2)
  54.                 dendivide2 = TextBox2.Text.Substring(slash2 + 1)
  55.                 wholenum2 = 0
  56.  
  57.                 ' If only a whole number is entered in box2
  58.             ElseIf (space2 <= 0) And (slash2 <= 0) Then
  59.                 wholenum2 = TextBox2.Text
  60.                 numdivide2 = (wholenum2 * 2)
  61.                 dendivide2 = 2
  62.  
  63.                 ' If both a whole number and a fraction are entered in box2
  64.             Else
  65.                 wholenum2 = TextBox2.Text.Substring(0, space2)
  66.                 numdivide2 = TextBox2.Text.Substring((space2 + 1), (slash2 - (space2 + 1)))
  67.                 dendivide2 = TextBox2.Text.Substring(slash2 + 1)
  68.                 If wholenum2 < 0 Then
  69.                     numdivide2 = ((wholenum2 * dendivide2) - numdivide2)
  70.                 Else
  71.                     numdivide2 = (wholenum2 * dendivide2) + numdivide2
  72.                 End If
  73.  
  74.  
  75.             End If
  76.  
  77.             ' Multiplies the 2 numerators 
  78.             numtemp1 = (numdivide1 * dendivide2)
  79.             ' Multiplies the  denominators
  80.             dentemp1 = (dendivide1 * numdivide2)
  81.  
  82.             ' If the numerator is higher then the denominator_
  83.             ' and not a negitive #
  84.             If numtemp1 >= dentemp1 And dentemp1 > 0 Then
  85.  
  86.                 ' Setting variables
  87.                 Dim LoopCountadd As Integer
  88.                 Dim TimesLoopedadd As Integer
  89.                 TimesLoopedadd = 0
  90.                 LoopCountadd = 0
  91.                 Do While (Math.Abs(numtemp1)) >= dentemp1
  92.                     ' Subtracts the numerator from the denominator
  93.                     TimesLoopedadd = (dentemp1 - numtemp1)
  94.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  95.                     LoopCountadd += 1
  96.                     wholenumtemp = (LoopCountadd)
  97.                     ' Makes the remainder = the new numerator answer
  98.                     numtemp1 = (numtemp1 - dentemp1)
  99.                 Loop
  100.                 ' Otherwise this loop is used
  101.             ElseIf (Math.Abs(numtemp1)) >= dentemp1 Then
  102.  
  103.                 ' Setting variables
  104.                 Dim LoopCountadd As Integer
  105.                 Dim TimesLoopedadd As Integer
  106.                 TimesLoopedadd = 0
  107.                 LoopCountadd = 0
  108.                 dentemp1 = (Math.Abs(dentemp1))
  109.                 ' A loop to subtract the denominator from the numerator until the numerator_
  110.                 ' is smaller than the denominator
  111.                 Do While (Math.Abs(numtemp1)) >= (dentemp1)
  112.                     ' Subtracts the numerator from the denominator
  113.                     TimesLoopedadd = ((Math.Abs(numtemp1)) - dentemp1)
  114.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  115.                     LoopCountadd -= 1
  116.                     ' Makes WholeNumber = the total of times the loop occurred
  117.                     wholenumtemp = (LoopCountadd)
  118.                     ' Makes the remainder = the new numerator answer
  119.                     numtemp1 = (Math.Abs(numtemp1) - dentemp1)
  120.  
  121.                     ' Goes back to see if the numerator is higher than the denominator
  122.                 Loop
  123.  
  124.             End If
  125.  
  126.             'Reduces the fraction
  127.             Dim reduce = GCD(numtemp1, dentemp1)
  128.             numtemp1 = (numtemp1 / reduce)
  129.             dentemp1 = (dentemp1 / reduce)
  130.  
  131.         Catch ex As Exception
  132.             MessageBox.Show("Please enter Fractions in the following formats only" _
  133.             & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
  134.             "#" & vbCr & "-#", "Error")
  135.             TextBox1.Clear()
  136.             TextBox2.Clear()
  137.             TextBox3.Clear()
  138.  
  139.         End Try
  140.         ' These display the results
  141.         ' If the anwser is a whole # only
  142.         If numtemp1 = 0 Then
  143.             TextBox3.Text = "" & (wholenumtemp)
  144.  
  145.             'If the answer is fractional only
  146.         ElseIf (wholenumtemp) = Nothing Or (wholenumtemp = 0) Then
  147.             TextBox3.Text = "" & (numtemp1) & "/" & (dentemp1)
  148.  
  149.             ' If both a whole # and a Fraction
  150.         Else
  151.             TextBox3.Text = "" & (wholenumtemp) & " " & (numtemp1) & "/" & (dentemp1)
  152.  
  153.         End If
  154.  
  155.     End Sub
  156.  

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
Reply


Similar .NET Framework bytes