VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Simple Calculator

by Sagar Khatale (1 Submission)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 27th July 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Simple Calculator

API Declarations


Dim opd(3) As Double, tno As Double
Dim opr(3) As String
Dim top1 As Integer, otop As Integer
Dim str As String, nstr As String, tstr As String, ostr As String, tstr1 As String
Dim DotPressed As Boolean

Rate Simple Calculator



Private Sub cmd_Click(Index As Integer)
str = cmd(Index).Caption
nstr = nstr + str
Text1.Text = nstr
opd(top1) = Val(nstr)
End Sub

Private Sub cmddecimal_Click()
DotPressed = True

'if "." is prssed, integer number is not entered
'and fractional number has to be entered then
'1. set number to "0"
'2. place decimal point after "0"
'3. append the digit of fractional number after "."

If (nstr = "") Then
    Text1.Text = "0"
    nstr = "0"
End If

nstr = nstr + cmddecimal.Caption
Text1.Text = nstr
opd(top1) = Val(nstr)
End Sub

Private Sub cmdopr_Click(Index As Integer)
Calculate (Index)
End Sub

Private Sub Form_Load()
DotPressed = False
str = ""
nstr = ""
tstr = ""
ostr = ""
top1 = 0
otop = 0
End Sub

Public Sub Calculate(ind As Integer)
DotPressed = False
nstr = ""
str = ""
top1 = top1 + 1
tstr = cmdopr(ind).Caption
opr(otop) = tstr

If (otop = 1) Then
    Select Case opr(0)
        Case "+"
            tno = opd(0) + opd(1)
            MoveData (tno)
        Case "-"
            tno = opd(0) - opd(1)
            MoveData (tno)
        Case "*"
            tno = opd(0) * opd(1)
            MoveData (tno)
        Case "/"
            tno = opd(0) / opd(1)
            MoveData (tno)
    End Select
    
    If (opr(0) = "=") Then
        Text1.Text = opd(0)
        top1 = 0
        otop = 0
        str = ""
        nstr = ""
    End If
Else
    otop = otop + 1
End If
End Sub

Public Sub MoveData(tno As Double)
'Display result in the TextField
Text1.Text = tno

'Move data from [top] to [top-1] position into Stack
opd(0) = tno
opr(0) = opr(1)
top1 = 1
otop = 1
End Sub


Download this snippet    Add to My Saved Code

Simple Calculator Comments

No comments have been posted about Simple Calculator. Why not be the first to post a comment about Simple Calculator.

Post your comment

Subject:
Message:
0/1000 characters