找回密码
 加入
搜索
查看: 8070|回复: 17

[AU3基础] 如何判断输入是否为数字

 火.. [复制链接]
发表于 2013-5-23 17:46:10 | 显示全部楼层 |阅读模式
初学AU3,尝试做一个最简单的计算器,做是做出来了,但是有问题,假如输入字符,就会得到错误结果,搞了半天,实在搞不定.恳请大侠指点.万分感谢.

Global Const $GUI_ENVET_CLOSE = -3
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)

Local $Gui = GUICreate("简易计算器",450,100)
Local $y = "|+|-|*|/|"
Local $Input1 = GUICtrlCreateInput("",30,30,50,20)
Local $Combo = GUICtrlCreateCombo("",90,30,50,20,-1,-1)
Local $Input2 = GUICtrlCreateInput("",150,30,50,20)
Local $Button = GUICtrlCreateButton("=",210,30,50,20)
Local $Text = GUICtrlCreateEdit("",270,30,150,20)

GUICtrlSetData($Combo,$y)

GUISetOnEvent($GUI_ENVET_CLOSE,"main")
GUICtrlSetOnEvent($Input1,"main")
GUICtrlSetOnEvent($Input2,"main")
GUICtrlSetOnEvent($Button,"main")

GUISetState(@SW_SHOW)

While 1
        GUISetBkColor(RandomColor())
        Sleep(3000)
WEnd

Func main()
        Local $n1 = GUICtrlRead($Input1)
        Local $n2 = GUICtrlRead($Input2)
        Local $x = GUICtrlRead($Combo)
       
        Switch @GUI_CtrlId
                Case $Button
                        Switch $x
                                Case "+"
                                        GUICtrlSetData($Text,$n1 + $n2)
                                Case "-"
                                        GUICtrlSetData($Text,$n1 - $n2)
                                Case "*"
                                        GUICtrlSetData($Text,$n1 * $n2)
                                Case "/"
                                        GUICtrlSetData($Text,$n1 / $n2)
                        EndSwitch
                Case $GUI_ENVET_CLOSE
                        Exit
        EndSwitch
EndFunc

Func RandomColor()
        Local $temp = "0x"
        Return $temp & Hex(Random(0,255,1),2) &  Hex(Random(0,255,1),2) &  Hex(Random(0,255,1),2)
EndFunc

评分

参与人数 1金钱 -10 收起 理由
afan -10

查看全部评分

发表于 2013-5-23 18:06:10 | 显示全部楼层
input 控件加 0x2000 样式
GUICtrlCreateInput("",30,30,50,20,0x2000) ;$ES_NUMBER = 0x2000
发表于 2013-5-23 18:08:51 | 显示全部楼层
有可用的基本内置函数的, 建议有事没事都多翻翻帮助文档.
StringIsDigit
发表于 2013-5-24 07:18:27 | 显示全部楼层
本帖最后由 user3000 于 2013-5-24 07:26 编辑

回复 2# afan

老大, 加那个样式就无法输入小数点了, 这是'计算器'呢.

发完上话,再多想了下, StringIsDigit 也不适用于检测带小数点的输入....看来只有自己定义个检测函数了
发表于 2013-5-24 08:05:45 | 显示全部楼层
回复 4# user3000


    没错,应该放过一个小数点的。简单的应用可以加 StringIsFloat 判断。
如果限制输入的话则需拦截分析,这方面以前就有过一些讨论和解决方法~
发表于 2013-5-24 18:32:04 | 显示全部楼层
回复 5# afan


    这是个值得思考的问题,标记一下,有时间再来看,今晚上又有应酬~我勒个去~好几天没在家吃饭了~
发表于 2013-5-25 10:24:51 | 显示全部楼层
MsgBox(0,0,Number('1.2')=1.2)
发表于 2013-5-25 10:26:09 | 显示全部楼层
另外,官网上有个计算器的现成例子,可以参考下
发表于 2013-5-26 15:13:22 | 显示全部楼层
#include <EditConstants.au3>

#include <WinAPI.au3> ; used for Lo/Hi word
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

Global Const $GUI_ENVET_CLOSE = -3
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)

Local $Gui = GUICreate("简易计算器", 450, 100)
Local $y = "|+|-|*|/|"
Local $Input1 = GUICtrlCreateInput("", 30, 30, 50, 20)
Local $Combo = GUICtrlCreateCombo("", 90, 30, 50, 20, -1, -1)
Local $Input2 = GUICtrlCreateInput("", 150, 30, 50, 20)
Local $Button = GUICtrlCreateButton("=", 210, 30, 50, 20)
Local $Text = GUICtrlCreateEdit("", 270, 30, 150, 20)

GUICtrlSetData($Combo, $y,"+")

GUISetOnEvent($GUI_ENVET_CLOSE, "main")
GUICtrlSetOnEvent($Input1, "main")
GUICtrlSetOnEvent($Input2, "main")
GUICtrlSetOnEvent($Button, "main")

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
        GUISetBkColor(RandomColor())
        Sleep(3000)
