Hi everyone.

I know how to make a calculator, with basics and a little more complex functions, that's not my problem. My problem relies in that, the only way I can make a calculator is by 2 terms at a time, i.e: I can't make this operation (12 + 21 - 3 * 5 / 3) , I can only make (12 + 21) And the result of this multiplied by five and so on.. I know there are ways so that when i press the equal button, all of the input is processed and the result is given, but I Dont know how! Can any on `please help me? I've coded a lot of calculators for prcatice, but I wan't to go further with this one!

Help would be really appreciated!

You can make an array of char like Expression(63).

Then check if any of the characters in the array is / by using a for loop. If it finds /, it should stores its location in a variable. So for [5 + 16 + 7 * 14 / 28] it will store 15 (including the spaces and starting from 0). Then make two for loops that check the numbers before and after the / and store them in separate variables. You'll need to use the value 15 you stored earlier in these for loops (their starting points). Then perform 14 / 28 and rewrite the array Expression as 5 + 16 + 7 * 0.5.

Then repeat and loop for another /. If you can't find one, move on to * and do the same and so on. Also, you'll need to look for parentheses as well as (2/3)/4 is not the same as 2/(3/4).

Dim Terminos() As String
        Dim i As Short = 0
        Dim OperatorIndex As Short = 0

        For Each Letter In txtOperaciones.Text.ToCharArray

            Select Case Letter

                Case "+"

                    ReDim Preserve Terminos(i)
                    Terminos(i) = txtOperaciones.Text.Substring(OperatorIndex, txtOperaciones.Text.IndexOf("+"))
                    OperatorIndex = txtOperaciones.Text.IndexOf("+") + 1
                    MessageBox.Show(Terminos(i))
                    i = i + 1
                    Exit Select


                Case "-"

                    ReDim Preserve Terminos(i)
                    Terminos(i) = txtOperaciones.Text.Substring(OperatorIndex, txtOperaciones.Text.IndexOf("-"))
                    OperatorIndex = txtOperaciones.Text.IndexOf("-") + 1
                    MessageBox.Show(Terminos(i))
                    i = i + 1


                Case "*"

                    ReDim Preserve Terminos(i)
                    Terminos(i) = txtOperaciones.Text.Substring(OperatorIndex, txtOperaciones.Text.IndexOf("*"))
                    OperatorIndex = txtOperaciones.Text.IndexOf("*") + 1
                    i = i + 1
                    Exit Select

                Case "/"

                    ReDim Preserve Terminos(i)
                    Terminos(i) = txtOperaciones.Text.Substring(OperatorIndex, txtOperaciones.Text.IndexOf("/"))
                    OperatorIndex = txtOperaciones.Text.IndexOf("/") + 1
                    i = i + 1
                    Exit Select
            End Select


        Next

Alright, here is my problem. I've done the loop thing so far, and when i make an input like the following: (2 + 1 + 3 + 5 + 4) everything is perfect. Each term before the operator gets saved into the array. But when I have an input like the following: ( 6 + 8 - 2), which means, when I combine operators, the array terminos() is saved as follows: (Terminos(0) = 6) but (Terminos(1) = 8 - 2) instead of being only 8. Note that this only happens when combining operators!

Please help me!

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.