当前位置:编程学习 > VB >>

跪求VB设计计算器 一个!必须能用!

由于本人今天已经提问了3次!每一次的答案都不是很可行,主要体现在运行VB中总是提示 语言错误什么的。所以希望最后一次提问能够得到完美的答案

下面说一下要求:

1.要求该VB设计的计算器 有 加减乘除,并且可以正确使用。

2.回答问题者回复的内容必须经过验证可以使用。

3.想讲解详细,本人是菜鸟

追问:亲哥- -您就发发慈悲给我发份吧 QQ邮箱301090986

谢谢您啦~~~~真的急着用- -!!!!

答案:

'-----------------------------------------------------------------------------------------
'程序:VB 计算器
'作者:王峰
'QQ:447201162
'创建日期:2010年1月16日
'最后优化:2010年11月28日
'博客:447201162.blog.163.com
'-----------------------------------------------------------------------------------------
'关于控件的设置
'
'数字按钮:   数字按钮(0)~数字按钮(9)    0~9
'运算按钮:   运算按钮(0)~运算按钮(3)    +,-,*,/
'小数点按钮: 小数点                     .
'等于号:     等于按钮
'开方按钮:   开方
'平方按钮:   平方
'立方按钮:   立方
'C,CE按钮:   C
'M+:         M_ADD
'M-:         M_DEG
'MC:         M_C
'MR:         M_R
'π:          π
'Label1       Label1
'Text1        Text1
'-----------------------------------------------------------------------------------------
Dim 运算 As Boolean
Dim 运算符 As Long
Dim 数A As Long
Dim 数B As Long
Dim 输入 As Boolean
Dim 等于 As Boolean
Dim M As Long

Private Sub C_Click(Index As Integer)
CE
End Sub

Private Sub M_ADD_Click()
M = M + Val(Text1)
运算 = True
If M <> 0 Then Label1.Caption = "M" Else Label1.Caption = ""
End Sub

Private Sub M_C_Click()
Label1.Caption = ""
M = 0
End Sub

Private Sub M_R_Click()
Text1 = M
输入 = True
运算 = True
End Sub

Private Sub M_RED_Click()
M = M - Val(Text1)
运算 = True
If M <> 0 Then Label1.Caption = "M" Else Label1.Caption = ""
End Sub

Private Sub Text1_Change()
On Error Resume Next
If Left(Text1, 1) = "." <> 0 Then
Text1 = "0" & Trim(Text1)
ElseIf Left(Text1, 2) = "-." <> 0 Then
Text1 = "-0" & Trim(Abs(Text1))
End If
End Sub


Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = -1
End Sub

Private Sub π_Click()
Text1 = 3.14159265358979
运算 = False
End Sub

Private Sub 等于按钮_Click()
If 等于 = False Then 数B = Text1
If 运算符 = 1 Then
Text1 = Val(Text1) + Val(数A)
运算 = True
End If
If 运算符 = 2 Then
If 等于 = False Then Text1 = Val(数A) - Val(Text1) Else Text1 = Val(Text1) - Val(数A)
运算 = True
End If
If 运算符 = 3 Then
Text1 = Val(Text1) * Val(数A)
运算 = True
End If
If 运算符 = 4 Then
If Val(Text1) = "0" Then
Text1 = "除数不能为零。"
Else
If 等于 = False Then Text1 = Val(数A) / Val(Text1) Else Text1 = Val(Text1) / Val(数A)
End If
运算 = True
End If
If 等于 = False Then 数A = 数B
等于 = True
输入 = False
运算 = True
End Sub

Private Sub 开方_Click()
On Error GoTo err
Text1 = Sqr(Text1)
运算 = True
Exit Sub
err:
Text1 = "负数没有平方根!"
End Sub

Private Sub 立方_Click()
Text1 = Val(Text1) * Val(Text1) * Val(Text1)
运算 = True
End Sub

Private Sub 平方_Click()
Text1 = Val(Text1) * Val(Text1)
运算 = True
End Sub

Private Sub 诗雨_Click()

End Sub

Private Sub 数字按钮_Click(Index As Integer)
If 运算 = True Or Text1 = "3.14159265358979" Then
运算 = False
Text1 = Index
输入 = True
Else
Text1 = Text1 & Index
End If
If 等于 = True Then 运算符 = True
End Sub

Private Sub 小数点_Click()
If 等于 = True Or Text1 = "3.14159265358979" Then
Text1 = "0."
End If
If InStr(Text1, ".") = 0 Then
Text1 = Text1 & "."
End If
If Text1 = "." Then Text1 = "0."
运算 = False
End Sub

Private Sub 运算按钮_Click(Index As Integer)
If 运算符 <> 1 And 运算符 <> 2 And 运算符 <> 3 And 运算符 <> 4 Then
数A = Text1
GoTo 1
End If
If 等于 = True Then GoTo 2
If 输入 = False Then GoTo 1
If Index + 1 = 1 Then
Text1 = Val(Text1) + Val(数A)
运算 = True
End If
If Index + 1 = 2 Then
Text1 = Val(数A) - Val(Text1)
运算 = True
End If
If Index + 1 = 3 Then
Text1 = Val(Text1) * Val(数A)
运算 = True
End If
If Index + 1 = 4 Then
Text1 = Val(数A) / Val(Text1)
运算 = True
End If
数A = Text1
输入 = False
Exit Sub
2:
数A = Text1
1:
等于 = False
运算 = True
运算符 = Index + 1
End Sub

Private Sub 正负_Click()
Text1 = 0 - Val(Text1)
End Sub

Private Sub CE()
Text1 = ""
运算 = False
运算符 = True
数A = 0
输入 = False
等于 = False
数B = 0
End Sub

 

'------------------------------------------------------------------

可以连续按等于按钮计算,类似windows计算器

你一点都不会啊?缺少语句结束都搞不定

Option Explicit
Public num As String, num1 As String
Dim C As Integer

Private Sub Command1_Click(Index As Integer)
Text1.SetFocus
  num = num + Command1(Index).Caption
  Text1.Text = num
End Sub

Private Sub Command2_Click(Index As Integer)
num1 = num
  Text1.Text = ""
  num = ""
  C = Index
End Sub

Private Sub Command3_Click()
End
End Sub

Private Sub Command4_Click()
'清空Text1
 Text1.Text = ""
 num = "'"
 
End Sub

Private Sub Command5_Click()
If InStr(num, ".") Then
  MsgBox "已存在小数点"
  Exit Sub
Else
num = num + Command5.Caption
End If
End Sub

Private Sub Command6_Click()
If Text1.Text <> "" Then
  Text1.Text = -1 * Text1.Text
  num = Text1.Text
  End If
End Sub

Private Sub Command7_Click()
Select Case C
    Case 0
    num = Str(Val(num1) + Val(num))
    Case 1
    num = Str(Val(num1) - Val(num))
    Case 2
    num = Str(Val(num1) * Val(num))
    Case 3
    If Val(num) = 0 Then
    MsgBox "error"
    Text1.Text = ""
    num = ""
    Else
    num = Str(Val(num1) / Val(num))
    End If
    End Select
    Text1.Text = num
End Sub

Private Sub Form_Load()

End Sub

 

?

上一个:vb程序右键打开文件的问题。
下一个:VB编程_如何在txt文件首尾插入内容!

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,