PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 I can't multiply more than 2 values help? read desc-VBForums
Results 1 to 7 of 7

Thread: I can't multiply more than 2 values help? read desc

  1. #1

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Question I can't multiply more than 2 values help? read desc

    Code:
    Public Class Form1
        Dim firstnum As Decimal
        Dim secondnum As Decimal
        Dim operations As Integer
        Dim operator_selector As Boolean = False
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Select()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ""
            TextBox1.Select() 
        End Sub 'this code is for clearing
    
        Private Sub btnplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplus.Click
            firstnum = Val(TextBox1.Text)
            TextBox1.Text = ""
            operator_selector = True
            operations = 1
            TextBox1.Select()
        End Sub 'this code is for Add (+)
    
        Private Sub btnminus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnminus.Click
            firstnum = Val(TextBox1.Text)
            TextBox1.Text = ""
            operator_selector = True
            operations = 2
            TextBox1.Select()
        End Sub 'this code is for subtract
    
        Private Sub btnmnozenje_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmnozenje.Click
    
            firstnum = Val(TextBox1.Text)
            TextBox1.Text = ""
            operator_selector = True
            operations = 3
            TextBox1.Select()
        End Sub 'this code is for multiply
    
        Private Sub btndelenje_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelenje.Click
            firstnum = Val(TextBox1.Text)
            TextBox1.Text = ""
            operator_selector = True
            operations = 4
            TextBox1.Select()
        End Sub 'this code is for divide
    
        Private Sub btnresult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnresult.Click
            If operator_selector = True Then
                secondnum = Val(TextBox1.Text)
                If operations = 1 Then
                    TextBox1.Text = firstnum + secondnum
                ElseIf operations = 2 Then
                    TextBox1.Text = firstnum - secondnum
                ElseIf operations = 3 Then
                    TextBox1.Text = firstnum * secondnum
                Else
                    If secondnum = 0 Then
                        TextBox1.Text = "Cannot divide by 0."
                    Else
                        TextBox1.Text = firstnum / secondnum / firstnum
                    End If
                End If
                TextBox1.Select()
                TextBox1.Text = TextBox1.Text.Replace(",", ".")
                TextBox1.SelectionStart = TextBox1.SelectionStart + 16
                TextBox1.SelectionLength = 0
            End If
            operator_selector = False
        End Sub
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode.Equals(Keys.Enter) Then
                btnresult.PerformClick()
            ElseIf e.KeyCode.Equals(Keys.Multiply) Then
                btnmnozenje.PerformClick()
            ElseIf e.KeyCode.Equals(Keys.Divide) Then
                btndelenje.PerformClick()
            ElseIf e.KeyCode.Equals(Keys.Subtract) Then
                btnminus.PerformClick()
            ElseIf e.KeyCode.Equals(Keys.Add) Then
                btnplus.PerformClick()
            End If
        End Sub
    End Class
    if i calculate like this
    3+3+3 i got result 6
    if i calculate like this
    3+3 and now if i click to show result textbox will show result 6 and after that if i calculate like this 6+3 i got result 9
    the point is i want more operations at one time like 3+3+2*2/4 to get result 4, with my code if i try to calculate like this 3+3+2*2/4 i will get result 6

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,293

    Re: I can't multiply more than 2 values help? read desc

    It's a much harder problem, to be sure. As long as you have just the one operator, then your calculation is much simpler. Once you have more operators, then operator precedence comes into play. After all 3+3+2*2/4 does not equal 4, it equals 7. The way you get four is to ignore operator precedence and just calculate from left to right, but that's not the right answer.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: I can't multiply more than 2 values help? read desc

    And now what i need to edit..? I hope that it's not whole code

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,293

    Re: I can't multiply more than 2 values help? read desc

    Yeah, it would be. What you are talking about creating is an equation solver, which is not a simple thing to do. There are a couple people here who have written them, but it's not a simple change. Effectively, you have to find all the operators, then do them in the order of multiplications, then divisions, then addition, then subtraction. If you include parentheses, it gets even more complicated.
    My usual boring signature: Nothing

  5. #5
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: I can't multiply more than 2 values help? read desc

    You may want to consider adding NCalc to your project you would have to download it and add the reference then you can import it with Imports Ncalc

    After doing that you can use codes like this.
    vb.net Code:
    1. Dim x As New Expression("3*2+2*3")
    2.         MsgBox(x.Evaluate)
    This is just a basic example it can do harder equations as well.
    Last edited by Vexslasher; Nov 9th, 2016 at 08:19 PM.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,618

    Re: I can't multiply more than 2 values help? read desc

    If you interested in looking at code of an implementation, here is the link to one from dday9, and a link to a tutorial on his implementation.

    http://www.vbforums.com/showthread.p...sion-Evaluator

    http://www.vbforums.com/showthread.p...sion-Evaluator

    I'm sure there are others, somewhere, but this is the one I've seen pop up enough to remember to look for.

  7. #7

    Thread Starter
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: I can't multiply more than 2 values help? read desc

    it is much harder than I thought..

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width