PDA

View Full Version : Unable to give a finish to a calculator


techyayush
Jul 12, 2008, 11:04 AM
Hi I am ayush

I have made a calculator that performs every arithmetic operation but the problem is that suppose I want to add 1+1+1. so I stored the value of first1 in x(variable) then I click on + then added another 1 in variable in y then when I again hit plus button the textbox should show value 2 but it doesn't then when I again click on 1 and press = then it shows 3.But I want to show that as soon as I second time hit plus then answer should be on textbox.


To make this calculator I've assigned value of "a" for every arithmetic operation and using these values I come to the result. Every time the result shown is correct but not showing of result as soon as arithmetic operator's button is a big problem.. So help me

znet2705zc
Jul 27, 2008, 08:09 AM
Look at this code. (a sample of calculator)

Option Explicit
Dim strPreviousValue As String
Dim strOperator As String
Dim blnDot As Boolean
Dim strMemory As String
Dim blnDisplay As Boolean
Dim strAuthor As String
Private Sub Form_Load()
lblDisplay.Caption = "0"
strPreviousValue = ""
strMemory = "
strMemory = "
strOperator = ""
blnDot = False
blnDisplay = True

End Sub
Private Sub cmdNumber_Click(Index As Integer)
If blnDisplay = True Then
If blnDot = False And lblDisplay.Caption = "
strOperator = " Then
lblDisplay.Caption = ""
blnDot = False
blnDisplay = True

End Sub
Private Sub cmdNumber_Click(Index As Integer)
If blnDisplay = True Then
If blnDot = False And lblDisplay.Caption = "." Then
lblDisplay.Caption = "0."
blnDisplay = True
blnDot = True
End If
End Sub
Private Sub cmdCE_Click()
If Timer1.Enabled = True Then Timer1.Enabled = False
blnDot = False
lblDisplay.Caption = "
End If
lblDisplay.Caption = lblDisplay.Caption + cmdNumber(Index).Caption
Else
strPreviousValue = lblDisplay.Caption
lblDisplay.Caption = cmdNumber(Index).Caption
blnDisplay = True
End If
End Sub
Private Sub cmdDot_Click()
If blnDisplay = True Then
If blnDot = False Then
blnDot = True
lblDisplay.Caption = lblDisplay.Caption + "
strOperator = ""
strPreviousValue = "
End If
Exit Sub
End If
If blnDisplay = False And blnDot = False Then
lblDisplay.Caption = "
'strMemory = "
blnDisplay = True
blnDot = True
End If
End Sub
Private Sub cmdCE_Click()
If Timer1.Enabled = True Then Timer1.Enabled = False
blnDot = False
lblDisplay.Caption = "
End Sub
Private Sub cmdOperator_Click(Index As Integer)
blnDot = False
blnDisplay = False
If Len(strOperator) = 1 Then
Call math
strOperator = cmdOperator(Index).Caption
Else
strOperator = cmdOperator(Index).Caption
strPreviousValue = lblDisplay.Caption
End If
End Sub
Private Function math()
On Error Go to errorHandler
Select Case strOperator
Case "+"
strOperator = "-"
strPreviousValue = "x"
'strMemory = "/"
End Sub
Private Sub cmdOperator_Click(Index As Integer)
blnDot = False
blnDisplay = False
If Len(strOperator) = 1 Then
Call math
strOperator = cmdOperator(Index).Caption
Else
strOperator = cmdOperator(Index).Caption
strPreviousValue = lblDisplay.Caption
End If
End Sub
Private Function math()
On Error GoTo errorHandler
Select Case strOperator
Case "Error "
lblDisplay.Caption = Val(lblDisplay.Caption) + Val(strPreviousValue)
Case ""
strPreviousValue = "
lblDisplay.Caption = Val(strPreviousValue) - Val(lblDisplay.Caption)
Case "
blnDot = False
blnDisplay = False
End Function
Private Sub cmdIsEqualTo_Click()
Call math
strOperator = ""
lblDisplay.Caption = Val(lblDisplay.Caption) * Val(strPreviousValue)
Case "M+"
lblDisplay.Caption = Val(strPreviousValue) / Val(lblDisplay.Caption)
End Select
Exit Function
errorHandler:
lblDisplay.Caption = "M-"
strOperator = "MR"
strPreviousValue = "MC"
strMemory = "
blnDot = False
blnDisplay = False
End Function
Private Sub cmdIsEqualTo_Click()
Call math
strOperator = "
lblDisplay.Caption = "
If Len(lblDisplay.Caption) <= 19 Then
lblDisplay.FontSize = 15
Else
lblDisplay.FontSize = 10
End If
End Sub
Private Sub cmdPlusOrMinus_Click()
lblDisplay.Caption = Val(lblDisplay.Caption) - (Val(lblDisplay.Caption) + Val(lblDisplay.Caption))
End Sub
Private Sub cmdSqrt_Click()
lblDisplay.Caption = Sqr(Val(lblDisplay.Caption))
End Sub
Private Sub cmdMemory_Click(Index As Integer)
If Timer1.Enabled = False Then
Select Case cmdMemory(Index).Caption
Case "
End Select
End If
End Sub
Private Sub Timer1_Timer()
strAuthor = Mid$(strAuthor, 2, Len(strAuthor) - 1) + Left(strAuthor, 1)
lblDisplay.Caption = Left(strAuthor, 18)
End Sub

KISS
Jul 27, 2008, 01:12 PM
It's so much easier doing the calculations using RPN (Reverse Polish Notation) and doing a Algebraic to RPN conversion

1 1 1 + + or 1 1 + 1 + is so much easier to do than 1+1+1.

Been there, done that.

In the second example, the code becomes:
Push 1
Push 1
Add
Push 1
Add

Use a stack at least 4 deep and an operator operates on the last two elements of the stack.

The algebraic BS has to deal with operator precedence and parenthsis.