473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calculator Logic

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/VB...alculator1.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...

Nov 20 '05 #1
5 6851
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/VB...alculator1.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...
Nov 20 '05 #2
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


"Steven Smith" <St**********@emailaccount.com> wrote in message news:<88****************************@phx.gbl>...
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/VB...alculator1.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...

Nov 20 '05 #3
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
Nov 20 '05 #4
"Steven Smith" <St**********@emailaccount.com> wrote in news:889501c37ed0
$c****************@phx.gbl:

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

Nov 20 '05 #5
Hi Chris,

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

It has a stack of one! ;-)

Regards,
Chris
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: aleksander.helgaker | last post by:
I'm learning to program python on my Mac and I'd like some help. I followed a tutorial which showed how to make a calculator for working out the area of a shape. E.g. the area of a circal is...
10
by: collegestudent | last post by:
hello there, I was wondering if anyone could give me some advice on where to start on a project for college where I have to design and create a hand-held calculator simulation in Visual...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
6
by: psylencer | last post by:
Hi guys, I'm working on my first ASP web site. The idea of the site is to calculate which mobile phone plan is right for you. The idea is that the user answers a few questions about their...
2
by: senthilit2006 | last post by:
hi friends now only i start to learn vb.net n now i completed calculator program in vb.net but i dont know the concept for how to act the backspace button event..tel me some logic for tat.........
2
by: traineeirishprogrammer | last post by:
I want to develop a calculator in javascript, like the one in microsoft; I cant get around the logic of it. Here is my problem. If I type in a number then I have to wait for an operator then I...
2
by: deezle | last post by:
Hello, I am trying to get my calculator GUI to +,-,* and /. I have got all of them to work except my division. I was wondering if someone could helpme figure out the problem. Any input would help...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
Curtis Rutland
by: Curtis Rutland | last post by:
Have any of you ever used a Reverse Polish Notation calculator? I did in high school. It was easily the best calculator ever (the HP=32SII). RPN is great, because you don't have to use parenthesis....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.