vb计算器实训 实验报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实训8 实验报告
一、实验目的:制作一个计算器
二、实验进程:完成了十六进制,八进制的代码书写。添加了form3,完成了窗体转化。
三、实验原理与分析:
1、控件属性设置:
指导教师:郝谦
2、界面图:
Form1
指导教师:郝谦
Form2:
Form3:
指导教师:郝谦
3、代码书写:
Form1:
Option Explicit
Dim i As Integer
Dim j As Integer
Dim m As String
Dim x1 As Double
Dim x2 As Double
Dim y As String
Dim op As Integer
Dim lastinput As String
Private Sub About_Click()
Form1.Hide
Form3.Show
End Sub
Private Sub Command1_Click(Index As Integer) 输入数字0-9
If Text1.Text = "0." Or lastinput = "=" Then
Text1.Text = Index
lastinput = "number"
指导教师:郝谦
Else
If Form1.Tag <> "" Then
If Index = 10 Then
Text1.Text = "0."
Else
Text1.Text = Command1(Index).Caption
End If
Form1.Tag = ""
Else
Text1.Text = Text1.Text & Command1(Index).Caption
End If
lastinput = "number"
End If
End Sub
Private Sub Command10_Click()
Text1.Text = Format(Text1.Text / 100, "0.####################")
lastinput = "="
End Sub
Private Sub Command11_Click()
x2 = Val(Text1.Text)
Select Case y
Case "+"
x1 = x1 + x2
Case "-"
x1 = x1 - x2
Case "×"
x1 = x1 * x2
Case "÷"
If x2 = 0 Then
m = MsgBox("除数不能为0", vbExclamation + vbOKOnly, "提示")
Text1.SetFocus
Else
x1 = x1 / x2
End If
End Select
Text1.Text = Format(x1, "0.#####################")
lastinput = "="
Text1.SetFocus
End Sub
Private Sub Command2_Click(Index As Integer)
指导教师:郝谦
x1 = Val(Text1.Text)
op = Index
Select Case op
Case 0
y = "+"
Case 1
y = "-"
Case 2
y = "×"
Case 3
y = "÷"
End Select
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click() '正负号
Text1.Text = -Val(Text1.Text)
End Sub
Private Sub Command4_Click() '小数点
If InStr(Text1.Text, ".") Then
Exit Sub
Else
Text1.Text = Text1.Text + "."
End If
End Sub
Private Sub Command5_Click()
Dim length As Integer
length = Len(Text1.Text) '数字长度
If length = 1 Then
If Text1.Text <> "0." Then '除非结果为0,否则都要退一位
Text1.Text = "0."
End If
Else
Text1.Text = Left(Text1.Text, length - 1)
End If
End Sub
Private Sub Command6_Click() '清空当前数据
Text1.Text = "0."
End Sub
指导教师:郝谦
Private Sub Command7_Click() '清除所有数据
x1 = 0
x2 = 0
Text1.Text = Format(0, "0.")
End Sub
Private Sub Command8_Click()
If Text1.Text < 0 Then
m = MsgBox("被开方数要大于零", vbExclamation + vbOKOnly, "提示") Else
Text1.Text = Sqr(Val(Text1.Text))
lastinput = "="
End If
End Sub
Private Sub Command9_Click()
If Text1.Text = 0 Then
m = MsgBox("除数不能为0", vbExclamation + vbOKOnly, "提示")
Text1.SetFocus
Else
Text1.Text = Format(1 / Val(Text1.Text), "0.####################")
lastinput = "="
End If
End Sub
Private Sub copy_Click() '复制
Clipboard.Clear
Clipboard.SetText Text1.SelText
End Sub
Private Sub Form_Initialize() '利用控件数组产生0-9的控件
For i = 1 To 9
Load Command1(i)
Select Case i Mod 3
Case 0
With Command1(i)
.Visible = True
.Caption = i
.Left = Command1(0).Left + 1440
.Top = Command1(i - 3).Top + Command1(i).Height - 1095 End With
Case 1
With Command1(i)
.Visible = True
指导教师:郝谦