# Simple Sientific Calculator

Page 1 of 1

## 2 Replies - 7851 Views - Last Post: 10 December 2007 - 07:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=39241&amp;s=463ec917ae703e9796ce197edb2a5840&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sanvel

Reputation: 0
• Posts: 3
• Joined: 07-December 07

# Simple Sientific Calculator

Posted 10 December 2007 - 02:39 PM

Hello Everyone,

As promised, I am posting my code for a simple scientific calculator. This calculator is supposed to allow the following functions to be calculated in addition to the basic calculation such Addition, Subtraction, Division, and Multiplication:

o Tangent
o Square Root
o Sine
o Cosine
o Absolute Value

So far I have been able to code the calculator to handle the above Functions, but I am having problems, believe it or not, with the Basic math calculations. I can figure out how to make the calculator perform:

o Subtraction
o Division
o and Multiplication

I can’t seem to get the +, -, *, /, and = buttons to do what they are supposed to; any suggestions?

Santos

Below is my Code:

```___________________________________________________________
Option Explicit On
Option Strict On
Imports System.Math

Public Class FrmSientificCal
Dim dblDisplay As Double
Dim dblResult As Double
Dim strLastOperator As String = ""

Private Sub subUpdateDisplay(ByVal strInput As String)

If strLastOperator = "" Then
Me.txtDisplay.Text = Me.txtDisplay.Text & strInput
Else
Me.txtDisplay.Text = strInput
End If

strLastOperator = ""

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.txtDisplay.Text = ""

End Sub

Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged

End Sub

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

Call subUpdateDisplay("1")

End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click

Call subUpdateDisplay("2")

End Sub

Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click

Call subUpdateDisplay("3")

End Sub

Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click

Call subUpdateDisplay("4")

End Sub

Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click

Call subUpdateDisplay("5")

End Sub

Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click

Call subUpdateDisplay("6")

End Sub

Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click

Call subUpdateDisplay("7")

End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click

Call subUpdateDisplay("8")

End Sub

Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click

Call subUpdateDisplay("9")

End Sub

Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click

Call subUpdateDisplay("0")

End Sub

Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click

Call subUpdateDisplay("")

End Sub

Private Sub btnPlusMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlusMinus.Click

Call subUpdateDisplay("")

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

Me.txtDisplay.Text = ""

End Sub

[My Problem Starts Here]

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

Call subUpdateDisplay("")

End Sub

Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click

Call subUpdateDisplay("")

End Sub

Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

Call subUpdateDisplay("")

End Sub

Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click

Call subUpdateDisplay("")

End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

End Sub

[My Problem Ends Here]

Private Sub btnTangent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTangent.Click

Try
strLastOperator = "Tan"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Tan(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)

Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, You must enter a numeric value"

End Try

End Sub

Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click

Try
strLastOperator = "sqrt"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Sqrt(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)

Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, you must enter a numeric value"

End Try

End Sub

Private Sub btnSine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSine.Click

Try
strLastOperator = "Sin"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Sin(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)

Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, you must enter a numeric value"

End Try

End Sub

Private Sub btnCosine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCosine.Click

Try
strLastOperator = "Cos"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Cos(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)

Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, you must enter a numeric value"

End Try

End Sub

Private Sub btnAbsValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbsValue.Click

Try
strLastOperator = "Abs"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Abs(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)

Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, you must enter a numeric value"

End Try

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

End

End Sub

End Class

```

This post has been edited by PsychoCoder: 10 December 2007 - 07:53 PM

Is This A Good Question/Topic? 0

## Replies To: Simple Sientific Calculator

### #2 DilutedImage

Reputation: 15
• Posts: 646
• Joined: 20-November 06

## Re: Simple Sientific Calculator

Posted 10 December 2007 - 06:05 PM

It's been a while since I've done any VB, and I don't have VB on this system, so this may not work. It should at least get you on the right track though:

```Option Explicit On
Option Strict On
Imports System.Math

Public Class FrmSientificCal
Dim dblDisplay As Double
Dim dblResult As Double
'Dim strLastOperator As String = ""
Dim strPreviousValue As String = ""
Dim strOperator As String = ""

Private Sub subUpdateDisplay(ByVal strInput As String)
If strOperator = "" Then
Me.txtDisplay.Text = Me.txtDisplay.Text & strInput
Else
Me.txtDisplay.Text = strInput
End If
strLastOperator = ""
End Sub

Private Sub equate()
Select Case strOperator
Case "+":
Me.txtDisplay.Text = Str(Val(strPreviousValue) + Val(Me.txtDisplay.Text))
Case "-":
Me.txtDisplay.Text = Str(Val(strPreviousValue) - Val(Me.txtDisplay.Text))
Case "*":
Me.txtDisplay.Text = Str(Val(strPreviousValue) * Val(Me.txtDisplay.Text))
Case "/":
Me.txtDisplay.Text = Str(Val(strPreviousValue) / Val(Me.txtDisplay.Text))
End Select
strPreviousValue = ""
strOperator = ""
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.txtDisplay.Text = ""
End Sub

Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged
End Sub

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Call subUpdateDisplay("1")
End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
Call subUpdateDisplay("2")
End Sub

Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
Call subUpdateDisplay("3")
End Sub

Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
Call subUpdateDisplay("4")
End Sub

Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
Call subUpdateDisplay("5")
End Sub

Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
Call subUpdateDisplay("6")
End Sub

Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
Call subUpdateDisplay("7")
End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
Call subUpdateDisplay("8")
End Sub

Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
Call subUpdateDisplay("9")
End Sub

Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
Call subUpdateDisplay("0")
End Sub

Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
Call subUpdateDisplay("")
End Sub

Private Sub btnPlusMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlusMinus.Click
Call subUpdateDisplay("")
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Me.txtDisplay.Text = ""
End Sub

'[My Problem Starts Here]

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Call equate()
End Sub

Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
If strOperator <> "" Then
Call equate()
End If
strPreviousValue = Me.txtDisplay.Text
strOperator = "/"
End Sub

Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
If strOperator <> "" Then
Call equate()
End If
strPreviousValue = Me.txtDisplay.Text
strOperator = "*"
End Sub

Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
If strOperator <> "" Then
Call equate()
End If
strPreviousValue = Me.txtDisplay.Text
strOperator = "-"
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If strOperator <> "" Then
Call equate()
End If
strPreviousValue = Me.txtDisplay.Text
strOperator = "+"
End Sub

'[My Problem Ends Here]

Private Sub btnTangent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTangent.Click
Try
'strLastOperator = "Tan"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Tan(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, You must enter a numeric value"
End Try
End Sub

Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click
Try
'strLastOperator = "sqrt"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Sqrt(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, you must enter a numeric value"
End Try
End Sub

Private Sub btnSine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSine.Click
Try
'strLastOperator = "Sin"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Sin(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, you must enter a numeric value"
End Try
End Sub

Private Sub btnCosine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCosine.Click
Try
'strLastOperator = "Cos"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Cos(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, you must enter a numeric value"
End Try
End Sub

Private Sub btnAbsValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbsValue.Click
Try
'strLastOperator = "Abs"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Abs(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, you must enter a numeric value"
End Try
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End Sub

End Class
```

And to give credit where due, the idea came from this page.

### #3 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: Simple Sientific Calculator

Posted 10 December 2007 - 07:57 PM

This is actually a VB.Net question so I'm moving it to that form. Also, I wrote a tutorial on creating a calculator and this calculator has quite a bit of the functionality you're looking for such as sin, square root, etc, might be worth it to read through it:

Calculator in VB.Net