Results 1 to 3 of 3

Thread: [RESOLVED] how to use regex or an array to display some output from an arithmetic input?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    7

    Resolved [RESOLVED] how to use regex or an array to display some output from an arithmetic input?

    Hi guys!! I've to make a program that reads an arithmetical sentence from a textbox, e.g 12.3*34+4*5-3 and then on another textbox identify and display what is the content of the first textbox, for example taking the last ex:

    If I have this input:
    12.3*34+4*5-3

    The output should look like this:

    <number>12.3
    <operator> *
    <number> 34
    <operator> +
    <number> 4
    <operator> *
    <number> 5
    <operator> -
    <number> 3

    This program should only read this operators like (+,-,*,/).

    I'm have in mind 2 ways of doing it, one with regex and the other taking the first sentence, put it on an array, then trim it, and somehow get the values of the array and identify if it is a number or a decimal number and if it's an operator and then display it.

    The difficult part or the part that i can't conceptualize is how to implement it with regex (it should be easier with a regex format than storing it on a array).

    I write a bit of code, I used an array to store and then to print the numbers and I used the split function to separate the math operator, but I dunno how to print the operators and the tags.

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim Sentencia As String = TextBox1.Text
    3.         Dim i As Integer
    4.         Dim arryTextBox() As String
    5.  
    6.         TextBox2.Clear()
    7.  
    8.         arryTextBox = Sentencia.Split("+", "-", "*", "/")
    9.  
    10.         For i = 0 To UBound(arryTextBox)
    11.  
    12.             Dim Array As String = arryTextBox(i)
    13.  
    14.             'MsgBox(arryTextBox(i)) 'this msgbox works it prints the values of the array
    15.             TextBox2.Text = TextBox2.Text & Array & vbCrLf
    16.         Next i
    17.        
    18.     End Sub

    Btw, I also have the code to validate the input on the textbox ::wink::

    Any help will be very appreciated,

    Thanks in advanced!!

  2. #2
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    Re: how to use regex or an array to display some output from an arithmetic input?

    vb.net Code:
    1. If Me.txtEquation.Text <> String.Empty Then
    2.     Me.txtSeperated.Text = "<number> "
    3.  
    4.     For Each EquChar In Me.txtEquation.Text
    5.         Dim OperatorMatch As Match = Regex.Match(EquChar, "(\+|-|\*|/)")
    6.  
    7.         If OperatorMatch.Success Then
    8.             Me.txtSeperated.Text &= vbCrLf & "<operator> "
    9.         End If
    10.         Me.txtSeperated.Text &= EquChar
    11.         If OperatorMatch.Success Then
    12.             Me.txtSeperated.Text &= vbCrLf & "<number> "
    13.         End If
    14.     Next
    15. End If

    Put this in your button click event; this probably isn't the best way to solve your problem, but it works.

    Also, you shouldn't use "Array" as a variable name as it is also a Class and doing so could produce errors.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    7

    Re: how to use regex or an array to display some output from an arithmetic input?

    OMG WOW!! Thank so much man!! I'll have to study a lot about regex functions. You made my day!!

    Thanks for ur advice, i wont write that again ^^

    A+++++

    Thanks again!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width