I am trying to make a vb program that reads a text file and manipulated the data within.
For instance, my text file contains the following information:

27
rt
3
+
5
*
2
/

4

2
^
3

This is all the information my text file contains. I am trying to read the first line, store the line into a variable of some sort, read the next two lines and operate them against the first, then store as a variable and then loop until it goes through all the math involved and then displays the output of 8. ( 27 rt 3 = 3 | 3 + 5 = 8 | 8 * 2 = 16 | 16 / 4 = 4 | 4 - 2 = 2 | 2 ^ 3 = 8 )

Now, The code i have i can get it to read through the document. My code is as follows:

Imports System.IO

Module Module1

    Sub Main()
    Dim list As New List(Of String)

    Using r As StreamReader = New StreamReader("c:\myMathProblem.txt")
        Dim line As String

        line = r.ReadLine

        Do While (Not line Is Nothing)
        list.Add(line)
        Console.WriteLine(line)
        line = r.ReadLine
        Loop
    End Using

    End Sub

End Module

This runs through the entire document prints the whole document then exit. I cant figure out how to slow it down store the lines as variable so i can operate against them. Any help will be appreciated.

Hi,

I have added all the data to the ListBox, and compiled it this way...

Check the Code:

Private Sub Command1_Click()
    Dim i As Integer
    Dim x As Single
    Dim y As Single
    Dim TStr As String
    i = 0
    x = List1.List(i)
    y = 0
    Do
        i = i + 1
        TStr = Trim(List1.List(i))
        Select Case TStr
            Case "+"
                i = i + 1
                y = Val(List1.List(i))
                x = x + y
            Case "-"
                i = i + 1
                y = Val(List1.List(i))
                x = x - y
            Case "*"
                i = i + 1
                y = Val(List1.List(i))
                x = x * y
            Case "/"
                i = i + 1
                y = Val(List1.List(i))
                If y = 0 Then
                    MsgBox "Division By Zero....", vbCritical, "Check sequence"
                    Exit Sub
                End If
                x = x / y
            Case "^"
                i = i + 1
                y = Val(List1.List(i))
                x = x ^ y
            Case "rt"
                i = i + 1
                y = Val(List1.List(i))
                x = x ^ (1 / y)
        End Select
    Loop Until i >= (List1.ListCount - 1)
    MsgBox "Result is : " & x
    '
End Sub

Here, you have to take care that, there is a sign between all the numbers..
Extra signs Need to be included in Select List as required.. accordingly..

Regards
Veena

Awesome, I like the way that works. Unfortunately i need to grab the data from the text file, unless i have a way of importing that data from the txt file to the list box in question this wont quite work out for me.

In case your wondering why i need to use the txt file is because i was told i need to, dont quite get it myself but thats what is wanted.

I am going to try to see if i can use a lot of what you wrote to try to do it, just reading from that darn text file is the hardest part for me, since i need to read, store what i read then read more and operate.

Reading the text file is actually fairly simple if you use the File class.

Something like this works:

I assumed the last part should have a '-' instead of a space

4
-
2
^
3


    Imports System.IO

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim x As Single
        Dim y As Single
        Dim TStr As String
        Dim Strlist As List(Of String) = File.ReadAllLines("c:\myMathProblem.txt").ToList           
        i = 0
        x = Strlist(i)            
        y = 0
        Do
            i = i + 1
            TStr = Trim(Strlist(i))
            i = i + 1
            Select Case TStr
                Case "+"
                    y = Val(Strlist(i))
                    x = x + y
                Case "-"
                    y = Val(Strlist(i))
                    x = x - y
                Case "*"
                    y = Val(Strlist(i))
                    x = x * y
                Case "/"
                    y = Val(Strlist(i))
                    If y = 0 Then
                        MsgBox "Division By Zero....", vbCritical, "Check sequence"
                        Exit Sub
                    End If
                    x = x / y
                Case "^"
                    y = Val(Strlist(i))
                    x = x ^ y
                Case "rt"
                    y = Val(Strlist(i))
                    x = x ^ (1 / y)
            End Select
        Loop Until i >= (Strlist.Count - 1)
        MsgBox "Result is : " & x
        '
    End Sub

On a side note, it can make things less confusing if you don't use keywords for variable names

Thanks for all the replies they are very helpful.

The only thing i dont understand is how i am supposed to integrate this code into mine. As you can see from the top i was working in my main module, this appears to be some sort of other module. Am i supposed to define it as its own module and call it in the main?

This should do it:

    Imports System.IO
    Module Module1
        Sub Main()
            Dim y As Single = 0
            Dim TStr As String = ""
            Dim Strlist As List(Of String) = File.ReadAllLines("c:\myMathProblem.txt").ToList
            Dim x As Single = Strlist(0)
            For i = 1 To Strlist.Count - 2 Step 2
                TStr = Trim(Strlist(i))
                Select Case TStr
                    Case "+"
                        y = Val(Strlist(i + 1))
                        x += y
                    Case "-"
                        y = Val(Strlist(i + 1))
                        x -= y
                    Case "*"
                        y = Val(Strlist(i + 1))
                        x *= y
                    Case "/"
                        y = Val(Strlist(i + 1))
                        If y = 0 Then
                            MsgBox("Division By Zero....", vbCritical, "Check sequence")
                            Exit Sub
                        End If
                        x /= y
                    Case "^"
                        y = Val(Strlist(i + 1))
                        x ^= y
                    Case "rt"
                        y = Val(Strlist(i + 1))
                        x = x ^ (1 / y)
                End Select
            Next
            MsgBox("Result is : " & x)
        End Sub
    End Module

The last code I showed you, can be used right in your module.

I know that was a double post i flaged it to be deleted. Thanks so much it works very well i appreciate it ! You saved me a huge headache thats been building for days. Thanks again!

Hi,

Good that, it is working now...
One more checking, you need to make...

 Case "rt"
    y = Val(Strlist(i + 1))
    If y = 0 Then
        Msgbox "Division By zero...."
        Exit Sub
    End If
    x = x ^ (1 / y)

Regards
Veena

Glad I could help, please remember to mark this solved thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.