Imbedded for/next?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Semajthewise
    New Member
    • Nov 2007
    • 38

    Imbedded for/next?

    Hi all. I'm starting on my next part of my teach myself vb program. What I am trying to do is on button click open a textfile showing the math as it would be done long hand. I started writing the code and now I think There might be a better way of doing it than what I have been doing. Here's the code as far as it goes.
    [code=vbnet]

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
    ' Declarations
    Dim sw As StreamWriter
    Dim sr As StreamReader
    Dim path1 As String = "Showme.Txt "
    Dim longest As Integer
    Dim length1 As Integer
    Dim length2 As Integer
    Dim length3 As Integer
    Dim length4 As Integer
    Dim length5 As Integer
    Dim line1 As String = AddBox1.Text
    Dim line2 As String = AddBox2.Text
    Dim line3 As String = AddBox3.Text
    Dim line4 As String = AddBox4.Text
    Dim line5 As String = AddBox5.Text
    Dim AnsLine As String = "_"
    Dim Inta As Integer
    Dim Intb As Integer
    Dim Intc As Integer
    Dim Intd As Integer
    Dim Inte As Integer
    Dim intf As Integer

    length1 = line1.Length
    length2 = line2.Length
    length3 = line3.Length
    length4 = line4.Length
    length5 = line5.Length
    Dim addspace As String = " "
    ' Deletes file if exists then recreates a new file to write to
    If File.Exists(pat h1) Then
    File.Delete(pat h1)
    End If
    sw = File.CreateText (path1)
    sw.WriteLine("W elcome to Show Me")
    sw.WriteLine()
    sw.WriteLine("H ere we will take the numbers you have entered and show")
    sw.WriteLine("y ou how to add them to get an answer.")
    sw.WriteLine()
    sw.WriteLine("F irst lets get the sumbers you entered")
    sw.WriteLine()
    'Checks to see if an entry was made in each textbox and writes
    'to file only if true. Does this for all 5 textboxes
    If length1 > 0 Then
    sw.WriteLine((V al(AddBox1.Text )))
    End If
    If length2 > 0 Then
    sw.WriteLine((V al(AddBox2.Text )))
    End If
    If length3 > 0 Then
    sw.WriteLine((V al(AddBox3.Text )))
    End If
    If length4 > 0 Then
    sw.WriteLine((V al(AddBox4.Text )))
    End If
    If length5 > 0 Then
    sw.WriteLine((V al(AddBox5.Text )))
    End If

    ' More text
    sw.WriteLine()
    sw.WriteLine("A nd line up the last digits.")
    sw.WriteLine()

    ' Checks the length of each string and sets "longest"
    ' to the length of the longest string
    If length1 >= length2 Then
    longest = length1
    Else
    longest = length2
    End If

    If longest >= length3 Then
    longest = longest
    Else
    longest = length3
    End If

    If longest >= length4 Then
    longest = longest
    Else
    longest = length4
    End If

    If longest >= length5 Then
    longest = longest
    Else
    longest = length5
    End If

    ' Insert a space at the beginning of the string for each
    ' instance of a string until all strings are the same length
    ' this will line up the last digits of each entry
    Do Until length1 = longest
    line1 = line1.Insert(0, addspace)
    length1 = line1.Length
    Loop
    'Writes the new string if it is numeric
    If (IsNumeric(line 1)) Then
    sw.WriteLine(li ne1)
    End If
    Do Until length2 = longest
    line2 = line2.Insert(0, addspace)
    length2 = line2.Length
    Loop
    If (IsNumeric(line 2)) Then
    sw.WriteLine(li ne2)
    End If
    Do Until length3 = longest
    line3 = line3.Insert(0, addspace)
    length3 = line3.Length
    Loop
    If (IsNumeric(line 3)) Then
    sw.WriteLine(li ne3)
    End If
    Do Until length4 = longest
    line4 = line4.Insert(0, addspace)
    length4 = line4.Length
    Loop
    If (IsNumeric(line 4)) Then
    sw.WriteLine(li ne4)
    End If
    Do Until length5 = longest
    line5 = line5.Insert(0, addspace)
    length5 = line5.Length
    Loop
    If (IsNumeric(line 5)) Then
    sw.WriteLine(li ne5)
    End If

    ' Creates a line of proper length to place under the math problem
    Do Until AnsLine.Length = longest
    AnsLine = AnsLine.Insert( 0, "_")
    Loop

    ' More text
    sw.WriteLine(An sLine)
    sw.WriteLine()
    sw.WriteLine("N ow that we have lined up the last digit of each number")
    sw.WriteLine("w e can begin to add our numbers together.")
    sw.WriteLine("F irst we'll the last numbers, or the numbers farthest right.")
    sw.WriteLine()


    If line1.Length > 0 And (IsNumeric(line 1)) Then
    Inta = line1.Substring (line1.Length - 1, 1)
    sw.WriteLine((V al(Inta)))
    End If
    If line2.Length > 0 And (IsNumeric(line 2)) Then
    Intb = line2.Substring (line2.Length - 1, 1)
    sw.WriteLine((V al(Intb)))
    End If
    If line3.Length > 0 And (IsNumeric(line 3)) Then
    Intc = line3.Substring (line3.Length - 1, 1)
    sw.WriteLine((V al(Intc)))
    End If
    If line4.Length > 0 And (IsNumeric(line 4)) Then
    Intd = line4.Substring (line4.Length - 1, 1)
    sw.WriteLine((V al(Intd)))
    End If
    If line5.Length > 0 And (IsNumeric(line 5)) Then
    Inte = line5.Substring (line5.Length - 1, 1)
    sw.WriteLine((V al(Inte)))
    End If

    intf = ((Val(Inta)) + (Val(Intb)) + (Val(Intc)) + (Val(Intd)) + (Val(Inte)))
    sw.WriteLine()
    sw.WriteLine("N ow let's add up the numbers and see what our answer is.")
    sw.WriteLine()
    sw.WriteLine((V al(intf)))

    If intf > 9 Then
    sw.WriteLine()
    sw.WriteLine("S ince our answer is over 9 we cant simply write the answer")
    sw.WriteLine("B ecause if we did our final answer would be wrong.")
    sw.WriteLine("W e will need to do what is called Carrying.")
    sw.WriteLine()
    sw.WriteLine("T his means that we are going to take the first number of our")
    sw.WriteLine("a nswer and carry it over to the next row and place it on top")
    sw.WriteLine("o f it like so.")

    Dim Carry As String = intf
    Dim CarryLen As Integer = Carry.Length
    Do Until CarryLen = longest
    Carry = Carry.Insert(0, addspace)
    CarryLen = Carry.Length
    Loop

    Dim carrydrop As String = Carry.Substring (longest - 1, 1)
    Dim droplen As Integer
    Do Until droplen = longest
    carrydrop = carrydrop.Inser t(0, addspace)
    droplen = carrydrop.Lengt h
    Loop

    Dim Carry1 As String = Carry.Substring (longest - 2, 1)
    Dim Carry1Len As Integer
    Do Until Carry1Len = (longest - 1)
    Carry1 = Carry1.Insert(0 , addspace)
    Carry1Len = Carry1.Length
    Loop

    sw.WriteLine()
    sw.WriteLine(Ca rry1)
    sw.WriteLine(li ne1)
    sw.WriteLine(li ne2)
    sw.WriteLine(li ne3)
    sw.WriteLine(li ne4)
    sw.WriteLine(li ne5)
    sw.WriteLine(An sLine)
    sw.WriteLine()
    sw.WriteLine("N ext we'll place the second number under the row we added")
    sw.WriteLine()
    sw.WriteLine(Ca rry1)
    sw.WriteLine(li ne1)
    sw.WriteLine(li ne2)
    sw.WriteLine(li ne3)
    sw.WriteLine(li ne4)
    sw.WriteLine(li ne5)
    sw.WriteLine(An sLine)
    sw.WriteLine(ca rrydrop)
    sw.WriteLine()
    sw.WriteLine("N ow we need to add up the next line dont forget to add")
    sw.WriteLine("t he number we carried over.")
    sw.WriteLine()

    If line1.Length > 0 And (IsNumeric(line 1)) Then
    Inta = line1.Substring (line1.Length - 2, 1)
    sw.WriteLine((V al(Inta)))
    End If
    If line2.Length > 0 And (IsNumeric(line 2)) Then
    Intb = line2.Substring (line2.Length - 2, 1)
    sw.WriteLine((V al(Intb)))
    End If
    If line3.Length > 0 And (IsNumeric(line 3)) Then
    Intc = line3.Substring (line3.Length - 2, 1)
    sw.WriteLine((V al(Intc)))
    End If
    If line4.Length > 0 And (IsNumeric(line 4)) Then
    Intd = line4.Substring (line4.Length - 2, 1)
    sw.WriteLine((V al(Intd)))
    End If
    If line5.Length > 0 And (IsNumeric(line 5)) Then
    Inte = line5.Substring (line5.Length - 2, 1)
    sw.WriteLine((V al(Inte)))
    End If
    sw.WriteLine()
    sw.WriteLine("A nd our answer is")
    sw.WriteLine()
    intf = (Val(carry1)) + ((Val(Inta)) + (Val(Intb)) + (Val(Intc)) + (Val(Intd)) + (Val(Inte)))
    sw.WriteLine(in tf)

    Else

    Dim Carry As String = intf
    Dim CarryLen As Integer = Carry.Length
    Do Until CarryLen = longest
    Carry = Carry.Insert(0, addspace)
    CarryLen = Carry.Length
    Loop

    Dim carrydrop As String = Carry.Substring (longest - 1, 1)
    Dim droplen As Integer
    Do Until droplen = longest
    carrydrop = carrydrop.Inser t(0, addspace)
    droplen = carrydrop.Lengt h
    Loop

    Dim Carry1 As String = Carry.Substring (longest - 2, 1)
    Dim Carry1Len As Integer
    Do Until Carry1Len = (longest - 1)
    Carry1 = Carry1.Insert(0 , addspace)
    Carry1Len = Carry1.Length
    Loop
    sw.WriteLine("S ince our answer is less than 10 we would write the answer")
    sw.WriteLine("b elow the numbers we just added")
    sw.WriteLine()
    sw.WriteLine(li ne1)
    sw.WriteLine(li ne2)
    sw.WriteLine(li ne3)
    sw.WriteLine(li ne4)
    sw.WriteLine(li ne5)
    sw.WriteLine(An sLine)
    sw.WriteLine(ca rrydrop)
    sw.WriteLine()
    End If

    sw.Flush()
    sw.Close()



    Diagnostics.Pro cess.Start(path 1)


    End Sub
    [/code]
    Now is the way I am going the best way or could i possibly make it shorter and easier by doing some kind of imbedded for/next style loop? If so could someone give me an idea how, or possibley a snippit of some other code that does simular to what I am trying that I can use for reference? Or even a whole new direction that may be better suited.

    Thanks in advance,
    James
Working...