Calculator Logic

  • Thread starter Thread starter Steven Smith
  • Start date Start date
S

Steven Smith

I was flicking through the windows accesories programs
for some inspiration for todays vb challenge when I came
accross the calculator program & thought that looks easy
I could do that. However it hasn't turned out to be as
easy as I first anticipated due to a calculators strange
logic

I done a bit of research on the web to see what I could
come up with and I found this following page which
outlines the ideas behind a calculator utility:

http://cyberlearn.fau.edu/cafolla/VBDotNet/calculator1.htm

from that page I've came up with the following code so
far.

performing the previous mathamatical operation when the
current operator is selected however has me got beat, the
rest of the app works as this page suggests, I'm sure its
quite simple but I'm just not getting my head around it
at the minute as this is my first solo project working
outside course books...

anyway here's the code:


\\\
'declare form level variables
Dim mblnFirstDigit As Boolean = True
Dim mstrDisplay As String
Dim mstrLastOperator As String = "+"
Dim mintRunningTotal, mintCurrentAmount, mintDigit As
Integer

Private Sub FileExitMenuItem_Click(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles FileExitMenuItem.Click
Me.Close()
End Sub

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

Private Sub DigitButtons_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ThreeButton.Click, EightButton.Click, TwoButton.Click,
NineButton.Click, FourButton.Click, SixButton.Click,
FiveButton.Click, OneButton.Click, SevenButton.Click
'determine which numeric button has been pressed
If sender Is OneButton Then
mintDigit = 1
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is TwoButton Then
mintDigit = 2
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is ThreeButton Then
mintDigit = 3
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is FourButton Then
mintDigit = 4
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is FiveButton Then
mintDigit = 5
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is SixButton Then
mintDigit = 6
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is SevenButton Then
mintDigit = 7
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is EightButton Then
mintDigit = 8
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is NineButton Then
mintDigit = 9
Call IsThisFirstDigit(sender, mintDigit)
Else 'sender is zero button
mintDigit = 0
Call IsThisFirstDigit(sender, mintDigit)
End If

End Sub

Private Sub IsThisFirstDigit(ByVal sender, ByVal
mintDigit)
'determine whether or not button clicked is the
first digit
If mblnFirstDigit = False Then
'add number to the display
Me.DisplayLabel.Text = Me.DisplayLabel.Text &
mintDigit
Else
'make display equal the number
Me.DisplayLabel.Text = mintDigit
End If
'now its not the first digit
mblnFirstDigit = False
End Sub

Private Function Last_Operator(ByVal sender, ByRef
mstrLastOperator)
'determine the last operator to be clicked in
order to perform correct sum
Select Case True
Case sender Is Me.DivideButton
mstrLastOperator = "/"
Case sender Is Me.MultiplyButton
mstrLastOperator = "*"
Case sender Is Me.SubtractButton
mstrLastOperator = "-"
Case sender Is Me.AddButton
mstrLastOperator = "+"
End Select
Return (mstrLastOperator)
End Function

Private Sub OperatorButtons_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles DivideButton.Click, SubtractButton.Click,
AddButton.Click, MultiplyButton.Click
'store the current value on the display to a
variable
mintCurrentAmount = Val(Me.DisplayLabel.Text)

'make the next digit entered the first in the
string
mblnFirstDigit = True

'call Private Function to determine last operator
here

'determine which operator has been clicked
Select Case True
Case sender Is Me.DivideButton
mintRunningTotal = mintRunningTotal /
mintCurrentAmount
Case sender Is Me.MultiplyButton
mintRunningTotal = mintRunningTotal *
mintCurrentAmount
Case sender Is Me.SubtractButton
mintRunningTotal = mintRunningTotal -
mintCurrentAmount
Case Else 'sender is me.addbutton
mintRunningTotal = mintRunningTotal +
mintCurrentAmount
End Select
End Sub

Private Sub CancelButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles CancelButton.Click
Me.DisplayLabel.Text = 0
mblnFirstDigit = True
End Sub

Private Sub EqualsButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles EqualsButton.Click
'display the running total
Me.DisplayLabel.Text = mintRunningTotal
End Sub
End Class

any suggestions or guidance is as always appreciated

Regards Steve...
 
Revised code for calculator application, now incorporates
all features of windows standard calculator but still
can't figure out this calculation routine, now my head
hurts ! :)

