用一个标输出结果
什么是标啊,我用msgbox输出的你最后还谢绝,我真服了你的语言表达能力了
Private Sub Command1_Click()
'计算text1
If IsNumeric(Text1) Then
If CLng(Text1) < 0 Then
MsgBox "text1 内为负数,不能开方"
Else
MsgBox "text1 开方结果:" & Sqr(CDbl(Text1))
End If
Else
MsgBox "text1 内不是数字"
End If
'计算text2
If IsNumeric(Text2) Then
If CLng(Text1) < 0 Then
MsgBox "text2 内为负数,不能开方"
Else
MsgBox "text2 开方结果:" & Sqr(CDbl(Text2))
End If
Else
MsgBox "text2 内不是数字"
End If
End Sub
思路:
1、判断值是否大于等于0
2、开平方这个数
3、判断开平方后取整和开平方后的数是否相等,如果相等是完全平方数,否则不是
例子:
dim a,b
for a=1 to 100
if a>=0 then
b=sqr(a)
if int(b)=b then
\'完全平方数
else
\'不是完全平方数
end if
else
\'这个数不能开平方
end if
next a
用记事本把这段代码复制过去,然后另存为frm就可以了
VERSION 500
Begin VBForm frmMain
Appearance = 0 'Flat
BackColor = &H80000005&
BorderStyle = 3 'Fixed Dialog
Caption = "简单计算器"
ClientHeight = 2430
ClientLeft = 45
ClientTop = 435
ClientWidth = 3150
KeyPreview = -1 'True
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2430
ScaleWidth = 3150
ShowInTaskbar = 0 'False
StartUpPosition = 3 '窗口缺省
Begin VBCommandButton Command10
Caption = "^"
Height = 375
Left = 1920
TabIndex = 22
Top = 1920
Width = 495
End
Begin VBPictureBox picDisplay
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 255
Left = 120
ScaleHeight = 225
ScaleWidth = 2865
TabIndex = 19
Top = 120
Width = 2895
Begin VBTextBox Text1
Alignment = 1 'Right Justify
BorderStyle = 0 'None
Height = 255
Left = 240
Locked = -1 'True
TabIndex = 20
Top = 0
Width = 2535
End
Begin VBLabel lblOperation
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 255
Left = 60
TabIndex = 21
Top = 0
Width = 255
End
End
Begin VBCommandButton Command9
Caption = "退格"
Height = 375
Left = 1920
TabIndex = 18
Top = 480
Width = 495
End
Begin VBCommandButton Command8
Caption = "清零"
Height = 375
Left = 2520
TabIndex = 17
Top = 480
Width = 495
End
Begin VBCommandButton cmdDot
Caption = ""
Height = 375
Left = 120
TabIndex = 16
Top = 1920
Width = 495
End
Begin VBCommandButton Command7
Caption = "Sqrt"
Height = 375
Left = 2520
TabIndex = 15
Top = 1920
Width = 495
End
Begin VBCommandButton Command6
Caption = "="
Height = 375
Left = 1320
TabIndex = 14
Top = 1920
Width = 495
End
Begin VBCommandButton Command5
Caption = "/"
Height = 375
Left = 2520
TabIndex = 13
Top = 1440
Width = 495
End
Begin VBCommandButton Command4
Caption = ""
Height = 375
Left = 1920
TabIndex = 12
Top = 1440
Width = 495
End
Begin VBCommandButton Command3
Caption = "-"
Height = 375
Left = 2520
TabIndex = 11
Top = 960
Width = 495
End
Begin VBCommandButton Command2
Caption = "+"
Height = 375
Left = 1920
TabIndex = 10
Top = 960
Width = 495
End
Begin VBCommandButton Command1
Caption = "9"
Height = 375
Index = 9
Left = 1320
TabIndex = 9
Top = 480
Width = 495
End
Begin VBCommandButton Command1
Caption = "8"
Height = 375
Index = 8
Left = 720
TabIndex = 8
Top = 480
Width = 495
End
Begin VBCommandButton Command1
Caption = "7"
Height = 375
Index = 7
Left = 120
TabIndex = 7
Top = 480
Width = 495
End
Begin VBCommandButton Command1
Caption = "6"
Height = 375
Index = 6
Left = 1320
TabIndex = 6
Top = 960
Width = 495
End
Begin VBCommandButton Command1
Caption = "5"
Height = 375
Index = 5
Left = 720
TabIndex = 5
Top = 960
Width = 495
End
Begin VBCommandButton Command1
Caption = "4"
Height = 375
Index = 4
Left = 120
TabIndex = 4
Top = 960
Width = 495
End
Begin VBCommandButton Command1
Caption = "3"
Height = 375
Index = 3
Left = 1320
TabIndex = 3
Top = 1440
Width = 495
End
Begin VBCommandButton Command1
Caption = "2"
Height = 375
Index = 2
Left = 720
TabIndex = 2
Top = 1440
Width = 495
End
Begin VBCommandButton Command1
Caption = "1"
Height = 375
Index = 1
Left = 120
TabIndex = 1
Top = 1440
Width = 495
End
Begin VBCommandButton Command1
Caption = "0"
Height = 375
Index = 0
Left = 720
TabIndex = 0
Top = 1920
Width = 495
End
End
Attribute VB_Name = "frmmain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Data As Double
Private lOperation As Long
Private fClear As Boolean
Private Sub cmdDot_Click()
If InStr(1, Text1Text, "") <= 0 Then Text1Text = Text1Text & ""
End Sub
Private Sub Command1_Click(Index As Integer)
If fClear = True Then Text1Text = "": fClear = False
Text1Text = Text1Text & Index
End Sub
Private Sub Command10_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 5
lblOperationCaption = "^"
End Sub
Private Sub Command2_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 1
lblOperationCaption = "+"
End Sub
Private Sub Command3_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 2
lblOperationCaption = "-"
End Sub
Private Sub Command4_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 3
lblOperationCaption = ""
End Sub
Private Sub Command5_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 4
lblOperationCaption = "/"
End Sub
Private Sub Command6_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 0
lblOperationCaption = ""
End Sub
Private Sub Command7_Click()
If Val(Text1Text) <= 0 Then MsgBox "开方数大于等于0"
Text1Text = Sqr(Val(Text1Text))
Data = Val(Text1Text)
fClear = True
lOperation = 0
lblOperationCaption = ""
End Sub
Private Sub Command8_Click()
Text1Text = ""
lOperation = 0
lblOperationCaption = ""
End Sub
Private Sub Command9_Click()
If Len(Text1Text) > 0 Then Text1Text = Left(Text1Text, Len(Text1Text) - 1)
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0"): Command1_Click 0
Case Asc("1"): Command1_Click 1
Case Asc("2"): Command1_Click 2
Case Asc("3"): Command1_Click 3
Case Asc("4"): Command1_Click 5
Case Asc("5"): Command1_Click 4
Case Asc("6"): Command1_Click 6
Case Asc("7"): Command1_Click 7
Case Asc("8"): Command1_Click 8
Case Asc("9"): Command1_Click 9
Case Asc(""): cmdDot_Click
Case Asc("+"): Command2_Click
Case Asc("-"): Command3_Click
Case Asc(""): Command4_Click
Case Asc("/"): Command5_Click
Case Asc("^"): Command10_Click
Case Asc("s"): Command7_Click
Case Asc("S"): Command7_Click
Case vbKeyBack: Command9_Click
Case Asc("`"): Command8_Click
Case Asc("="): Command6_Click
Case 13: Command6_Click
End Select
KeyAscii = 0
End Sub
最简单最快速的实现方法:
窗口上放两个TextBox,一个名为Text1,用于输入表达式,一个名为Text2,用于输出结果。
放一个CommandButton,名为Command1,点击后进行计算。
然后在工程中引用Microsoft
Script
Control
然后加入以下代码:
Dim
C1
As
New
ScriptControl
Private
Sub
Command1_Click()
Dim
str1
As
String
str1
=
MeText1Text
str1
=
Replace(str1,
"%",
"
Mod
")
str1
=
Replace(str1,
"sqrt",
"sqr")
C1Language
=
"VBScript"
MeText2Text
=
C1Modules(GlobalModule)Eval(str1)
End
Sub
代码就这么一点点,呵呵,够简洁吧?但是可以完全完成你要的功能。
ok,运行看看吧。
____________________________________________________________
补充:
你用的VB6吧?绝对没有问题,我测试了ok才贴出来的。你有在工程中引用Microsoft
Script
Control
10的库吗?
我刚才又把代码拷贝过去测试了的确没有问题。
你能把问题详细情况说下吗?提示什么错误之类的?
_________________________________________________
代码解释:
'引用Microsoft
Script
Control脚本解释库,可以解释VBS和JS的脚本。
'下面定义一个ScriptControl的新实例C1
Dim
C1
As
New
ScriptControl
'按下按钮的时候进行表达式计算。使用脚本引擎将表达式按照VBS语法进行解释。
Private
Sub
Command1_Click()
Dim
str1
As
String
str1
=
MeText1Text'读取Text1的内容
str1
=
Replace(str1,
"%",
"
Mod
")
'将%取模运算符替换为
Mod
(VBS的取模运算符)
str1
=
Replace(str1,
"sqrt",
"sqr")
'将sqrt开放函数替换为VBS中的sqr开方函数
C1Language
=
"VBScript"
'设置脚本语言为VBSript。
'其实设置为JScript也可以,只要把Replace(str1,
"sqrt",
"sqr")
'改为Replace(str1,
"sqrt",
"Mathsqrt")即可,
'这样可以省掉替换%那一句,我用VBScript部分原因是为了
'防止像楼下katar1024那样的人,要简洁这个更简洁啊,你怎么不用?
MeText2Text
=
C1Modules(GlobalModule)Eval(str1)
'最后上面这句就是调用脚本引擎的全局模块的Eval方法运算表达式,
'返回结果显示在Text2中。
End
Sub
再PS:我早知道楼下会出现katar1024这样的人来蹭分,要简洁也不是你那样。
不用引用你以为很好吗?CreateObject是后期绑定,还要Query,比直接引用库要慢不少,也不见得方便,你那招没啥新意,拜托~!
查看原帖>>
求采纳
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)