各位大虾
小弟最近在学习VB编程,近日需要编写一个计算器,但小弟资质尚浅,还请各位大虾能指点一二。
希望哪位大哥有空帮我写个比较全面的计算器代码,并能把一些重要的代码解释一下,小弟谢过了!!!
各位大虾
小弟最近在学习VB编程,近日需要编写一个计算器,但小弟资质尚浅,还请各位大虾能指点一二。
希望哪位大哥有空帮我写个比较全面的计算器代码,并能把一些重要的代码解释一下,小弟谢过了!!!
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents Button4 As System.Web.UI.WebControls.Button
Protected WithEvents Button5 As System.Web.UI.WebControls.Button
Protected WithEvents Button6 As System.Web.UI.WebControls.Button
Protected WithEvents Button7 As System.Web.UI.WebControls.Button
Protected WithEvents Button8 As System.Web.UI.WebControls.Button
Protected WithEvents Button9 As System.Web.UI.WebControls.Button
Protected WithEvents Button10 As System.Web.UI.WebControls.Button
Protected WithEvents Button11 As System.Web.UI.WebControls.Button
Protected WithEvents Button12 As System.Web.UI.WebControls.Button
Protected WithEvents Button13 As System.Web.UI.WebControls.Button
Protected WithEvents Button14 As System.Web.UI.WebControls.Button
Protected WithEvents Button15 As System.Web.UI.WebControls.Button
Protected WithEvents Button16 As System.Web.UI.WebControls.Button
Protected WithEvents Button17 As System.Web.UI.WebControls.Button
Protected WithEvents Button18 As System.Web.UI.WebControls.Button
Protected WithEvents Button19 As System.Web.UI.WebControls.Button
Protected WithEvents Button20 As System.Web.UI.WebControls.Button
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents Label4 As System.Web.UI.WebControls.Label
Protected WithEvents Label5 As System.Web.UI.WebControls.Label
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
If Label2.Text = "" Then '除法的实现
Label3.Text = "/"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
Else
Label2.Text = TextBox1.Text
Select Case Label3.Text
Case "+"
TextBox1.Text = CDbl(Label1.Text) + CDbl(Label2.Text)
Case "*"
TextBox1.Text = CDbl(Label1.Text) * CDbl(Label2.Text)
Case "-"
TextBox1.Text = CDbl(Label1.Text) - CDbl(Label2.Text)
Case "/"
TextBox1.Text = CDbl(Label1.Text) / CDbl(Label2.Text)
Case "1/x"
TextBox1.Text = 1 / CDbl(Label1.Text)
End Select
Label3.Text = "/"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "1"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "1"
Label4.Text = ""
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "2"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "2"
Label4.Text = ""
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "3"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "3"
Label4.Text = ""
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "4"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "4"
Label4.Text = ""
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "5"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "5"
Label4.Text = ""
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "6"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "6"
Label4.Text = ""
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "7"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "7"
Label4.Text = ""
End If
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "8"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "8"
Label4.Text = ""
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "9"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "9"
Label4.Text = ""
End If
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "0"
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "0"
Label4.Text = ""
End If
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
If Label4.Text = "" Then
TextBox1.Text = TextBox1.Text & "."
Else
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "."
Label4.Text = ""
End If
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
If Label2.Text = "" Then '加法的实现
Label3.Text = "+"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
Else
Label2.Text = TextBox1.Text
Select Case Label3.Text
Case "+"
TextBox1.Text = CDbl(Label1.Text) + CDbl(Label2.Text)
Case "*"
TextBox1.Text = CDbl(Label1.Text) * CDbl(Label2.Text)
Case "-"
TextBox1.Text = CDbl(Label1.Text) - CDbl(Label2.Text)
Case "/"
TextBox1.Text = CDbl(Label1.Text) / CDbl(Label2.Text)
Case "1/x"
TextBox1.Text = 1 / CDbl(Label1.Text)
End Select
Label3.Text = "+"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
End If
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
If Label2.Text = "" Then '减法的实现
Label3.Text = "-"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
Else
Label2.Text = TextBox1.Text
Select Case Label3.Text
Case "+"
TextBox1.Text = CDbl(Label1.Text) + CDbl(Label2.Text)
Case "*"
TextBox1.Text = CDbl(Label1.Text) * CDbl(Label2.Text)
Case "-"
TextBox1.Text = CDbl(Label1.Text) - CDbl(Label2.Text)
Case "/"
TextBox1.Text = CDbl(Label1.Text) / CDbl(Label2.Text)
Case "1/x"
TextBox1.Text = 1 / CDbl(Label1.Text)
End Select
Label3.Text = "-"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
End If
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
If Label2.Text = "" Then '乘法的实现
Label3.Text = "*"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
Else
Label2.Text = TextBox1.Text
Select Case Label3.Text
Case "+"
TextBox1.Text = CDbl(Label1.Text) + CDbl(Label2.Text)
Case "*"
TextBox1.Text = CDbl(Label1.Text) * CDbl(Label2.Text)
Case "-"
TextBox1.Text = CDbl(Label1.Text) - CDbl(Label2.Text)
Case "/"
TextBox1.Text = CDbl(Label1.Text) / CDbl(Label2.Text)
Case "1/x"
TextBox1.Text = 1 / CDbl(Label1.Text)
End Select
Label3.Text = "*"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
End If
End Sub
Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
Label2.Text = TextBox1.Text '等于的实现
Select Case Label3.Text
Case "+"
TextBox1.Text = CDbl(Label1.Text) + CDbl(Label2.Text)
Case "*"
TextBox1.Text = CDbl(Label1.Text) * CDbl(Label2.Text)
Case "-"
TextBox1.Text = CDbl(Label1.Text) - CDbl(Label2.Text)
Case "/"
TextBox1.Text = CDbl(Label1.Text) / CDbl(Label2.Text)
Case "1/x"
TextBox1.Text = 1 / CDbl(Label1.Text)
End Select
Label2.Text = ""
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
If Label2.Text = "" Then '倒数的实现
Label3.Text = "1/x"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
Else
Label2.Text = TextBox1.Text
Select Case Label3.Text
Case "+"
TextBox1.Text = CDbl(Label1.Text) + CDbl(Label2.Text)
Case "*"
TextBox1.Text = CDbl(Label1.Text) * CDbl(Label2.Text)
Case "-"
TextBox1.Text = CDbl(Label1.Text) - CDbl(Label2.Text)
Case "/"
TextBox1.Text = CDbl(Label1.Text) / CDbl(Label2.Text)
Case "1/x"
TextBox1.Text = 1 / CDbl(Label1.Text)
End Select
Label3.Text = "1/x"
Label1.Text = TextBox1.Text
Label4.Text = TextBox1.Text
End If
End Sub
Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
TextBox1.Text = "" 'CE的实现
End Sub
Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
Label1.Text = "" ' C的实现
Label2.Text = ""
Label3.Text = ""
Label4.Text = ""
TextBox1.Text = ""
End Sub
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
Dim s As String 'Backspace的实现
Dim t As String
s = TextBox1.Text
t = Left(s, Len(s) - 1)
TextBox1.Text = t
End Sub
End Class