VB6.0 中型计算器 制作
来源:网络收集 点击: 时间:2024-05-07首先,框架是非常重要的,在界面上利用好框架会让编程简单很多
1.分区 使用Frame控件 (caption属性 改 显示名称) 划分4个区域,如图
2.放置 CommandButton控件 作 命令按键
TextBox控件 作 显示结果按键
接下来进入定义阶段

定义
还是这张图
tip:CommandButton (caption属性 改 显示名称)
TextBox (text属性 改 显示名称)
编程中名称尽量使用英语或数字
运算区:3个黄色TextBox (名称)分别改为t1 t2 t3
区域外:紫色TextBox (名称)改为t4
数字区:共12个CommandButton
0~9 10个按键(名称)分别改为 Cmd0 Cmd1 Cmd2 。。。按规律
. 小数点按键(名称)改为 Cmd小数点
+/- 相反数按键 (名称)改为 相反数
符号区:共10个CommandButton
+ - * / 四个按键 (名称)分别改为 加 减 乘 除
\ 按键 (名称)改为 整除
^ 按键 (名称)改为 幂
√ 按键 (名称)改为 根号
| | 按键 (名称)改为 绝对值
ANS 按键 (名称)改为 ANS
= 按键 (名称)改为 等于
功能区:共4个CommandButton
Del 按键 (名称)改为 删除
CE 按键 (名称)改为 清除
AC 按键(名称)改为 清空
Quit 按键(名称)改为 退出
排布如图

