calculator coding

a calculator coding, that xcepts numberz n gives sollutions! just

Page 1 of 1

3 Replies - 8241 Views - Last Post: 01 February 2010 - 01:15 AM Rate Topic: ****- 1 Votes

#1 baby greenz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-January 07

calculator coding

Post icon  Posted 06 January 2007 - 06:21 PM

hi,
im ria...im studyin software developement in UK.

we are doin a work in class...but i kinda seem 2 be stuck..!!

we are makin a calculator in vb that wud except input from the user n also ask the user to define an operation(+ - * /).
i knw the normal one......wer the program takes 2 or 3 numberz n the user preses a desired command button(Add,multi,div.sub) n get the result.

eg. commandadd.caption = val(txt1.txt) + val(txt2.txt)

but wat v are required to do is....create a program tht wud take thenumberz n gv the ans(like above) but also gv an option 2 the user to continue frm ther...lik inreal calcz!
so he can add 2 more numberz to the result!!
sumthin like tht.....!
one more question...can this be done in parameterz???

i hav done a bit on this.....but its in the coll computerz...! so if u want i can get that by monday...but its not much!!!!!
^_^
hope u can help me here/...!
thankx a ton

ps....sorry the mailz soo long!! ^_^

This post has been edited by baby greenz: 06 January 2007 - 06:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: calculator coding

#2 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: calculator coding

Posted 06 January 2007 - 08:39 PM

What basic are you using?
did you mean that a="(234*(34+6)-7*2/9)/5" find the value of a?
To do this, you need to:
a ) read the numbers in the array
b ) read the operations in the array
c ) calculate the operation queue to the array
d ) start calculating

If your teacher gives credit for irrational solutions you can add excel spreadsheet to the project and do something like: range("converter")="=" & a and a=range("converter")

Hope it helped :D
Was This Post Helpful? 0
  • +
  • -

#3 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: calculator coding

Posted 06 January 2007 - 09:36 PM

But if you meant you needed to make a calculator, you can use my snippet for guidance, if you do not know where to start.

You can find this snippet here.

The code part would be:
Dim buffer, x, tehe
 
Private Sub nupp_Click(Index As Integer)
	Select Case Index
		Case 0 To 9
			x = Val(Me.Text1.Text)
			If Len(IIf(x > 0, x, x * -1)) < 10 Then
				Me.Text1.Text = Val(Me.Text1.Text & Index)
			End If
		Case 10
			buffer = x
			tehe = 0
			Me.Text1.Text = 0
		Case 11
			buffer = x
			tehe = 1
			Me.Text1.Text = 0
		Case 12
			buffer = x
			tehe = 2
			Me.Text1.Text = 0
		Case 13
			buffer = x
			tehe = 3
			Me.Text1.Text = 0
		Case 14
			x = Val(Me.Text1.Text)
			Select Case tehe
				Case 0: Me.Text1.Text = buffer / x
				Case 1: Me.Text1.Text = buffer * x
				Case 2: Me.Text1.Text = buffer - x
				Case 3: Me.Text1.Text = buffer + x
				Case 4: Me.Text1.Text = buffer Mod x
				Case 4: Me.Text1.Text = buffer ^ x
			End Select
		Case 15
			buffer = x
			tehe = 4
			Me.Text1.Text = 0
		Case 15
			buffer = x
			tehe = 5
			Me.Text1.Text = 0
		Case 17
			x = 3.141592653
			Me.Text1.Text = x
		Case 18
			x = Val(Me.Text1.Text) * -1
			Me.Text1.Text = x
		Case 20
			x = 0
			Me.Text1.Text = 0
	End Select
	
End Sub


Hope it helped :D
Was This Post Helpful? 0
  • +
  • -

#4 Bramenath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-February 10

Re: calculator coding

Posted 01 February 2010 - 01:15 AM

View Postbaby greenz, on 6 Jan, 2007 - 05:21 PM, said:

hi,
im ria...im studyin software developement in UK.

we are doin a work in class...but i kinda seem 2 be stuck..!!

we are makin a calculator in vb that wud except input from the user n also ask the user to define an operation(+ - * /).
i knw the normal one......wer the program takes 2 or 3 numberz n the user preses a desired command button(Add,multi,div.sub) n get the result.

eg. commandadd.caption = val(txt1.txt) + val(txt2.txt)

but wat v are required to do is....create a program tht wud take thenumberz n gv the ans(like above) but also gv an option 2 the user to continue frm ther...lik inreal calcz!
so he can add 2 more numberz to the result!!
sumthin like tht.....!
one more question...can this be done in parameterz???

i hav done a bit on this.....but its in the coll computerz...! so if u want i can get that by monday...but its not much!!!!!
^_^
hope u can help me here/...!
thankx a ton

ps....sorry the mailz soo long!! ^_^



