要用VB做一个计算器,需要的功能不多,就加减乘除,能清空,还有退出,界面我已经给设计好了,一共是16个commandbutton,commandbutton1-commandbutton9分别表示1-9,commandbutton10表示0,commandbutton11-commandbutton14表示+ - * / ,commandbutton15是小数点,commandbutton16是=.
清空和退出用下拉菜单做,如果不行用commandbutton也可以.
还有一个textbox框,作用就很明显咯.
我也试着做过,但是就是搞不懂怎么才能让后一个输入的数字能跟前面的数字组成一个数,研究了很久还是想不出来该怎么解决,希望高手们给我做一个完整的过程哦,小弟感激不尽呢.希望程序语句都能搞清楚点,最好不要有纰漏哦.
第一个给出让我搞好的答案,我就把分数给他,最少加20分.
最好能按照我的要求完整地搞下来,我用的也是vb6.0
我给你一个好不好?
与你说的已经控件有所不同:
要建立一个具有17个元素的commandbutton控件数组,名称为command1(i),下标从0-16.另外需要手工设定的属性为:
'Form1.MaxButton = False手工设定
'Form1.MinButton = False手工设定
'Form1.BorderStyle = 3 手工设定
代码如下:
Dim I As Integer, RunSign As Byte
Dim Num1 As Single, Num2 As Single, Equal As Single
Dim StrNum1 As String, StrNum2 As String
Dim FirstNum As Boolean, PointFlag As Boolean
Dim SignFlag As Boolean
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)
FirstNum = False
Else
StrNum1 = StrNum1 & Trim(Str(Index))
End If
Text1.Text = StrNum1
Case 10
If Not PointFlag Then
If FirstNum = True Then
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
PointFlag = True
Text1.Text = StrNum1
End If
Case 11
If Not SignFlag Then
StrNum1 = Text1.Text
Equal = Val(StrNum1)
Else
Call Run
End If
FirstNum = True
PointFlag = False
SignFlag = False
Case 12 To 15
FirstNum = True
PointFlag = False
If SignFlag Then
Call Run
Else
SignFlag = True
StrNum2 = StrNum1
StrNum1 = ""
End If
RunSign = Index - 11
Case Else
Call cleardata
End Select
End Sub
Private Sub Form_Load()
Form1.Caption = "STS计算器"
For I = 0 To 16
Command1(I).FontSize = 18
Command1(I).FontName = "Times New Roman"
Next
Text1.Text = "0."
Text1.FontSize = 18
Text1.BackColor = &HC0FFFF
For I = 0 To 9
Command1(I).Caption = I
Next
Command1(10).Caption = "·"
Command1(11).Caption = "="
Command1(12).Caption = "+"
Command1(13).Caption = "-"
Command1(14).Caption = "×"
Command1(15).Caption = "÷"
Command1(16).Caption = "清 除"
Call cleardata
End Sub
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case RunSign
Case 1
Equal = Num1 + Num2
Case 2
Equal = Num1 - Num2
Case 3
Equal = Num1 * Num2
Case 4
Equal = Num1 / Num2
End Select
StrNum1 = Str(Equal)
If Abs(StrNum1) < 1 And Abs(StrNum1) > 0 Then
If StrNum1 < 0 Then StrNum1 = "-0" + Mid(StrNum1, 2)
If StrNum1 > 0 Then StrNum1 = "0" + Mid(StrNum1, 2)
End If
Text1.Text = StrNum1
End Sub
Sub cleardata()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
RunSign = 0
SignFlag = False
Text1.Text = "0."
End Sub
设计运行界面为:
要建立一个具有17个元素的commandbutton控件数组,名称为command1(i),下标从0-16.另外需要手工设定的属性为:
'Form1.MaxButton = False手工设定
'Form1.MinButton = False手工设定
'Form1.BorderStyle = 3 手工设定
代码如下:
Dim I As Integer, RunSign As Byte
Dim Num1 As Single, Num2 As Single, Equal As Single
Dim StrNum1 As String, StrNum2 As String
Dim FirstNum As Boolean, PointFlag As Boolean
Dim SignFlag As Boolean
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)
FirstNum = False
Else
StrNum1 = StrNum1 & Trim(Str(Index))
End If
Text1.Text = StrNum1
Case 10
If Not PointFlag Then
If FirstNum = True Then
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
PointFlag = True
Text1.Text = StrNum1
End If
Case 11
If Not SignFlag Then
StrNum1 = Text1.Text
Equal = Val(StrNum1)
Else
Call Run
End If
FirstNum = True
PointFlag = False
SignFlag = False
Case 12 To 15
FirstNum = True
PointFlag = False
If SignFlag Then
Call Run
Else
SignFlag = True
StrNum2 = StrNum1
StrNum1 = ""
End If
RunSign = Index - 11
Case Else
Call cleardata
End Select
End Sub
Private Sub Form_Load()
Form1.Caption = "STS计算器"
For I = 0 To 16
Command1(I).FontSize = 18
Command1(I).FontName = "Times New Roman"
Next
Text1.Text = "0."
Text1.FontSize = 18
Text1.BackColor = &HC0FFFF
For I = 0 To 9
Command1(I).Caption = I
Next
Command1(10).Caption = "·"
Command1(11).Caption = "="
Command1(12).Caption = "+"
Command1(13).Caption = "-"
Command1(14).Caption = "×"
Command1(15).Caption = "÷"
Command1(16).Caption = "清 除"
Call cleardata
End Sub
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case RunSign
Case 1
Equal = Num1 + Num2
Case 2
Equal = Num1 - Num2
Case 3
Equal = Num1 * Num2
Case 4
Equal = Num1 / Num2
End Select
StrNum1 = Str(Equal)
If Abs(StrNum1) < 1 And Abs(StrNum1) > 0 Then
If StrNum1 < 0 Then StrNum1 = "-0" + Mid(StrNum1, 2)
If StrNum1 > 0 Then StrNum1 = "0" + Mid(StrNum1, 2)
End If
Text1.Text = StrNum1
End Sub
Sub cleardata()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
RunSign = 0
SignFlag = False本回答被提问者采纳
高手不要笑我~~
Private Sub Command1_Click()
Cls
Dim a As Single
Dim b As Single
a = InputBox("请输入被加数" + vbCrLf + "然后单击确定键继续", "计算器", "50")
b = InputBox("请输入加数" + vbCrLf + "然后单击确定键继续", "计算器", "50")
CurrentX = 2100
CurrentY = 1800
Print a; "+"; b; "="; a + b
End Sub
Private Sub Command2_Click()
Cls
Dim a As Single
Dim b As Single
a = InputBox("请输入被减数" + vbCrLf + "然后单击确定键继续", "计算器", "50")
b = InputBox("请输入减数" + vbCrLf + "然后单击确定键继续", "计算器", "50")
CurrentX = 2100
CurrentY = 1800
Print a; "-"; b; "="; a - b
End Sub
Private Sub Command3_Click()
Cls
Dim a As Single
Dim b As Single
a = InputBox("请输入被乘数" + vbCrLf + "然后单击确定键继续", "计算器", "50")
b = InputBox("请输入乘数" + vbCrLf + "然后单击确定键继续", "计算器", "50")
CurrentX = 2100
CurrentY = 1800
Print a; "*"; b; "="; a * b
End Sub
Private Sub Command4_Click()
Cls
Dim a As Single
Dim b As Single
a = InputBox("请输入被除数" + vbCrLf + "然后单击确定键继续", "计算器", "50")
b = InputBox("请输入除数" + vbCrLf + "然后单击确定键继续", "计算器", "50")
CurrentX = 2100
CurrentY = 1800
Print a; "/"; b; "="; a / b
End Sub
窗体界面颜色自定,按钮颜色自定,呵呵
Option Explicit
Dim strNumber As String
Dim strPoint As String
Dim dblNum1 As Double
Dim intOperator As Integer
'清除结果
Private Sub cmdGT_Click()
txtDisplay.Text = "0."
strNumber = ""
strPoint = "."
intOperator = 7
End Sub
'输入数字
Private Sub cmdNumber_Click(Index As Integer)
strNumber = strNumber & cmdNumber(Index).Caption
txtDisplay.Text = strNumber & strPoint
End Sub
Private Sub cmdOnOff_Click()
End
End Sub
'运算过程
Private Sub cmdOperator_Click(Index As Integer)
Dim dblnum2 As Double
'是第一次单击运算符时,将输入的值先赋给第一个数,否则赋值给第二个数进行运算
If intOperator = 7 Then
dblNum1 = CDbl(txtDisplay.Text)
Else
dblnum2 = CDbl(Val(txtDisplay.Text))
'根据输入的符号进行运算
'求普通运算
Select Case intOperator
Case 0
dblNum1 = dblNum1 + dblnum2
Case 1
dblNum1 = dblNum1 - dblnum2
Case 2
dblNum1 = dblNum1 * dblnum2
Case 3
If dblnum2 <> 0 Then
dblNum1 = dblNum1 / dblnum2
Else
MsgBox "除数不能为“0”!请重新输入除数。", vbOKOnly + vbInformation, "除零错误"
Index = intOperator
End If
Case 6
dblNum1 = dblNum1 * dblnum2 / 100
End Select
End If
'取得当前输入的运算符,以做下次运算
intOperator = Index
strNumber = ""
txtDisplay = CStr(dblNum1)
'判断是否为文本框中的数字加点
If Not txtDisplay Like "*.*" Then
txtDisplay.Text = txtDisplay.Text & "."
End If
End Sub
Private Sub cmdOtherOper_Click(Index As Integer)
Dim dblNum As Double
'求平方根,平方,
dblNum = CDbl(Val(txtDisplay.Text))
Select Case Index
Case 0
'验证数据是否有效
If dblNum >= 0 Then
txtDisplay.Text = CStr(Sqr(dblNum))
Else
MsgBox "负数不能开平方根!", _
vbOKOnly + vbCritical, "开平方根错误"
End If
Case 1
txtDisplay.Text = CStr(dblNum ^ 2)
End Select
'判断是否为文本框中的数字加点
If Not txtDisplay Like "*.*" Then
txtDisplay.Text = txtDisplay.Text & "."
End If
End Sub
Private Sub cmdPoint_Click()
strNumber = strNumber & strPoint
strPoint = ""
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'使被按下的数字键的对应按钮取得焦点
Select Case KeyCode
Case 48 To 57
cmdNumber(KeyCode - 48).SetFocus
Case 96 To 105
cmdNumber(KeyCode - 96).SetFocus
Case Else
'使按下的符号键对应的按钮取得焦点
If KeyCode = 107 Or (Shift = vbShiftMask And KeyCode = 187) Then
cmdOperator(0).SetFocus
cmdOperator_Click (0)
ElseIf KeyCode = 109 Or KeyCode = 189 Then
cmdOperator(1).SetFocus
cmdOperator_Click (1)
ElseIf KeyCode = 106 Or (Shift = vbShiftMask And KeyCode = 56) Then
cmdOperator(2).SetFocus
cmdOperator_Click (2)
ElseIf KeyCode = 111 Or KeyCode = 191 Then
cmdOperator(3).SetFocus
cmdOperator_Click (3)
ElseIf KeyCode = 13 Then
cmdOperator(7).SetFocus
cmdOperator_Click (7)
ElseIf KeyCode = 8 Then
cmdGT.SetFocus
Call cmdGT_Click
End If
End Select
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
'将合法的数据输入到文本框
Select Case KeyAscii
Case 48 To 58
'调用数字键点击处理程序
cmdNumber_Click KeyAscii - 48
KeyAscii = 0
Case 46
'调用小数点输入
cmdPoint_Click
KeyAscii = 0
Case 13
'当敲击回车时,不能触发Form的 KeyUp 事件,因此在这里设置文本框的焦点
txtDisplay.SetFocus
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay.SetFocus
End Sub
Private Sub Form_Load()
strNumber = ""
strPoint = "."
intOperator = 7
End Sub
我从我自己做的计算器里复制来的。
Text1.Text = Text1.Text & "1"
End Sub
Private Sub Command2_Click()
Text1.Text = Text1.Text & "2"
End Sub
用这种方法你试试!
必要的时候用Val转换成数字!我用的VB6.0 ,这可能有点差别!