Simple Sientific Calculator

Having problems with the basic operators...

Page 1 of 1

2 Replies - 6767 Views - Last Post: 10 December 2007 - 07:57 PM Rate Topic: -----

#1 sanvel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 Addition
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



Mod Edit: Please use code tags when posting your code ==> :code:

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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1634
  • View blog
  • 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

Hope that tutorial is able to answer some of your questions
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1