Option Explicit

Private mdblResult           As Double
Private mdblSavedNumber      As Double
Private mstrDot              As String
Private mstrOp               As String
Private mstrDisplay          As String
Private mblnDecEntered       As Boolean
Private mblnOpPending        As Boolean
Private mblnNewEquals        As Boolean
Private mblnEqualsPressed    As Boolean
Private mintCurrKeyIndex    As Integer
Private LastInput   As Integer


Private Sub Command1_Click(Index As Integer)

Dim strPressedKey   As String
    
    mintCurrKeyIndex = Index
    
    If mstrDisplay = "ERROR" Then
        mstrDisplay = ""
    End If
    
    strPressedKey = Command1(Index).Caption
    
    Select Case strPressedKey
        Case "0", "1", "2", "3", "4", _
             "5", "6", "7", "8", "9"
            If mblnOpPending Then
                mstrDisplay = ""
                mblnOpPending = False
            End If
            If mblnEqualsPressed Then
                mstrDisplay = ""
                mblnEqualsPressed = False
            End If
            mstrDisplay = mstrDisplay & strPressedKey
        Case "."
            If mblnOpPending Then
                mstrDisplay = ""
                mblnOpPending = False
            End If
            If mblnEqualsPressed Then
                mstrDisplay = ""
                mblnEqualsPressed = False
            End If
            If InStr(mstrDisplay, ".") > 0 Then
                Beep
            Else
                mstrDisplay = mstrDisplay & strPressedKey
            End If
        Case "+", "-", "*", "/"
            mdblResult = Val(mstrDisplay)
            mstrOp = strPressedKey
            mblnOpPending = True
            mblnDecEntered = False
            mblnNewEquals = True
            
        
        Case "="
            If mblnNewEquals Then
                mdblSavedNumber = Val(mstrDisplay)
                mblnNewEquals = False
            End If
            Select Case mstrOp
                Case "+"
                    mdblResult = mdblResult + mdblSavedNumber
                Case "-"
                    mdblResult = mdblResult - mdblSavedNumber
                Case "*"
                    mdblResult = mdblResult * mdblSavedNumber
                Case "/"
                    If mdblSavedNumber = 0 Then
                        mstrDisplay = "ERROR"
                    Else
                        mdblResult = mdblResult / mdblSavedNumber
                    End If
                Case Else
                    mdblResult = Val(mstrDisplay)
            End Select
            If mstrDisplay <> "ERROR" Then
                mstrDisplay = Format$(mdblResult)
            End If
            mblnEqualsPressed = True

        Case "Backspace"
            If Val(mstrDisplay) <> 0 Then
                mstrDisplay = Left$(mstrDisplay, Len(mstrDisplay) - 1)
                mdblResult = Val(mstrDisplay)
            End If
        Case "CE"
            mstrDisplay = ""
        End Select
        
    If mstrDisplay = "" Then
       Text1.Text = "0."
    Else
        mstrDot = IIf(InStr(mstrDisplay, ".") > 0, "", ".")
       Text1.Text = mstrDisplay & mstrDot
        If Left$(Text1.Text, 1) = "0" Then
            Text1.Text = Mid$(Text1.Text, 2)
        End If
    End If
    
    If Text1.Text = "." Then Text1.Text = "0."
    


End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
 
 Dim intIndex    As Integer
    
    Select Case KeyCode
  
        Case vbKey0, vbKeyNumpad0:  intIndex = 10
        Case vbKey1, vbKeyNumpad1:  intIndex = 0
        Case vbKey2, vbKeyNumpad2:  intIndex = 1
        Case vbKey3, vbKeyNumpad3:  intIndex = 2
        Case vbKey4, vbKeyNumpad4:  intIndex = 3
        Case vbKey5, vbKeyNumpad5:  intIndex = 4
        Case vbKey6, vbKeyNumpad6:  intIndex = 5
        Case vbKey7, vbKeyNumpad7:  intIndex = 6
        Case vbKey8, vbKeyNumpad8:  intIndex = 7
        Case vbKey9, vbKeyNumpad9:  intIndex = 8
        Case vbKeyDecimal:          intIndex = 9
        Case vbKeyAdd:              intIndex = 12
        Case vbKeySubtract:         intIndex = 13
        Case vbKeyMultiply:         intIndex = 14
        Case vbKeyDivide:           intIndex = 15
        Case Else:
        Exit Sub
    End Select
    
    Command1(intIndex).SetFocus
    Command1_Click intIndex
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)

Dim intIndex As Integer
    
    Select Case Chr$(KeyAscii)
       
        Case "=":       intIndex = 22
        Case Else:
        Exit Sub
    End Select
    
    Command1(intIndex).SetFocus
    Command1_Click intIndex
    
End Sub


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 01 February 2010 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1