Jump to content

Visual basic GUI Scientific Calculator BODMAS

Hi guys
I'm trying to make a GUI calculator in Visual Basic that need to use brackets so that it follows BODMAS. I'm struggling because it I'm using buttons for each input and not a text input. Do any of you guys have any advice on how I should try to think about it? I haven't had much luck using lists. Do you know of any good tutorials that I could follow?
I tried asking for help on stack overflow but they answered with let me google that for you type things.

Public Class Form1    Dim clearDisplay As Boolean    Dim clearLine As Boolean    Dim clearTop As Boolean    Public Calc As New Stack(Of Double)    Dim mem As Double    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnEnt.Click        Calc.Push(lblDisplay.Text)        clearDisplay = True        lblLine.Text = Calc.Peek()    End Sub    Private Sub Digit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _   Handles bttn1.Click, bttn0.Click, bttn2.Click, bttn3.Click, bttn4.Click, bttn5.Click, bttn6.Click, _   bttn7.Click, bttn8.Click, bttn9.Click        If clearDisplay Then            lblDisplay.Text = ""            clearDisplay = False        End If        lblDisplay.Text = lblDisplay.Text + sender.text    End Sub    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnCln.Click        lblDisplay.Text = ""        lblTop.Text = ""        lblLine.Text = Calc.Peek    End Sub    'Addition    Private Sub bttnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPlus.Click        lblTop.Text = Calc.Peek()        clearLine = True        lblLine.Text = lblDisplay.Text        Dim Num1 As Double = lblLine.Text        Dim Num2 As Double        Dim Result As Double        Num2 = Calc.Peek()        Result = Num2 + Num1        lblDisplay.Text = Result        Calc.Push(Result)    End Sub    'Subtraction    Private Sub bttnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMinus.Click        lblTop.Text = Calc.Peek()        clearLine = True        lblLine.Text = lblDisplay.Text        Dim Num1 As Double = lblLine.Text        Dim Num2 As Double        Dim Result As Double        Num2 = Calc.Peek()        Result = Num2 - Num1        lblDisplay.Text = Result        Calc.Push(Result)    End Sub    'Multiplication    Private Sub bttnTimes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnTimes.Click        lblTop.Text = Calc.Peek()        clearLine = True        lblLine.Text = lblDisplay.Text        Dim Num1 As Double = lblLine.Text        Dim Num2 As Double        Dim Result As Double        Num2 = Calc.Peek()        Result = Num2 * Num1        lblDisplay.Text = Result        Calc.Push(Result)    End Sub    'Divison    Private Sub bttnDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnDiv.Click        lblTop.Text = Calc.Peek()        clearLine = True        lblLine.Text = lblDisplay.Text        Dim Num1 As Double = lblLine.Text        Dim Num2 As Double        Dim Result As Double        Num2 = Calc.Peek()        Result = Num2 / Num1        lblDisplay.Text = Result        Calc.Push(Result)    End Sub    Private Sub bttnClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnClr.Click        lblDisplay.Text = ""        lblLine.Text = ""        lblTop.Text = ""        Calc.Clear()    End Sub    Private Sub bttnMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMS.Click        Mem = lblDisplay.Text    End Sub    Private Sub bttnMR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMR.Click        lblDisplay.Text = Mem    End Sub    Private Sub bttnCL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnCL.Click        Mem = 0    End Sub    'Squared    Private Sub bttnPow2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPow2.Click        Dim value As Integer = Val(lblDisplay.Text)        lblDisplay.Text = (value ^ 2)        Calc.Push(lblDisplay.Text)    End Sub    Private Sub bttnPow3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPow3.Click        Dim value As Integer = Val(lblDisplay.Text)        lblDisplay.Text = (value ^ 3)        Calc.Push(lblDisplay.Text)    End Sub        Private Function Factoral(ByVal n As Integer)        If n = 0 Then            Factoral = 1        Else            Factoral = n * Factoral(n - 1)        End If    End Function    Private Sub bttnFac_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnFac.Click        Dim n As Integer        n = Integer.Parse(lblDisplay.Text)        lblDisplay.Text = Factoral(n)        Calc.Push(lblDisplay.Text)    End Sub    ''To the power N    'Private Sub bttnPowN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPowN.Click    '    Dim n As Double = lblDisplay.Text    '    lblDisplay.Text = ""    '    lblLine.Text = n    'End Sub    'Square root'    Private Sub bttnSqr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnSqr.Click        If lblDisplay.Text < 0 Then            MsgBox("Can't work out the square root of " & _                    "of a negative number")        Else            lblDisplay.Text = Math.Sqrt(Val(lblDisplay.Text))        End If    End Sub    'Log'    Private Sub bttnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnLog.Click                    lblDisplay.Text = Math.Log(lblDisplay.Text)        clearDisplay = True    End Sub    'Cos'    Private Sub bttnCos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnCos.Click        lblDisplay.Text = Math.Cos(Val(lblDisplay.Text))    End Sub    'Tan'    Private Sub bttnTan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnTan.Click        lblDisplay.Text = Math.Tan(Val(lblDisplay.Text))    End Sub    'Sin'    Private Sub bttnSin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnSin.Click        lblDisplay.Text = Math.Sin(Val(lblDisplay.Text))    End SubEnd Class


Here I have managed to use stacks, but do not know how to implement BODMAS :/

Link to comment
Share on other sites

Link to post
Share on other sites

Hi guys

I'm trying to make a GUI calculator in Visual Basic that need to use brackets so that it follows BODMAS. I'm struggling because it I'm using buttons for each input and not a text input. Do any of you guys have any advice on how I should try to think about it? I haven't had much luck using lists. Do you know of any good tutorials that I could follow?

I tried asking for help on stack overflow but they answered with let me google that for you type things.

 

// didnt want to quote the whole thing

There is a:

code

Please use it, it will make it more easy to help you :)

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to comment
Share on other sites

Link to post
Share on other sites

<p>Make it so you have a textbox where your values are entered to.</p>
<p> </p>
<p>So if you press a button it will look something like:</p>
<pre class="_prettyXprint _lang- _linenums:1">
Private Sub cmd_x_Click()
'Put the value x into the txtNUMBER text box
txtNUMBER = txtNUMBER + "x"
End Sub
</pre>
<p>Then you just add your functions (*,-,+, tan etc.)</p>
<p>And the user can again (if needed) enter another number.</p>
<p> </p>
<p>Source: <a href="http://www.dreamincode.net/forums/topic/50628-create-a-calculator/">http://www.dreamincode.net/forums/topic/50628-create-a-calculator/</a></p>
<p> </p>
<p>As for the BODMAS, i think that the math in VB.NET takes care of that, but i am not sure.</p>

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to comment
Share on other sites

Link to post
Share on other sites

I have figured out how to do it logically, how would you use VB to use each number either side of an operator?

 

2 + 3 + 4 * 5

0 1 2 3 4 5 6

 

Operators are always odd numbers in the list :p

I need to figure out how to search the list, find the operator with higher precedence and then continue until the problem is solved.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×