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

用VB编写程序

要求:1.要有数字,可以用鼠标充当键盘输入数字。2.只需要一个输入框,当我们输入运算数1以后,按一下运算符,输入框清空,接着输入第二个运算数再单机等于号。也就是和普通计算器一样的仿真计算器。
答案:
联系我吧   QQ  906015019
Dim x As String
Private Sub Command1_Click()
Text1.Text = Text1.Text & "1"
End Sub
Private Sub Command10_Click()
Text1.Text = Text1.Text & "6"
End Sub
Private Sub Command11_Click()
Text1.Text = Text1.Text & "9"
End Sub
Private Sub Command12_Click()
If Label1.Caption = "+" Then
Text1.Text = Trim(Str(Val(x) + Val(Text1.Text)))
End If
If Label1.Caption = "-" Then
Text1.Text = Trim(Str(Val(x) - Val(Text1.Text)))
End If
If Label1.Caption = "*" Then
Text1.Text = Trim(Str(Val(x) * Val(Text1.Text)))
End If
If Label1.Caption = "/" Then
If Val(Text1.Text) = 0 Then
Text1.Text = "除数不能为零"
Else
If -1 < Val(x) / Val(Text1.Text) And Val(x) / Val(Text1.Text) < 0 Then
Text1.Text = Trim("-0" & Trim(Str(Abs(Val(x) / Val(Text1.Text)))))
Else
If 0 < Val(x) / Val(Text1.Text) And Val(x) / Val(Text1.Text) < 1 Then
Text1.Text = Trim("0" & Trim(Str(Val(x) / Val(Text1.Text))))
Else
Text1.Text = Trim(Str(Val(x) / Val(Text1.Text)))
End If
End If
End If
End If
End Sub
Private Sub Command13_Click()
x = Text1.Text
Text1.Text = ""
Label1.Caption = "+"
End Sub
由于字数限制,没办法将全部代码附上,你可以联系我
'第二题答案
Dim a As Integer, b As Integer

Private Sub Command1_Click()
Command1.Tag = "y"
Command2.Tag = ""
Command3.Tag = ""
Command4.Tag = ""
fuzhi
End Sub

Sub fuzhi()
a = Val(Text1.Text)
Text1.Text = ""
Text1.SetFocus
End Sub

Private Sub Command2_Click()
Command2.Tag = "y"
Command1.Tag = ""
Command3.Tag = ""
Command4.Tag = ""
fuzhi
End Sub

Private Sub Command3_Click()
Command3.Tag = "y"
Command1.Tag = ""
Command2.Tag = ""
Command4.Tag = ""
fuzhi
End Sub

Private Sub Command4_Click()
Command4.Tag = "y"
Command1.Tag = ""
Command2.Tag = ""
Command3.Tag = ""
fuzhi
End Sub

Private Sub Command5_Click()
b = Val(Text1.Text)
Text1.Text = ""
If Command1.Tag = "y" Then
s = a + b
ElseIf Command2.Tag = "y" Then
s = a - b
ElseIf Command3.Tag = "y" Then
s = a * b
Else
s = a / b
End If
Text1.Text = s
End Sub

Private Sub Form_Load()
Text1.Text = ""
End Sub
'第二题答案Dim a As Integer, b As IntegerPrivate Sub Command1_Click()    Command1.Tag = "y"    Command2.Tag = ""    Command3.Tag = ""    Command4.Tag = ""    fuzhiEnd SubSub fuzhi()    a = Val(Text1.Text)    Text1.Text = ""    Text1.SetFocusEnd SubPrivate Sub Command2_Click()    Command2.Tag = "y"    Command1.Tag = ""    Command3.Tag = ""    Command4.Tag = ""    fuzhiEnd SubPrivate Sub Command3_Click()    Command3.Tag = "y"    Command1.Tag = ""    Command2.Tag = ""    Command4.Tag = ""    fuzhiEnd SubPrivate Sub Command4_Click()    Command4.Tag = "y"    Command1.Tag = ""    Command2.Tag = ""    Command3.Tag = ""    fuzhiEnd SubPrivate Sub Command5_Click()    b = Val(Text1.Text)    Text1.Text = ""    If Command1.Tag = "y" Then        s = a + b    ElseIf Command2.Tag = "y" Then        s = a - b    ElseIf Command3.Tag = "y" Then        s = a * b    Else        s = a / b    End If    Text1.Text = sEnd SubPrivate Sub Form_Load()    Text1.Text = ""End Sub
dim int a,c,d
写 按钮 点击事件
每个 数字按钮都是 int(text1.text + 本身) //强制 将 text1中的数字加上本身转换为 整型,也可以转换为别的 比如浮点 之类 的 ,看你具体的精确度
运算符 按钮 事件
a=int(text1.text)
text1.text=" "
b= "运算符"
等于按钮 事件
c=int(text1.text)
if b="运算符" then d=a 运算符 c
end if
text1.text=d

上一个:VB问题啊!急
下一个:什么叫做“vB编程”。

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,