Você está na página 1de 17

PRACTICE 03

Building a Math Calculator


Visual Basic.NET
by Has Bunton

1
The Math Calculator
1. The application emulates the
operation of a hand-held calculator
and implement the basic arithmetic
operations
2. The Calculator application window
should look like the following.

by Has Bunton 2
Designing the User Interface (1)
1. Start a project. Set the desired font for
the form.
2. Add a label control for the display. Set its
BorderStyle property to Fixed 3D and
change other properties as desired.
3. Draw a Button control on the form, change
its caption (Text property) to 1, name it
Bttn1, and format it nicely. The other digit
buttons on the form will be copies of this
one with Text and Name properties to 1, 2,
… and Bttn2, Bttn3, … respectively.

by Has Bunton 3
Designing the User Interface (2)
1. Place the remaining controls on the form and use
the names and captions as follows.
• Type Name Caption
• Form CalculatorForm
• Label lblDisplay
• Button bttnPeriod .
• Button bttnMultiply *
• Button bttnDivide /
• Button bttnPlus +
• Button bttnNegate +/-
• Button bttnMinus -
• Button bttnClear C
• Button bttnInverse 1/X
• Button bttnEquals =
• Button bttnSin Sin
• Button bttnLog Log
• Button bttnCos Cos
by Has Bunton 4
Programming the Math Calculator
Application (1)
1. Dim clearDisplay As Boolean
2. Dim Operand1 As Double, Operand2 As Double
3. Dim Operator As String

4. Private Sub DigitClick(ByVal sender As System.Object, ByVal e As _


System.EventArgs) Handles bttn0.Click, bttn1.Click, bttn2.Click, _
bttn3.Click, bttn4.Click, bttn5.Click, bttn6.Click, bttn7.Click, _
bttn8.Click, bttn9.Click *
5. If clearDisplay Then
6. lblDisplay.Text = ""
7. clearDisplay = False
8. End If
9. lblDisplay.Text = lblDisplay.Text + sender.text
10. End Sub

by Has Bunton 5
Programming the Math Calculator
Application (2)
11. Private Sub bttnPeriod_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnPeriod.Click
12. If lblDisplay.Text.IndexOf(".") > 0 Then
13. Exit Sub
14. Else
15. lblDisplay.Text = lblDisplay.Text & "."
16. End If
17. End Sub

18. Private Sub bttnPlus_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles bttnPlus.Click
19. Operand1 = Val(lblDisplay.Text)
20. Operator = "+"
21. clearDisplay = True
22. End Sub

by Has Bunton 6
Programming the Math Calculator
Application (3)
23. Private Sub bttnEquals_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnEquals.Click
24. Dim result As Double
25. Operand2 = Val(lblDisplay.Text)
26. Try
27. Select Case Operator
28. Case "+"
29. result = Operand1 + Operand2
30. Case "-"
31. result = Operand1 - Operand2
32. Case "*"
33. result = Operand1 * Operand2
34. Case "/"
35. If Operand2 <> "0" Then
36. result = Operand1 / Operand2
37. End If
38. End Select

by Has Bunton 7
Programming the Math Calculator
Application (4)
39. lblDisplay.Text = result
40. Catch exc As Exception
41. MsgBox(exc.Message)
42. result = "ERROR"
43. Finally
44. lblDisplay.Text = result
45. clearDisplay = True
46. End Try
47. End Sub

48. Private Sub bttnMinus_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles bttnMinus.Click
49. Operand1 = Val(lblDisplay.Text)
50. Operator = "-"
51. clearDisplay = True
52. End Sub
by Has Bunton 8
Programming the Math Calculator
Application (5)
53. Private Sub bttnMultiply_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnMultiply.Click
54. Operand1 = Val(lblDisplay.Text)
55. Operator = "*"
56. clearDisplay = True
57. End Sub

58. Private Sub bttnDivide_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles bttnDivide.Click
59. Operand1 = Val(lblDisplay.Text)
60. Operator = "/"
61. clearDisplay = True
62. End Sub

