用vb做一个计算器
1 最基本的计算器,有加减乘除,开根号,负数运算等;2可以用鼠标单击,也可以用键盘输入;
3可以连加减乘除;
4 在基本的基础上有sin,cos,tan,平方和立方的运算.
答案:先建立一个新的项目文件,里面包含一个Form1的窗体。然后,添加控件,包括一个Text框(Text1),和17个按钮,这17个按钮使用的名称都是Command1,只是Index属性不同,就好象一个数组,创立一组按钮的方法是,先在窗体上建立一个按钮(Command1),然后在Command1上击鼠标右键,选择复制(Copy),然后在窗体上任意部位击右键,选择粘贴(Paste),系统会提示是否创建控件组,选择“是”,重复上面步骤,直至创立17个按钮为止。各控件属性如下表:
Text1 BackColor=&H00C0FFFF&Text=“0.”
Command1 Index 0――16Index 0――9 Caption=“0”-“9”Index 10 Caption=“.”Index 11 Caption=“=”Index 12 Caption=“+”Index 13 Caption=“-”Index 14 Caption=“×”Index 15 Caption=“÷”Index 16 Caption=“清除”
代码如下:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean '判断是否是数字开头
Dim PointFlag As Boolean '判断是否已有小数点
Dim Runsign As Integer '储存运算符号
Dim SignFlag As Boolean '判断是否已有运算符号
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = " "
StrNum2 = " "
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = "0. "
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
StrNum2 = Str(equal)
StrNum1 = StrNum2
Text1.Text = StrNum2
End Sub
Private Sub about_Click()
MsgBox "Create by jianghuali。2007/3/7 ", vbDefaultButton1, "VB教程第四章范例 "
End Sub
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 + Str(Index)
End If
Text1.Text = StrNum1
Case 10
If Not PointFlag Then
If FirstNum Then
StrNum1 = "0. "
FirstNum = False
Else
StrNum1 = StrNum1 + ". "
End If
Else
Exit Sub
End If
PointFlag = True
Text1.Text = StrNum1
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 11
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else
Call ClearData
End Select
End Sub
Private Sub Form_Load()
Call ClearData
End Sub用vb做这种东西是最简单的了用鼠标点简单的可以用 command控件
用键盘输入是写个KEY事件按哪个数字就直接调用哪个command控件的点击事件
连加减乘除最简单了 别把你保存数字的函数或者控件清空就可以了
sin,cos,tan,平方和立方的运算 这些VB里有内部函数直接用 后面的写下公式就可以了Dim shu1 As Single, shu2 As Single, suanfu As String
Dim su As Boolean
Private Sub Command1_Click(Index As Integer)
If Not su Then
If Val(Text1.Text) = 0 And InStr(Text1.Text, ".") = 0 Then
Text1.Text = Trim(Str(Index))
Else
Text1.Text = Text1.Text & Trim(Str(Index))
End If
Else
shu1 = Val(Text1.Text)
Text1.Text = Str(Index)
su = False
End If
End Sub
Private Sub Command2_Click(Index As Integer)
Call Command4_Click
suanfu = Command2(Index).Caption
su = True
End Sub
Private Sub Command3_Click()
Text1.Text = Text1.Text + "."
If (InStr(Text1.Text, ".") = 1) Then
Text1.Text = ""
End If
If InStr(Text1.Text, ".") < Len(Text1.Text) Then
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End If
End Sub
Private Sub Command4_Click()
shu2 = Val(Text1.Text)
If shu1 <> 0 Then
Select Case suanfu
Case "+"
Text1.Text = shu1 + shu2
Case "-"
Text1.Text = shu1 - shu2
Case "*"
Text1.Text = shu1 * shu2
Case "/"
If shu2 = 0 Then
Text1.Text = ""
Else
Text1.Text = shu1 / shu2
End If
End Select
End If
End Sub
Private Sub Command5_Click()
If Text1.Text = "" Then
Exit Sub
End If
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End Sub
Private Sub Command6_Click(Index As Integer)
Text1.Text = "0"
End Sub
Private Sub Command7_Click()
Text1.Text = Str(Val(Text1.Text) * (-1))
End Sub
Private Sub Command8_Click()
Text1.Text = Sqr(Val(Text1.Text))
End Sub
Private Sub Command9_Click()
Text1.Text = Val(Text1.Text) / 100
End Sub
Private Sub Form_Load()
Text1.Text = ""
End Sub
上一个:VB保存加载配置ini
下一个:关于vb的msgbox的问题