can someone please give me a shove in the right
direction...

here's the page with ideas behind the calculator logic
again:

http://cyberlearn.fau.edu/cafolla/VBDotNet/calculator1.htm


\\\
'declare form level variables
Dim mblnFirstDigit As Boolean = True
Dim mstrDisplay As String
Dim mstrLastOperator As String = "+"
Dim mintRunningTotal, mintCurrentAmount, mintDigit As
Integer

Private Sub FileExitMenuItem_Click(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles FileExitMenuItem.Click
Me.Close()
End Sub

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

Private Sub ProcessDigitButtons(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles OneButton.Click, TwoButton.Click,
ThreeButton.Click, _
FourButton.Click, FiveButton.Click,
SixButton.Click, _
SevenButton.Click, EightButton.Click,
NineButton.Click, _
ZeroButton.Click
'determine which numeric button has been pressed
If sender Is Me.OneButton Then
mintDigit = 1
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is Me.TwoButton Then
mintDigit = 2
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is Me.ThreeButton Then
mintDigit = 3
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is Me.FourButton Then
mintDigit = 4
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is Me.FiveButton Then
mintDigit = 5
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is Me.SixButton Then
mintDigit = 6
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is Me.SevenButton Then
mintDigit = 7
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is Me.EightButton Then
mintDigit = 8
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is Me.NineButton Then
mintDigit = 9
Call IsThisFirstDigit(sender, mintDigit)
Else 'sender is me.ZeroButton
mintDigit = 0
Call IsThisFirstDigit(sender, mintDigit)
End If

'validate against multi zero input at beggining
of string
If Me.DisplayLabel.Text.StartsWith("00") Then
Me.DisplayLabel.Text = 0
mblnFirstDigit = True
End If

End Sub

Private Sub IsThisFirstDigit(ByVal sender, ByVal
mintDigit)
'determine whether or not button clicked is the
first digit
If mblnFirstDigit = False Then
'add number to the display
Me.DisplayLabel.Text = Me.DisplayLabel.Text &
mintDigit
Else
'make display equal the number
Me.DisplayLabel.Text = mintDigit
End If
'now its not the first digit
mblnFirstDigit = False
End Sub

Private Function Last_Operator(ByVal sender, ByRef
mstrLastOperator)
'determine the last operator to be clicked in
order to perform correct sum
Select Case True
Case sender Is Me.DivideButton
mstrLastOperator = "/"
Case sender Is Me.MultiplyButton
mstrLastOperator = "*"
Case sender Is Me.SubtractButton
mstrLastOperator = "-"
Case sender Is Me.AddButton
mstrLastOperator = "+"
End Select
Return (mstrLastOperator)
End Function

Private Sub ProcessOperatorButtons(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles DivideButton.Click, SubtractButton.Click,
AddButton.Click, MultiplyButton.Click
'store the current value on the display to a
variable
mintCurrentAmount = Val(Me.DisplayLabel.Text)

'make the next digit entered the first in the
string
mblnFirstDigit = True

'call Private Function to determine last operator
here

'determine which operator has been clicked
Select Case True
Case sender Is Me.DivideButton
mintRunningTotal = mintRunningTotal /
mintCurrentAmount
Case sender Is Me.MultiplyButton
mintRunningTotal = mintRunningTotal *
mintCurrentAmount
Case sender Is Me.SubtractButton
mintRunningTotal = mintRunningTotal -
mintCurrentAmount
Case sender Is Me.AddButton
mintRunningTotal = mintRunningTotal +
mintCurrentAmount
Case sender Is Me.SquareButton
Case Else 'sender is me.PercentButton
End Select
End Sub

Private Sub ProcessCancelButtons(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles CancelButton.Click, CancelEButton.Click
'cancel entire sum or current calculation only
If sender Is Me.CancelButton Then
Me.DisplayLabel.Text = 0
mintRunningTotal = 0
mblnFirstDigit = True
Else 'if sender is me.CanelEButton
Me.DisplayLabel.Text = 0
mblnFirstDigit = True
End If
End Sub

Private Sub EqualsButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles EqualsButton.Click
'display the running total
Me.DisplayLabel.Text = mintRunningTotal
End Sub

Private Sub BackSpaceButton_Click(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles BackSpaceButton.Click
'declare variable
Dim intX As Integer
Dim strString As String
'assign values
strString = Me.DisplayLabel.Text
intX = strString.Length - 1
're-display the string minus the last character
Me.DisplayLabel.Text = strString.Remove(intX, 1)
End Sub

Private Sub ProcessMemoryFunctions(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles MemoryClearButton.Click,
MemoryRecallButton.Click, MemorySaveButton.Click, _
MemoryAddButton.Click
'declare variables
Static intMemory As Integer
'perform memory storage & retrieval functions
Select Case True
Case Me.MemoryClearButton Is sender
intMemory = 0
Case Me.MemoryRecallButton Is sender
Me.DisplayLabel.Text = intMemory
Case Me.MemorySaveButton Is sender
intMemory = Val(Me.DisplayLabel.Text)
Case Me.MemoryAddButton Is sender
intMemory = intMemory + Val
(Me.DisplayLabel.Text)
End Select

End Sub

Private Sub PlusMinusButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles PlusMinusButton.Click
'declare variables
Dim intNegPos As Integer
Static blnSwitch As Boolean = True
'assign values
intNegPos = Val(Me.DisplayLabel.Text)
'switch between positive and negative numbers
If intNegPos > 0 AndAlso blnSwitch = True Then
Me.DisplayLabel.Text = intNegPos-intNegPos*2
blnSwitch = False

ElseIf intNegPos < 0 AndAlso blnSwitch = False
Then
Me.DisplayLabel.Text = intNegPos-intNegPos*2
blnSwitch = True
End If

End Sub
End Class
///

regards steve...
 
Steven,

try Scientific & Currency calculator from
http://www.delphipages.com/result.cfm?ID=3482

Fully functional expression calculator (binary application)
MAIN FEATURES:
includes Excel OLE graphing
includes Periodic Table of the Elements
supports complex numbers
supports currency numbers
supports financial functions
calculates mathematical expression on the fly
includes common math functions and constants (125 built-in)
user can define own functions, variables and constants
supports "if" and "while" functions
supports boolean expressions
includes customizable unit conversion
includes system of linear equations solver
expression can be symbolic differentiated
Windows and Linux platform
update service
 
Hi Stephen,

There was a thread about your Hangman game in which I mentioned the
benefits of going through your code on paper with a pen and NO COMPUTER.

Maybe you didn't get to read that post. If not, may I suggest that you
have a look?

I know what's wrong with your program but I'm not going to tell you. ;-)

But I <will> give you some hints.

Dr Ray talks about LastOp. I didn't see one in your program.

What's the difference between hitting an operator, eg Add, and hitting
Equals?
What should be the same about these events?

What should be different between Equals and Cancel?
What should be the same?

Have a work at it and come back.

I hope that no-one else gives you definitive answers. Often I give whole
routines to people in answer to a query. But your situation is that you are
learning programming, not coding. I want to watch as you learni the problem
solving techniques. You are off to a good start having learnt to ask for help.
Now you must learn to apply hints.

And learn to forego the screen in favour of paper. :-)

Best wishes,
Fergus
 
[email protected]:

Here's another hint if you was a more efficient approach. Get a book on
data structures or look online for articles about expression trees,
postfix/infix notation, and stacks.

Hope this gives a little inspiration.

Chris
 
Hi Chris,

Steven's calculator isn't an expression evaluator - it's the (Accumulator
[+-* /] Operand) type.

It has a stack of one! ;-)

Regards,
Chris
 
Back
Top