数字区编程
1.数字投入 (If Else End If)的运用
按键 0 编入
Private Sub Cmd0_Click()
If t2.Text = Then
t1.Text = t1.Text + 0 ‘当没有运算符号时 数字0投入 t1
Else
t3.Text = t3.Text + 0 ‘当有运算符号时 数字0投入 t1
End If
If t4.Text 0. Then 快捷键的运用(可省去)便于开始第二段运算
t2.Text =
t3.Text =
t4.Text = 0. ’t4的常规形态 0.
t1.Text = + 0
End If
End Sub
按键 1 编入
Private Sub Cmd1_Click()
If t2.Text = Then
t1.Text = t1.Text + 1
Else
t3.Text = t3.Text + 1
End If
If t4.Text 0. Then
t2.Text =
t3.Text =
t4.Text = 0.
t1.Text = + 1
End If
End Sub
按键 2 编入 同上改写编码 (1改成2)
按键 3 编入 同上改写编码 (1改成3).。。。。
小数点编入 (重点1) InStr函数的运用
Private Sub Cmd小数点_Click()
If t1.Text And InStr(t1.Text, .) = 0 Then
t1.Text = t1.Text + .
Else ’InStr(t1.Text, .)表示.在t1.Text的字符串中排在第几位
If t3.Text And InStr(t3.Text, .) = 0 Then ‘=0 表示不存在.
t3.Text = t3.Text + . 这里运用的含义就是小数点在一个数中只出现一次
End If
End If
End Sub
相反数 编入 赋值变量运用
Private Sub 相反数_Click()
Dim a As Double
Dim b As Double
a = Val(t1.Text)
b = Val(t4.Text)
If t2.Text = And t3.Text = Then
t4.Text = Str(-a) ’直接在t1内进行相反数运算
Else
t1.Text = Str(b) ‘对运算结果进行相反数运算
t4.Text = Str(-b)
t2.Text =
t3.Text =
End If
End Sub
符号区编程 (1) (++重点)
加
Private Sub 加_Click()
t2.Text = +
End Sub
减
Private Sub 减_Click()
If t1.Text = Then
t1.Text = t1.Text + -
Else
If t2.Text = Then
t2.Text = - ’作负号运用
Else
If InStr(t3.Text, -) = 0 And t3.Text = Then
t3.Text = t3.Text + - ‘and表并列条件
End If
End If
End If
End Sub
乘
Private Sub 乘_Click()
t2.Text = *
End Sub
除
Private Sub 除_Click()
If t2.Text = Then
t2.Text = /
Else
If t3.Text And InStr(t3.Text, /) = 0 And t2.Text = ^ Then
t3.Text = t3.Text + / ’分数指数幂的运用
End If
End If
End Sub
整除
Private Sub 整除_Click()
If t2.Text = Then
t2.Text = \
Else
If t3.Text And InStr(t3.Text, \) = 0 And t2.Text = ^ Then
t3.Text = t3.Text + \
End If
End If
End Sub
幂
Private Sub 幂_Click()
t2.Text = ^
End Sub
根号
Private Sub 根号_Click()
If Val(t1.Text) = 0 Then
t4.Text = Sqr(Val(t1.Text)) ‘sqr函数 求算数平方根
Else
t4.Text = Math ERROR ’定义域,被开方数0,显示错误
End If
End Sub
绝对值
Private Sub 绝对值_Click()
If t1.Text Then
t4.Text = Abs(Val(t1.Text)) ‘Abs函数 求绝对值
End If
End Sub
符号区编程 (2) (++重点)
Private Sub 等于_Click()
Dim l As Long
Dim k As Long
Dim c As Long
Dim d As Long
Select Case t2
Case +
result = Val(t1.Text) + Val(t3.Text)
Case -
result = Val(t1.Text) - Val(t3.Text)
Case *
result = Val(t1.Text) * Val(t3.Text)
Case /
If Val(t3.Text) 0 Then
result = Val(t1.Text) / Val(t3.Text)
Else
result = Math ERROR
End If
Case \
If Val(t3.Text) 0 Then
result = Val(t1.Text) \ Val(t3.Text)
Else
result = Math ERROR
End If
Case ^
If InStr(t3.Text, /) 0 Then
l = Len(t3.Text)
k = InStr(t3.Text, /)
c = Mid(t3.Text, 1, k - 1)
d = Mid(t3.Text, k + 1, l)
If d = 0 Then
t3.Text = Math ERROR
result = Math ERROR
Else
t3.Text = Str(c / d)
End If
End If
If t1.Text = 0 And Val(t3.Text) = 0 Then
result = Math ERROR
Else
If Val(t1.Text) 0 And d Mod 2 = 0 And d 0 Then
result = Math ERROR
Else
result = Val(t1.Text) ^ Val(t3.Text)
End If
End If
Case
result = Val(t1.Text)
End Select
t4.Text = result
End Sub
保存结果到下次运算
Private Sub ANS_Click()
If t1.Text And t4.Text 0. Then
t1.Text = Val(t4.Text) ’保存结果到下次运算
t2.Text =
t3.Text =
t4.Text = 0.
End If
End Sub
tip:Case函数
Select Case t2 ’对t2进行讨论
Case + t2.Text=+
result = Val(t1.Text) + Val(t3.Text) 产生事件
Len函数
l = Len(t3.Text) ‘ t3.Text字符串的长度
Mid函数
c = Mid(t3.Text, 1, k - 1) ’在t3.Text字符串中 从第1位起到第k - 1位终的长度
Mod函数
d Mod 2 = 0 ‘d/2 取余数
功能区编程
Private Sub 删除_Click()
If t1.Text And t2.Text = Then
t1.Text = Left(t1.Text, Len(t1.Text) - 1)
Else
If t3.Text Then
t3.Text = Left(t3.Text, Len(t3.Text) - 1)
End If
End If
End Sub
Private Sub 清除_Click()
If t2.Text = Then
t1.Text =
Else
t3.Text =
End If
End Sub
Private Sub 清空_Click()
t1.Text =
t2.Text =
t3.Text =
t4.Text = 0.
End Sub
Private Sub 退出_Click()
End
End Sub
tip:left函数
t1.Text = Left(t1.Text, Len(t1.Text) - 1) ‘ t1.Text字符串去掉最后一位
结束
注意事项Commandbutton控件颜色不可改
TextBox控件BackColour改背景颜色
Font改字体,大小
版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_703210.html