[AHK]为AutoHotkey打造 表达式求值功能(eval函数)

 为AutoHotkey打造 表达式求值功能(eval函数),方案很多可以是中缀表达式转后缀表达式、也可以是利用正则表达式处理表达式形式。

本位列举两种方案,方案一:利用Excel的Com对象来辅助计算,好处是支持%  自动忽略空格  还支持excel的内置函数,比如sum这类函数。

方案二:利用VBScript的Com对象来辅助计算,简洁明了,不过不支持% ,也能自动忽略空格,不支持英文函数。


;用excel当计算器 支持% 空格 
;作者: David 尹(153671978)  
oExcel := ComObjCreate("Excel.Application")  
oExcel.Workbooks.Add 
MsgBox % oExcalc("(1     + 4%)*100 ")
MsgBox % oExcalc("(1     + 12^2  )*100 /3400")
MsgBox % oExcalc("sum(1     + 2  +3+4+5+6+7+8+9+10)")
oExcel.DisplayAlerts:= False
oExcel.Quit
return

 oExcalc(string,format="")
 {
     global oExcel
     try{
        oExcel.Range("A1").Formula := "= " . string
     }
    return % oExcel.Range("A1").Value
 }




;这个借用Vbs也可以,不过不支持百分号。
eval(str)
{
    spt := ComObjCreate("ScriptControl")
    spt.Language := "VBScript"
    Return,% spt.Eval(str)
}

 

你可能感兴趣的:(AutoHotkey,效率)