VB6.0 中型计算器 制作

2024-10-12 07:24:52

1、首先,框架是非常重要的,在界面上利用好框架会让编程简单很多1.分区 使用Frame控件 (caption属性 改 显示名称) '划分4个区域,如图2.放置 CommandButton控件 作 命令按键 TextBox控件 作 显示结果按键接下来进入定义阶段

VB6.0 中型计算器 制作

3、数亨蚂擤缚字区编程1.数字投入 (If Else End If)的运用按键 0 编入Private Sub Cm颊俄岿髭d0_Click()If t2.Text = "" Then t1.Text = t1.Text + "0" ‘当没有运算符号时 数字0投入 t1Else t3.Text = t3.Text + "0" ‘当有运算符号时 数字0投入 t1End IfIf t4.Text <> "0." Then '快捷键的运用(可省去)便于开始第二段运算 t2.Text = "" t3.Text = "" t4.Text = "0." ’t4的常规形态 0. t1.Text = "" + "0"End IfEnd Sub按键 1 编入Private Sub Cmd1_Click()If t2.Text = "" Then t1.Text = t1.Text + "1"Else t3.Text = t3.Text + "1"End IfIf t4.Text <> "0." Then t2.Text = "" t3.Text = "" t4.Text = "0." t1.Text = "" + "1"End IfEnd 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 IfEnd IfEnd Sub相反数 编入 赋值变量运用Private Sub 相反数_Click()Dim a As DoubleDim 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 IfEnd Sub

4、符号区编程 (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 IfEnd IfEnd 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 IfEnd IfEnd 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 IfEnd IfEnd 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 IfEnd Sub绝对值Private Sub 绝对值_Click()If t1.Text <> "" Then t4.Text = Abs(Val(t1.Text)) ‘Abs函数 求绝对值End IfEnd Sub

5、符号区编程 (2) (++重点)Private Sub 等于_Click()D坡纠课柩im l As LongDim k As LongDim c As LongDim d As LongSelect Case t2Case "+" 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 IfCase "\"If Val(t3.Text) <> 0 Then result = Val(t1.Text) \ Val(t3.Text)Else result = "Math ERROR"End IfCase "^"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 IfEnd IfIf 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 IfEnd IfCase "" result = Val(t1.Text)End Select t4.Text = resultEnd 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 IfEnd Subtip: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 取余数

6、功能区编程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 IfEnd IfEnd SubPrivate Sub 清除_Click()If t2.Text = "" Then t1.Text = ""Else t3.Text = ""End IfEnd SubPrivate Sub 清空_Click() t1.Text = "" t2.Text = "" t3.Text = "" t4.Text = "0."End SubPrivate Sub 退出_Click()EndEnd Subtip:left函数t1.Text = Left(t1.Text, Len(t1.Text) - 1) ‘ t1.Text字符串去掉最后一位

7、结束

猜你喜欢