by Has Bunton 9
Programming the Math Calculator
Application (6)
63. Private Sub CalculatorForm_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles MyBase.KeyPress
64. If System.Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Then
65. If clearDisplay Then
66. lblDisplay.Text = ""
67. clearDisplay = False
68. End If
69. lblDisplay.Text = lblDisplay.Text + e.KeyChar
70. End If
71. If e.KeyChar = "C" Or e.KeyChar = "c" Then
72. lblDisplay.Text = ""
73. End If

by Has Bunton 10
Programming the Math Calculator
Application (7)
74. ''' the following statements program the keys that correspond to operators
75. ''' the Select Case statement is discussed in Chapter 4
76. ''' when an operator key is pressed, we call the event handler for the
77. ''' corresponding button, as if the user had clicked the button
78. ''' since the Equals button is the default button of the form, you can
79. ''' perform simple arithmetic operations without using the mouse at all.
80. ''' just press the digits of the first number, then the symbol of the operation,
81. ''' then the digits of the second number and then press Enter to see the result
82. ''' you can explor this code, as well as program the other buttons on the form,
83. ''' after you have read Chapters 4 and 5.

by Has Bunton 11
Programming the Math Calculator
Application (8)
84. Select Case e.KeyChar
85. Case "+"
86. bttnPlus_Click(sender, e)
87. Case "-"
88. bttnMinus_Click(sender, e)
89. Case "*"
90. bttnMultiply_Click(sender, e)
91. Case "/"
92. bttnDivide_Click(sender, e)
93. End Select
94. End Sub

by Has Bunton 12
Programming the Math Calculator
Application (9)
95. Private Sub bttnInverse_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnInverse.Click
96. If Val(lblDisplay.Text) <> 0 Then
97. lblDisplay.Text = 1 / lblDisplay.Text
98. clearDisplay = True
99. End If
100. End Sub

101. Private Sub bttnNegate_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles bttnNegate.Click
102. lblDisplay.Text = -Val(lblDisplay.Text)
103. clearDisplay = True
104. End Sub

by Has Bunton 13
Programming the Math Calculator
Application (10)
105. Select Case e.KeyChar
106. Case "+"
107. bttnPlus_Click(sender, e)
108. Case "-"
109. bttnMinus_Click(sender, e)
110. Case "*"
111. bttnMultiply_Click(sender, e)
112. Case "/"
113. bttnDivide_Click(sender, e)
114. End Select
115. End Sub

by Has Bunton 14
Programming the Math Calculator
Application (11)
116. Private Sub bttnInverse_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnInverse.Click
117. If Val(lblDisplay.Text) <> 0 Then
118. lblDisplay.Text = 1 / lblDisplay.Text
119. clearDisplay = True
120. End If
121. End Sub

122. Private Sub bttnNegate_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles bttnNegate.Click
123. lblDisplay.Text = -Val(lblDisplay.Text)
124. clearDisplay = True
125. End Sub

by Has Bunton 15
Programming the Math Calculator
Application (12)
126. Private Sub bttnClear_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnClear.Click
127. lblDisplay.Text = ""
128. End Sub

129. Private Sub bttnSin_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles bttnSin.Click
130. lblDisplay.Text = Math.Sin(lblDisplay.Text)
131. clearDisplay = True
132. End Sub

133. Private Sub bttnCos_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles bttnCos.Click
134. lblDisplay.Text = Math.Cos(lblDisplay.Text)
135. clearDisplay = True
136. End Sub

by Has Bunton 16
Programming the Math Calculator
Application (13)
137. Private Sub bttnLog_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttnLog.Click
138. If Val(lblDisplay.Text) < 0 Then
139. MsgBox("Can't calculate the logarithm of a negative number")
140. Else
141. lblDisplay.Text = Math.Log(lblDisplay.Text)
142. End If
143. clearDisplay = True
144. End Sub
145. End Class

End! by Has Bunton 17

Você também pode gostar