473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Fractions code completed Part 2

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.

Expand|Select|Wrap|Line Numbers
  1. ' Declare my variables
  2.     Dim Numerator1 As Integer
  3.     Dim Numerator2 As Integer
  4.     Dim Denominator1 As Integer
  5.     Dim Denominator2 As Integer
  6.     Dim Wholenumber1 As Integer
  7.     Dim Wholenumber2 As Integer
  8.     Dim NumeratorTemp1 As Integer
  9.     Dim DenominatorTemp1 As Integer
  10.     Dim WholenumberTemp As Integer
  11.     ' Gets a value of any spaces or slashs entered into either string
  12.     Dim Slash1 As Integer = TextBox1.Text.IndexOf("/")
  13.     Dim Space1 As Integer = TextBox1.Text.IndexOf(" ")
  14.     Dim Space2 As Integer = TextBox1.Text.IndexOf(" ")
  15.     Dim Slash2 As Integer = TextBox1.Text.IndexOf("/")
  16.  
Creat a function to get the greatest common divisor with this code

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.  
This code will run when the Addfract button is pressed
Expand|Select|Wrap|Line Numbers
  1.  Private Sub AddFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFract.Click
  2.         ' Attempts to run the code below
  3.         Try
  4.  
  5.             ' If there is only a fraction entered in box1
  6.             If (Space1 <= 0) Then
  7.                 Numerator1 = TextBox1.Text.Substring(0, Slash1)
  8.                 Denominator1 = TextBox1.Text.Substring(Slash1 + 1)
  9.  
  10.                 ' If only a whole number is entered in box1
  11.             ElseIf (Space1 <= 0) And (Slash1 <= 0) Then
  12.                 Wholenumber1 = TextBox1.Text
  13.                 Numerator1 = (Wholenumber1 * 2)
  14.                 Denominator1 = 2
  15.  
  16.                 ' If both a whole number and a fraction are entered in box1
  17.             Else
  18.                 Wholenumber1 = TextBox1.Text.Substring(0, Space1)
  19.                 Numerator1 = TextBox1.Text.Substring((Space1 + 1), (Slash1 - (Space1 + 1)))
  20.                 Denominator1 = TextBox1.Text.Substring(Slash1 + 1)
  21.                 If Wholenumber1 < 0 Then
  22.                     Numerator1 = ((Wholenumber1 * Denominator1) - Numerator1)
  23.                 Else
  24.                     Numerator1 = Numerator1 + (Wholenumber1 * Denominator1)
  25.                 End If
  26.  
  27.             End If
  28.  
  29.             ' If there is only a fraction entered in box2
  30.             If (Space2 <= 0) And (Slash2 > 0) Then
  31.                 Numerator2 = TextBox2.Text.Substring(0, Slash2)
  32.                 Denominator2 = TextBox2.Text.Substring(Slash2 + 1)
  33.                 Wholenumber2 = 0
  34.  
  35.                 ' If only a whole number is entered in box2
  36.             ElseIf (Space2 <= 0) And (Slash2 <= 0) Then
  37.                 Wholenumber2 = TextBox2.Text
  38.                 Numerator2 = (Wholenumber2 * 2)
  39.                 Denominator2 = 2
  40.  
  41.                 ' If both a whole number and a fraction are entered in box2
  42.             Else
  43.                 Wholenumber2 = TextBox2.Text.Substring(0, Space2)
  44.                 Numerator2 = TextBox2.Text.Substring((Space2 + 1), (Slash2 - (Space2 + 1)))
  45.                 Denominator2 = TextBox2.Text.Substring(Slash2 + 1)
  46.                 If Wholenumber2 < 0 Then
  47.                     Numerator2 = ((Wholenumber2 * Denominator2) - Numerator2)
  48.                 Else
  49.                     Numerator2 = Numerator2 + (Wholenumber2 * Denominator2)
  50.                 End If
  51.  
  52.  
  53.             End If
  54.  
  55.  
  56.             'If denominators are different and denominator 1 can be divided into denominator 2 with_ 
  57.             ' a result of 2 and with no remainder. 
  58.             ' This is needed to fix errors that occur in the math formula_
  59.             ' "numtemp1 = ((numsub1 * densub2) - (numsub2 * densub1))" 
  60.             If Denominator1 < Denominator2 And Denominator2 \ _
  61.             Denominator1 = 2 And Denominator2 Mod Denominator1 = 0 Then
  62.  
  63.                 'Creates the numerator of the answer
  64.                 Numerator1 = (Numerator1 * 2)
  65.                 Denominator1 = (Denominator1 * 2)
  66.                 numeratortemp1 = (Numerator1 + Numerator2)
  67.                 'Creates the denominator of the answer
  68.                 DenominatorTemp1 = (Denominator2)
  69.  
  70.                 ' Does the same as above except for when denominator 2 can be divided into denominator_
  71.                 ' 1 with a result of 2
  72.             ElseIf Denominator1 > Denominator2 And Denominator2 \ Denominator1 = 2 And Denominator1 Mod Denominator2 = 0 Then
  73.  
  74.                 'Creates the numerator of the answer
  75.                 Numerator2 = (Numerator2 * 2)
  76.                 Denominator2 = (Denominator2 * 2)
  77.                 NumeratorTemp1 = (Numerator1 + Numerator2)
  78.                 'Creates the denominator of the answer
  79.                 DenominatorTemp1 = (Denominator1)
  80.  
  81.                 ' This formula works for unevenly divisible denominators or when the numerator_
  82.                 ' can be evenly divided multiple times.
  83.             ElseIf Denominator1 <> Denominator2 Then
  84.  
  85.                 'Creates the numerator of the answer
  86.                 NumeratorTemp1 = ((Numerator1 * Denominator2) + (Numerator2 * Denominator1))
  87.                 'Creates the denominator of the answer
  88.                 DenominatorTemp1 = (Denominator1 * Denominator2)
  89.  
  90.                 ' If denominators match run this
  91.             Else
  92.                 ' Adds the 2 numerators to create an answer
  93.                 NumeratorTemp1 = (Numerator1 + Numerator2)
  94.                 ' Keeps the matching denominators
  95.                 DenominatorTemp1 = (Denominator2)
  96.  
  97.             End If
  98.  
  99.             ' If the numerator is higher then the denominator_
  100.             ' and not a negitive #
  101.             If NumeratorTemp1 >= DenominatorTemp1 Then
  102.  
  103.                 ' Setting variables
  104.                 Dim LoopCountadd As Integer
  105.                 Dim TimesLoopedadd As Integer
  106.                 TimesLoopedadd = 0
  107.                 LoopCountadd = 0
  108.                 Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
  109.                     ' Subtracts the numerator from the denominator
  110.                     TimesLoopedadd = (DenominatorTemp1 - NumeratorTemp1)
  111.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  112.                     LoopCountadd += 1
  113.                     WholenumberTemp = (LoopCountadd)
  114.                     ' Makes the remainder = the new numerator answer
  115.                     NumeratorTemp1 = (NumeratorTemp1 - DenominatorTemp1)
  116.                 Loop
  117.  
  118.             ElseIf (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1 Then
  119.  
  120.                 ' Setting variables
  121.                 Dim LoopCountadd As Integer
  122.                 Dim TimesLoopedadd As Integer
  123.                 TimesLoopedadd = 0
  124.                 LoopCountadd = 0
  125.  
  126.                 ' A loop to subtract the denominator from the numerator until the numerator_
  127.                 ' is smaller than the denominator
  128.                 Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
  129.                     ' Subtracts the numerator from the denominator
  130.                     TimesLoopedadd = ((Math.Abs(NumeratorTemp1)) - DenominatorTemp1)
  131.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  132.                     LoopCountadd -= 1
  133.                     ' Makes WholeNumber = the total of times the loop occurred
  134.                     WholenumberTemp = (LoopCountadd)
  135.                     ' Makes the remainder = the new numerator answer
  136.                     NumeratorTemp1 = (Math.Abs(NumeratorTemp1) - DenominatorTemp1)
  137.  
  138.                     ' Goes back to see if the numerator is higher than the denominator
  139.                 Loop
  140.  
  141.             End If
  142.  
  143.             'Reduces the fraction
  144.             Dim reduce = GCD(NumeratorTemp1, DenominatorTemp1)
  145.             NumeratorTemp1 = (NumeratorTemp1 / reduce)
  146.             DenominatorTemp1 = (DenominatorTemp1 / reduce)
  147.             ' If something goes wrong then display a message box informing the user to re-enter
  148.             ' Thier fractions in a proper format. Clear all textboxes and focus on textbox1
  149.         Catch ex As Exception
  150.             MessageBox.Show("Please enter Fractions in the following formats only" _
  151.             & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
  152.             "#" & vbCr & "-#", "Error")
  153.             TextBox1.Clear()
  154.             TextBox2.Clear()
  155.             TextBox3.Clear()
  156.             TextBox1.Focus()
  157.  
  158.         End Try
  159.         ' These display the results
  160.         ' If the anwser is a whole # only
  161.         If NumeratorTemp1 = 0 Then
  162.             TextBox3.Text = "" & (WholenumberTemp)
  163.  
  164.             'If the answer is fractional only
  165.         ElseIf (WholenumberTemp) = Nothing Or (WholenumberTemp = 0) Then
  166.             TextBox3.Text = "" & (NumeratorTemp1) & "/" & (DenominatorTemp1)
  167.  
  168.             ' If both a whole # and a Fraction
  169.         Else
  170.             TextBox3.Text = "" & (WholenumberTemp) & " " & (NumeratorTemp1) & "/" & (DenominatorTemp1)
  171.  
  172.         End If
  173.  
  174.     End Sub
  175.  
This one is for SubFract Button

Expand|Select|Wrap|Line Numbers
  1.  Private Sub SubFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubFract.Click
  2.  
  3.         ' Attempt to run the following code
  4.         Try
  5.  
  6.             ' If there is only a fraction entered in box1
  7.             If (space1 <= 0) Then
  8.                 Numerator1 = TextBox1.Text.Substring(0, Slash1)
  9.                 Denominator1 = TextBox1.Text.Substring(Slash1 + 1)
  10.  
  11.                 ' If only a whole number is entered in box1
  12.             ElseIf (space1 <= 0) And (slash1 <= 0) Then
  13.                 Wholenumber1 = TextBox1.Text
  14.                 Numerator1 = (Wholenumber1 * 2)
  15.                 Denominator1 = 2
  16.  
  17.                 ' If both a whole number and a fraction are entered in box1
  18.             Else
  19.                 Wholenumber1 = TextBox1.Text.Substring(0, Space1)
  20.                 Numerator1 = TextBox1.Text.Substring((Space1 + 1), (Slash1 - (Space1 + 1)))
  21.                 Denominator1 = TextBox1.Text.Substring(Slash1 + 1)
  22.                 If Wholenumber1 < 0 Then
  23.                     Numerator1 = ((Wholenumber1 * Denominator1) - Numerator1)
  24.                 Else
  25.                     Numerator1 = (Wholenumber1 * Denominator1) + Numerator1
  26.                 End If
  27.  
  28.             End If
  29.  
  30.             ' If there is only a fraction entered in box2
  31.             If (space2 <= 0) And (slash2 > 0) Then
  32.                 Numerator2 = TextBox2.Text.Substring(0, Slash2)
  33.                 Denominator2 = TextBox2.Text.Substring(Slash2 + 1)
  34.                 Wholenumber2 = 0
  35.  
  36.                 ' If only a whole number is entered in box2
  37.             ElseIf (space2 <= 0) And (slash2 <= 0) Then
  38.                 Wholenumber2 = TextBox2.Text
  39.                 Numerator2 = (Wholenumber2 * 2)
  40.                 Denominator2 = 2
  41.  
  42.                 ' If both a whole number and a fraction are entered in box2
  43.             Else
  44.                 Wholenumber2 = TextBox2.Text.Substring(0, Space2)
  45.                 Numerator2 = TextBox2.Text.Substring((Space2 + 1), (Slash2 - (Space2 + 1)))
  46.                 Denominator2 = TextBox2.Text.Substring(Slash2 + 1)
  47.                 If Wholenumber2 < 0 Then
  48.                     Numerator2 = ((Wholenumber2 * Denominator2) - Numerator2)
  49.                 Else
  50.                     Numerator2 = (Wholenumber2 * Denominator2) + Numerator2
  51.                 End If
  52.  
  53.             End If
  54.  
  55.             'If denominators are different and denominator 1 can be divided into denominator 2 with_ 
  56.             ' a result of 2 and with no remainder. 
  57.             ' This is needed to fix errors that occur in the math formula_
  58.             ' "numtemp1 = ((numsub1 * densub2) - (numsub2 * densub1))" 
  59.             If Denominator1 < Denominator2 And Denominator2 \ Denominator1 = 2 _
  60.             And Denominator2 Mod Denominator1 = 0 Then
  61.  
  62.                 'Creates the numerator of the answer
  63.                 Numerator1 = (Numerator1 * 2)
  64.                 Denominator1 = (Denominator1 * 2)
  65.                 NumeratorTemp1 = (Numerator1 - Numerator2)
  66.                 'Creates the denominator of the answer
  67.                 DenominatorTemp1 = (Denominator2)
  68.  
  69.                 ' Does the same as above except for when denominator 2 can be divided into denominator_
  70.                 ' 1 with a result of 2
  71.             ElseIf Denominator1 > Denominator2 And Denominator2 \ Denominator1 = 2 _
  72.             And Denominator1 Mod Denominator2 = 0 Then
  73.  
  74.                 'Creates the numerator of the answer
  75.                 Numerator2 = (Numerator2 * 2)
  76.                 Denominator2 = (Denominator2 * 2)
  77.                 NumeratorTemp1 = (Numerator1 - Numerator2)
  78.                 'Creates the denominator of the answer
  79.                 DenominatorTemp1 = (Denominator1)
  80.  
  81.                 ' This formula works for unevenly divisible denominators or when the numerator_
  82.                 ' can be evenly divided multiple times.
  83.             ElseIf Denominator1 <> Denominator2 Then
  84.  
  85.                 'Creates the numerator of the answer
  86.                 NumeratorTemp1 = ((Numerator1 * Denominator2) - (Numerator2 * Denominator1))
  87.                 'Creates the denominator of the answer
  88.                 DenominatorTemp1 = (Denominator1 * Denominator2)
  89.  
  90.                 ' If denominators match run this
  91.             Else
  92.                 ' Adds the 2 numerators to create an answer
  93.                 NumeratorTemp1 = (Numerator1 - Numerator2)
  94.                 ' Keeps the matching denominators
  95.                 DenominatorTemp1 = (Denominator2)
  96.  
  97.             End If
  98.  
  99.             ' If the numerator is higher then the denominator_
  100.             ' and not a negitive #
  101.             If NumeratorTemp1 >= DenominatorTemp1 Then
  102.  
  103.                 ' Setting variables
  104.                 Dim LoopCountadd As Integer
  105.                 Dim TimesLoopedadd As Integer
  106.                 TimesLoopedadd = 0
  107.                 LoopCountadd = 0
  108.                 Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
  109.                     ' Subtracts the numerator from the denominator
  110.                     TimesLoopedadd = (DenominatorTemp1 - NumeratorTemp1)
  111.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  112.                     LoopCountadd += 1
  113.                     WholenumberTemp = (LoopCountadd)
  114.                     ' Makes the remainder = the new numerator answer
  115.                     NumeratorTemp1 = (NumeratorTemp1 - DenominatorTemp1)
  116.                 Loop
  117.  
  118.             ElseIf (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1 Then
  119.  
  120.                 ' Setting variables
  121.                 Dim LoopCountadd As Integer
  122.                 Dim TimesLoopedadd As Integer
  123.                 TimesLoopedadd = 0
  124.                 LoopCountadd = 0
  125.  
  126.                 ' A loop to subtract the denominator from the numerator until the numerator_
  127.                 ' is smaller than the denominator
  128.                 Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
  129.                     ' Subtracts the numerator from the denominator
  130.                     TimesLoopedadd = ((Math.Abs(NumeratorTemp1)) - DenominatorTemp1)
  131.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  132.                     LoopCountadd -= 1
  133.                     ' Makes WholeNumber = the total of times the loop occurred
  134.                     WholenumberTemp = (LoopCountadd)
  135.                     ' Makes the remainder = the new numerator answer
  136.                     NumeratorTemp1 = (Math.Abs(NumeratorTemp1) - DenominatorTemp1)
  137.  
  138.                     ' Goes back to see if the numerator is higher than the denominator
  139.                 Loop
  140.  
  141.             End If
  142.  
  143.             'Reduces the fraction
  144.             Dim reduce = GCD(NumeratorTemp1, DenominatorTemp1)
  145.             NumeratorTemp1 = (NumeratorTemp1 / reduce)
  146.             DenominatorTemp1 = (DenominatorTemp1 / reduce)
  147.  
  148.             ' If something goes wrong then display a message box informing the user to re-enter
  149.             ' Thier fractions in a proper format. Clear all textboxes and focus on textbox1
  150.         Catch ex As Exception
  151.             MessageBox.Show("Please enter Fractions in the following formats only" _
  152.             & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
  153.             "#" & vbCr & "-#", "Error")
  154.             TextBox1.Clear()
  155.             TextBox2.Clear()
  156.             TextBox3.Clear()
  157.             TextBox1.focus()
  158.         End Try
  159.         ' These display the results
  160.         ' If the anwser is a whole # only
  161.         If NumeratorTemp1 = 0 Then
  162.             TextBox3.Text = "" & (WholenumberTemp)
  163.  
  164.             'If the answer is fractional only
  165.         ElseIf (WholenumberTemp) = Nothing Or (WholenumberTemp = 0) Then
  166.             TextBox3.Text = "" & (NumeratorTemp1) & "/" & (DenominatorTemp1)
  167.  
  168.             ' If both a whole # and a Fraction
  169.         Else
  170.             TextBox3.Text = "" & (WholenumberTemp) & " " & (NumeratorTemp1) & "/" & (DenominatorTemp1)
  171.  
  172.         End If
  173.  
  174.     End Sub
  175.  
Jan 11 '08 #1
1 1612
For Multifract button

Expand|Select|Wrap|Line Numbers
  1. Private Sub MultiFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultiFract.Click
  2.  
  3.         ' Attempt to run the following code
  4.         Try
  5.  
  6.             ' If there is only a fraction entered in box1
  7.             If (space1 <= 0) Then
  8.                 Numerator1 = TextBox1.Text.Substring(0, Slash1)
  9.                 Denominator1 = TextBox1.Text.Substring(Slash1 + 1)
  10.  
  11.                 ' If only a whole number is entered in box1
  12.             ElseIf (space1 <= 0) And (slash1 <= 0) Then
  13.                 Wholenumber1 = TextBox1.Text
  14.                 Numerator1 = (Wholenumber1 * 2)
  15.                 Denominator1 = 2
  16.  
  17.                 ' If both a whole number and a fraction are entered in box1
  18.             Else
  19.                 Wholenumber1 = TextBox1.Text.Substring(0, Space1)
  20.                 Numerator1 = TextBox1.Text.Substring((Space1 + 1), (Slash1 - (Space1 + 1)))
  21.                 Denominator1 = TextBox1.Text.Substring(Slash1 + 1)
  22.                 If Wholenumber1 < 0 Then
  23.                     Numerator1 = ((Wholenumber1 * Denominator1) - Numerator1)
  24.                 Else
  25.                     Numerator1 = (Wholenumber1 * Denominator1) + Numerator1
  26.                 End If
  27.  
  28.             End If
  29.  
  30.             ' If there is only a fraction entered in box2
  31.             If (space2 <= 0) And (slash2 > 0) Then
  32.                 Numerator2 = TextBox2.Text.Substring(0, Slash2)
  33.                 Denominator2 = TextBox2.Text.Substring(Slash2 + 1)
  34.                 Wholenumber2 = 0
  35.  
  36.                 ' If only a whole number is entered in box2
  37.             ElseIf (space2 <= 0) And (slash2 <= 0) Then
  38.                 Wholenumber2 = TextBox2.Text
  39.                 Numerator2 = (Wholenumber2 * 2)
  40.                 Denominator2 = 2
  41.  
  42.                 ' If both a whole number and a fraction are entered in box2
  43.             Else
  44.                 Wholenumber2 = TextBox2.Text.Substring(0, Space2)
  45.                 Numerator2 = TextBox2.Text.Substring((Space2 + 1), (Slash2 - (Space2 + 1)))
  46.                 Denominator2 = TextBox2.Text.Substring(Slash2 + 1)
  47.                 If Wholenumber2 < 0 Then
  48.                     Numerator2 = ((Wholenumber2 * Denominator2) - Numerator2)
  49.                 Else
  50.                     Numerator2 = (Wholenumber2 * Denominator2) + Numerator2
  51.                 End If
  52.  
  53.             End If
  54.  
  55.             ' Multiplies the 2 numerators 
  56.             NumeratorTemp1 = (Numerator1 * Numerator2)
  57.             ' Multiplies the  denominators
  58.             DenominatorTemp1 = (Denominator2 * Denominator1)
  59.  
  60.             ' If the numerator is higher then the denominator_
  61.             ' and not a negitive #
  62.             If NumeratorTemp1 >= DenominatorTemp1 Then
  63.  
  64.                 ' Setting variables
  65.                 Dim LoopCountadd As Integer
  66.                 Dim TimesLoopedadd As Integer
  67.                 TimesLoopedadd = 0
  68.                 LoopCountadd = 0
  69.                 Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
  70.                     ' Subtracts the numerator from the denominator
  71.                     TimesLoopedadd = (DenominatorTemp1 - NumeratorTemp1)
  72.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  73.                     LoopCountadd += 1
  74.                     WholenumberTemp = (LoopCountadd)
  75.                     ' Makes the remainder = the new numerator answer
  76.                     NumeratorTemp1 = (NumeratorTemp1 - DenominatorTemp1)
  77.                 Loop
  78.                 ' Otherwise this loop is used
  79.             ElseIf (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1 Then
  80.  
  81.                 ' Setting variables
  82.                 Dim LoopCountadd As Integer
  83.                 Dim TimesLoopedadd As Integer
  84.                 TimesLoopedadd = 0
  85.                 LoopCountadd = 0
  86.  
  87.                 ' A loop to subtract the denominator from the numerator until the numerator_
  88.                 ' is smaller than the denominator
  89.                 Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
  90.                     ' Subtracts the numerator from the denominator
  91.                     TimesLoopedadd = ((Math.Abs(NumeratorTemp1)) - DenominatorTemp1)
  92.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  93.                     LoopCountadd -= 1
  94.                     ' Makes WholeNumber = the total of times the loop occurred
  95.                     WholenumberTemp = (LoopCountadd)
  96.                     ' Makes the remainder = the new numerator answer
  97.                     NumeratorTemp1 = (Math.Abs(NumeratorTemp1) - DenominatorTemp1)
  98.  
  99.                     ' Goes back to see if the numerator is higher than the denominator
  100.                 Loop
  101.  
  102.             End If
  103.  
  104.             'Reduces the fraction
  105.             Dim reduce = GCD(NumeratorTemp1, DenominatorTemp1)
  106.             NumeratorTemp1 = (NumeratorTemp1 / reduce)
  107.             DenominatorTemp1 = (DenominatorTemp1 / reduce)
  108.  
  109.             ' If something goes wrong then display a message box informing the user to re-enter
  110.             ' Thier fractions in a proper format. Clear all textboxes and focus on textbox1
  111.         Catch ex As Exception
  112.             MessageBox.Show("Please enter Fractions in the following formats only" _
  113.             & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
  114.             "#" & vbCr & "-#", "Error")
  115.             TextBox1.Clear()
  116.             TextBox2.Clear()
  117.             TextBox3.Clear()
  118.             TextBox1.Focus()
  119.         End Try
  120.         ' These display the results
  121.         ' If the anwser is a whole # only
  122.         If NumeratorTemp1 = 0 Then
  123.             TextBox3.Text = "" & (WholenumberTemp)
  124.  
  125.             'If the answer is fractional only
  126.         ElseIf (WholenumberTemp) = Nothing Or (WholenumberTemp = 0) Then
  127.             TextBox3.Text = "" & (NumeratorTemp1) & "/" & (DenominatorTemp1)
  128.  
  129.             ' If both a whole # and a Fraction
  130.         Else
  131.             TextBox3.Text = "" & (WholenumberTemp) & " " & (NumeratorTemp1) & "/" & (DenominatorTemp1)
  132.  
  133.         End If
  134.     End Sub
  135.  
finally the Divfract button

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.         'Attempt to run the following code
  4.         Try
  5.  
  6.             ' If there is only a fraction entered in box1
  7.             If (space1 <= 0) Then
  8.                 Numerator1 = TextBox1.Text.Substring(0, Slash1)
  9.                 Denominator1 = TextBox1.Text.Substring(Slash1 + 1)
  10.  
  11.                 ' If only a whole number is entered in box1
  12.             ElseIf (space1 <= 0) And (slash1 <= 0) Then
  13.                 Wholenumber1 = TextBox1.Text
  14.                 Numerator1 = (Wholenumber1 * 2)
  15.                 Denominator1 = 2
  16.  
  17.                 ' If both a whole number and a fraction are entered in box1
  18.             Else
  19.                 Wholenumber1 = TextBox1.Text.Substring(0, Space1)
  20.                 Numerator1 = TextBox1.Text.Substring((Space1 + 1), (Slash1 - (Space1 + 1)))
  21.                 Denominator1 = TextBox1.Text.Substring(Slash1 + 1)
  22.                 If Wholenumber1 < 0 Then
  23.                     Numerator1 = ((Wholenumber1 * Denominator1) - Numerator1)
  24.                 Else
  25.                     Numerator1 = (Wholenumber1 * Denominator1) + Numerator1
  26.                 End If
  27.  
  28.             End If
  29.  
  30.             ' If there is only a fraction entered in box2
  31.             If (space2 <= 0) And (slash2 > 0) Then
  32.                 Numerator2 = TextBox2.Text.Substring(0, Slash2)
  33.                 Denominator2 = TextBox2.Text.Substring(Slash2 + 1)
  34.                 Wholenumber2 = 0
  35.  
  36.                 ' If only a whole number is entered in box2
  37.             ElseIf (space2 <= 0) And (slash2 <= 0) Then
  38.                 Wholenumber2 = TextBox2.Text
  39.                 Numerator2 = (Wholenumber2 * 2)
  40.                 Denominator2 = 2
  41.  
  42.                 ' If both a whole number and a fraction are entered in box2
  43.             Else
  44.                 Wholenumber2 = TextBox2.Text.Substring(0, Space2)
  45.                 Numerator2 = TextBox2.Text.Substring((Space2 + 1), (Slash2 - (Space2 + 1)))
  46.                 Denominator2 = TextBox2.Text.Substring(Slash2 + 1)
  47.                 If Wholenumber2 < 0 Then
  48.                     Numerator2 = ((Wholenumber2 * Denominator2) - Numerator2)
  49.                 Else
  50.                     Numerator2 = (Wholenumber2 * Denominator2) + Numerator2
  51.                 End If
  52.  
  53.             End If
  54.  
  55.             ' Multiplies the 2 numerators 
  56.             NumeratorTemp1 = (Numerator1 * Denominator2)
  57.             ' Multiplies the  denominators
  58.             DenominatorTemp1 = (Denominator1 * Numerator2)
  59.  
  60.             ' If the numerator is higher then the denominator_
  61.             ' and not a negitive #
  62.             If NumeratorTemp1 >= DenominatorTemp1 And DenominatorTemp1 > 0 Then
  63.  
  64.                 ' Setting variables
  65.                 Dim LoopCountadd As Integer
  66.                 Dim TimesLoopedadd As Integer
  67.                 TimesLoopedadd = 0
  68.                 LoopCountadd = 0
  69.                 Do While (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1
  70.                     ' Subtracts the numerator from the denominator
  71.                     TimesLoopedadd = (DenominatorTemp1 - NumeratorTemp1)
  72.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  73.                     LoopCountadd += 1
  74.                     WholenumberTemp = (LoopCountadd)
  75.                     ' Makes the remainder = the new numerator answer
  76.                     NumeratorTemp1 = (NumeratorTemp1 - DenominatorTemp1)
  77.                 Loop
  78.                 ' Otherwise this loop is used
  79.             ElseIf (Math.Abs(NumeratorTemp1)) >= DenominatorTemp1 Then
  80.  
  81.                 ' Setting variables
  82.                 Dim LoopCountadd As Integer
  83.                 Dim TimesLoopedadd As Integer
  84.                 TimesLoopedadd = 0
  85.                 LoopCountadd = 0
  86.                 DenominatorTemp1 = (Math.Abs(DenominatorTemp1))
  87.                 ' A loop to subtract the denominator from the numerator until the numerator_
  88.                 ' is smaller than the denominator
  89.                 Do While (Math.Abs(NumeratorTemp1)) >= (DenominatorTemp1)
  90.                     ' Subtracts the numerator from the denominator
  91.                     TimesLoopedadd = ((Math.Abs(NumeratorTemp1)) - DenominatorTemp1)
  92.                     ' Counts up 1 for every time the numerator is subtracted from the denominator
  93.                     LoopCountadd -= 1
  94.                     ' Makes WholeNumber = the total of times the loop occurred
  95.                     WholenumberTemp = (LoopCountadd)
  96.                     ' Makes the remainder = the new numerator answer
  97.                     NumeratorTemp1 = (Math.Abs(NumeratorTemp1) - DenominatorTemp1)
  98.  
  99.                     ' Goes back to see if the numerator is higher than the denominator
  100.                 Loop
  101.  
  102.             End If
  103.  
  104.             'Reduces the fraction
  105.             Dim reduce = GCD(NumeratorTemp1, DenominatorTemp1)
  106.             NumeratorTemp1 = (NumeratorTemp1 / reduce)
  107.             DenominatorTemp1 = (DenominatorTemp1 / reduce)
  108.  
  109.             ' If something goes wrong then display a message box informing the user to re-enter
  110.             ' Thier fractions in a proper format. Clear all textboxes and focus on textbox1
  111.         Catch ex As Exception
  112.             MessageBox.Show("Please enter Fractions in the following formats only" _
  113.             & vbCr & "# #/#" & vbCr & "#/#" & vbCr & "-# #/#" & vbCr & "-#/#" & vbCr & _
  114.             "#" & vbCr & "-#", "Error")
  115.             TextBox1.Clear()
  116.             TextBox2.Clear()
  117.             TextBox3.Clear()
  118.             TextBox1.Focus()
  119.         End Try
  120.         ' These display the results
  121.         ' If the anwser is a whole # only
  122.         If NumeratorTemp1 = 0 Then
  123.             TextBox3.Text = "" & (WholenumberTemp)
  124.  
  125.             'If the answer is fractional only
  126.         ElseIf (WholenumberTemp) = Nothing Or (WholenumberTemp = 0) Then
  127.             TextBox3.Text = "" & (NumeratorTemp1) & "/" & (DenominatorTemp1)
  128.  
  129.             ' If both a whole # and a Fraction
  130.         Else
  131.             TextBox3.Text = "" & (WholenumberTemp) & " " & (NumeratorTemp1) & "/" & (DenominatorTemp1)
  132.  
  133.         End If
  134.  
  135.     End Sub
  136.  
Feel free to use the code and if anyone has any suggestions on how to shorten or clean it up more please send me a pm

Thanks all,
James
Jan 11 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mickel Grönroos | last post by:
Hi, I have a Tkinter.Canvas of variable width. Is there a standard way of asking the canvas which parts of it that is visible? I.e. on the horizontal scale, I would like to know at what fraction...
2
by: Stedak | last post by:
C# I am writing a function that allows the user to enter fractions as answers (i.e. how many asprin tablets for a dosage?). I have the first half of the Regex figured out but don't know how to...
33
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half,...
5
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have the...
2
by: Mori | last post by:
Hi, Can someone supply a code example of displaying a string with a fractional part, say 5 and 7 16ths. I cannot find an example of how to use the Encoding object (if that is what you use). ...
15
by: farah727rash | last post by:
Hi everyone, I have this problem and I don't know what's wrong with my program. I am trying to enter my two variables height and weight as fraction numbers. I declared them as float and also as...
4
by: Bob | last post by:
Hi All, Was wondering if in C# there is an elegant way of displaying and or calculating fractions. The case: we have an app that measures/slices dices etc and all our internal measures and...
2
by: grmorris | last post by:
Hey all, I'm working on a program that takes two fractions as input and performs an operation on them (+,-,*,/). I've overloaded all the operators and written a function to reduce the result to...
0
by: Paddy | last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html) There seems to be a problem/difference in calculating with the new fractions module when comparing Python 26rc2 and 30rc1...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.