WEnd

Func main()
        Local $n1 = GUICtrlRead($Input1)
        Local $n2 = GUICtrlRead($Input2)
        Local $x = GUICtrlRead($Combo)

        Switch @GUI_CtrlId
                Case $Button
                        GUICtrlSetData($Text, Execute($n1&$x&$n2))
                Case $GUI_ENVET_CLOSE
                        Exit
                EndSwitch
EndFunc   ;==>main

Func RandomColor()
        Local $temp = "0x"
        Return $temp & Hex(Random(0, 255, 1), 2) & Hex(Random(0, 255, 1), 2) & Hex(Random(0, 255, 1), 2)
EndFunc   ;==>RandomColor
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
        #forceref $hWnd, $iMsg
        Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit, $hWndEdit1, $a
        If Not IsHWnd($Input1) Then $hWndEdit = GUICtrlGetHandle($Input1)
        If Not IsHWnd($Input2) Then $hWndEdit1 = GUICtrlGetHandle($Input2)
        $hWndFrom = $ilParam
        $iIDFrom = _WinAPI_LoWord($iwParam)
        $iCode = _WinAPI_HiWord($iwParam)
        Switch $hWndFrom
                Case $Input1, $Input2, $hWndEdit, $hWndEdit1
                        Switch $iCode

                                Case $EN_CHANGE ; Sent when the user has taken an action that may have altered text in an edit control
                                        If $ilParam = $hWndEdit Then
                                                $a = GUICtrlRead($Input1)
                                                If StringRegExp($a, "[^\d\.]", 0) Then
                                                        Beep(500, 100)
                                                        GUICtrlSetData($Input1, StringRegExpReplace($a, "[^\d\.]",''))
                                                        
                                                EndIf
                                                
                                        Else
                                                $a = GUICtrlRead($Input2)
                                                If StringRegExp($a, "[^\d\.]", 0) Then
                                                        Beep(500, 50)
                                                        GUICtrlSetData($Input2, StringRegExpReplace($a, "[^\d\.]",''))
                                                        
                                                EndIf
                                        EndIf

                                Case $EN_KILLFOCUS ; Sent when an edit control loses the keyboard focus

                        EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc
其实用按钮式样的不是更方便嘛。(像系统自带的计算器)
发表于 2013-5-29 17:29:24 | 显示全部楼层
嗯,果断收藏~~
发表于 2013-5-30 05:28:25 | 显示全部楼层
回复 9# pcbar

这个代码有问题哦,比如你输入12...35+12558...5.25这类的依然有效啊,但这些明显不是数字啊
发表于 2013-5-30 07:19:53 | 显示全部楼层
本帖最后由 flyeblue 于 2013-5-30 07:31 编辑

这个最简单的解决办法,
一:直接调用系统的计算器,有现成的为啥不用?
二:你如果非用autoit编,直接用inputbox
While True
        Local $temp1 ,$yunsuan,$temp2,$iMsgBoxAnswer
        $jieguo=0
        $temp1=0
        $temp2=0
        $yunsuan=''
        Do 
                $temp1=InputBox('注意','请输入第一个操作数:','','M')
                If  @error=1 Then ExitLoop 2
        Until         StringRegExp($temp1,"^\d+\.?\d*$")
        Do 
                $yunsuan=InputBox('注意','请输入运算符(+-*/):','','M1')
                If  @error=1 Then ExitLoop 2
        Until         StringRegExp($yunsuan,'[\+\-\*/]')
        Do 
                $temp2=InputBox('注意','请输入第二个操作数:','','M')
                If $yunsuan='/' And $temp2<>0 Then ContinueLoop
                If  @error=1 Then ExitLoop 2
        Until         StringRegExp($temp2,"^\d+\.?\d*$")
        Switch $yunsuan
                Case '+'
                        $jieguo=$temp1+$temp2
                Case '-'
                        $jieguo=$temp1-$temp2
                Case '*'
                        $jieguo=$temp1*$temp2
                Case '/'
                        $jieguo=$temp1/$temp2
        EndSwitch
        
        $iMsgBoxAnswer = MsgBox(65,"结果","结果为:" & @CRLF & $temp1&$yunsuan&$temp2&'='&$jieguo & @CRLF & "" & @CRLF & "点“确定”继续计算,点“取消”退出程序")
        If $iMsgBoxAnswer = 2 Then;Cancel
                ExitLoop
        EndIf
WEnd        
发表于 2013-5-30 20:15:46 | 显示全部楼层
有点太长了
 楼主| 发表于 2013-5-30 21:41:36 | 显示全部楼层
我不会,但是有个思路,用正则表达式判断行不?
发表于 2013-5-30 23:54:02 | 显示全部楼层
这个问题是很伤脑筋啊
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-25 02:52 , Processed in 0.084398 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表