微型计算器Gui.ahk

; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=91587
; 有Bug,5-1-1=5

name := "MicroCalculator v1.4"
Gui, Add, Edit, x12 y9 w120 h70 vEdit hwndHandle gEdit,
Gui, Add, Button, x12 y79 w30 h20 g!, n!
Gui, Add, CheckBox, x42 y79 w30 h20 gAOT, #
Gui, Add, Button, x102 y79 w30 h20 gIsPrime, IsPr
Gui, Add, Button, x12 y99 w30 h20 g`%, `%
Gui, Add, Button, x42 y99 w30 h20 g^, x^y
Gui, Add, Button, x72 y99 w30 h20 g√, √
Gui, Add, Button, x102 y99 w30 h20 g/, /
Gui, Add, Button, x102 y119 w30 h20 g*, x
Gui, Add, Button, x102 y139 w30 h20 g-, -
Gui, Add, Button, x102 y159 w30 h20 g+, +
Gui, Add, Button, x102 y179 w30 h20 gEqual, =
Gui, Add, Button, x72 y179 w30 h20 g., .
Gui, Add, Button, x12 y179 w15 h20 gOrder, <
Gui, Add, Button, x27 y179 w15 h20 gOrderReverse, >
Gui, Add, Edit, x72 y79 w30 h20 vRound,
Gui, Add, UpDown, x82 y79 w20 h20 gEdit, 2
Gui, Add, Button, x42 y179 w30 h20 g0, 0
Gui, Add, Button, x12 y159 w30 h20 g1, 1
Gui, Add, Button, x42 y159 w30 h20 g2, 2
Gui, Add, Button, x72 y159 w30 h20 g3, 3
Gui, Add, Button, x12 y139 w30 h20 g4, 4
Gui, Add, Button, x42 y139 w30 h20 g5, 5
Gui, Add, Button, x72 y139 w30 h20 g6, 6
Gui, Add, Button, x12 y119 w30 h20 g7, 7
Gui, Add, Button, x42 y119 w30 h20 g8, 8
Gui, Add, Button, x72 y119 w30 h20 g9, 9
Gui, Add, StatusBar, gSB vSB, Welcome!
SB_SetParts(73)
Menu, Menu, Add, CType
Menu, Menu, Add, 清除
Menu, 历史, Add, 1, 历史
Menu, 历史, Add, 2, 历史
Menu, 历史, Add, 3, 历史
Menu, 历史, Add, 4, 历史
Menu, 历史, Add, 5, 历史
Menu, 历史, Add, 6, 历史
Menu, 历史, Add, 7, 历史
Menu, 历史, Add, 8, 历史
Menu, 历史, Add, 9, 历史
Menu, 历史, Add, 10, 历史
Menu, Menu, Add, 历史, :历史
Gui, Menu, Menu
Gui, Show, h222 w146, %name%
return

GuiClose:
    ExitApp

FocusBack:
history++
Menu, 历史, Rename, %history%&, %Edit%=%numsym%
if (history = 10)
  history := 0
GuiControl, Text, Edit1, %numsym%
GuiControl, Focus, Edit1 
SendMessage, 0xB1, -2, -1,, ahk_id %Handle%
SendMessage, 0xB7,,,, ahk_id %Handle%
numsym := 0
return

Edit:
Gui, Submit, NoHide
if InStr(Edit, "!") > 0
  numsym := ZTrim(Fac(RTrim(Edit, "!")))
if InStr(Edit, "!") = 0
  numsym := Mather.Evaluate(Edit)
SB_SetText(numsym, 2)
return

SB:
Gui, Submit, NoHide
GuiControl, Text, Edit1, %SB%
SB_SetText(Edit)
Goto, Edit
return

历史:
GuiControl, Text, Edit1, % StrReplace(SubStr(A_ThisMenuItem, 1, InStr(A_ThisMenuItem, "=")), "=")
GuiControl, Focus, Edit1 
SendMessage, 0xB1, -2, -1,, ahk_id %Handle%
SendMessage, 0xB7,,,, ahk_id %Handle%
Goto, Edit
return

CType:
Gui, +OwnDialogs
MsgBox, 262192, Dev Note, This is still a WIP!!, 0
return

清除:
GuiControl, Text, Edit1
return

0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
!:
^:
+:
-:
*:
/:
%:
.:
√:
Gui, Submit, NoHide
numsym := Edit A_ThisLabel
if (A_ThisLabel = "√")
  numsym := A_ThisLabel Edit
Goto, FocusBack
return

Order:
Gui, Submit, NoHide
Sort, Edit, N D,
numsym := Edit
Goto, FocusBack
return

OrderReverse:
Gui, Submit, NoHide
Sort, Edit, N R D,
numsym := Edit
Goto, FocusBack
return

AOT:
Winset, Alwaysontop, , A
return

IsPrime:
Gui, Submit, NoHide
GuiControl, Text, Button3, % IsPrime(Edit)
Sleep, 1000
GuiControl, Text, Button3, IsPr
return

Equal:
$Enter::
if WinActive(name)
{
  Gui, Submit, NoHide
  SB_SetText(Edit)
  if InStr(Edit, "!") > 0
    numsym := ZTrim(Fac(RTrim(Edit, "!")))
  if InStr(Edit, "!") = 0
    numsym := Mather.Evaluate(Edit)
  GoSub, FocusBack
  GuiControlGet, clipboard, , Edit1, 
}
if WinActive(name) = 0
  Send, {enter}
return

$^Backspace::
ifWinActive ahk_class AutoHotkeyGUI
  Send ^+{Left}{Backspace}
ifWinNotActive ahk_class AutoHotkeyGUI
  Send, ^{backspace}
return

ZTrim(x) {
  global Round
  x := Round(x, Round)
  IfInString, x, .00
  x := % Floor(x)
  return x
}
IsPrime(n,k=2) {
  d := k+(k<7 ? 1+(k>2) : SubStr("6-----4---2-4---2-4---6-----2",Mod(k,30),1)) 
  Return n < 3 ? n>1 : Mod(n,k) ? (d*d <= n ? IsPrime(n,d) : "Yes") : "No"
}
Fac(x) {
  var := 1
  Loop, %x%
    var *= A_Index
  return var
}
Per(x, y) {
  Per :=(x/100)*y
  return Per
}
class Mather {
  Tokenize(Source) {
    Tokens := []
    
    while (RegexMatch(Source, "Ox)(?<Number>\d+\.\d+|\d+)|(?<Operator>[\+\-\~\!\*\^\/\√\%\&])|(?<Punctuation>[\(\)])", Match)) {
      loop, % Match.Count() {
        Name := Match.Name(A_Index)
        Value := Match[Name]
        
        if (Match.Len(A_Index)) {
          Tokens.Push({"Type": Name, "Value": Value})
        }
      }
      
      Source := SubStr(Source, Match.Pos(0) + Match.Len(0))
    }
    
    return Tokens
  }
  
  static BinaryPrecedence := {"+": 1, "-": 1, "&": 1, "*": 2, "^": 2, "/": 2, "√": 1, "%": 2}
  
  static UnaryPrecedence := 5
  
  EvaluateExpressionOperand(Tokens) {
    NextToken := Tokens.RemoveAt(1)
    
    if (NextToken.Type = "Punctuation" && NextToken.Value = "(") {
      
      Value := this.EvaluateExpression(Tokens)
      
      NextToken := Tokens.RemoveAt(1)
      
      return Value
    }
    else if (NextToken.Type = "Operator") {
      
      Value := this.EvaluateExpression(Tokens, this.UnaryPrecedence)
      
      switch (NextToken.Value) {
        case "+": {
          return Value
        }
        case "-": {
          return -Value
        }
        case "~": {
          return -Value + 1
        }
        case "√": {
          return ZTrim(Sqrt(Value))
        }
        case "&": {
          GuiControl, Text, Button3, % IsPrime(Value)
          Sleep, 1000
          GuiControl, Text, Button3, IsPr
          return Value
        }
      }
      
      Throw "Unary operator " NextToken.Value " is not implemented"
    }
    else if (NextToken.Type = "Number") {
      
      return NextToken.Value * 1
    }
    
  }
  
  EvaluateExpression(Tokens, Precedence := 0) {
    LeftValue := this.EvaluateExpressionOperand(Tokens)
    
    OperatorToken := Tokens.RemoveAt(1)
    
    while (OperatorToken.Type = "Operator" && this.BinaryPrecedence[OperatorToken.Value] >= Precedence) {
      RightValue := this.EvaluateExpression(Tokens, this.BinaryPrecedence[OperatorToken.Value])
      
      switch (OperatorToken.Value) {
        case "+": {
          LeftValue += RightValue
        }
        case "-": {
          LeftValue -= RightValue
        }
        case "*": {
          LeftValue *= RightValue
        }
        case "/": {
          LeftValue := LeftValue/RightValue
        }
        case "%": {
          LeftValue := Per(LeftValue, RightValue)
        }
        case "^": {
          LeftValue := LeftValue**RightValue
        }
      }
      
      OperatorToken := Tokens.RemoveAt(1)
    }
    
    if (OperatorToken) {
      Tokens.InsertAt(1, OperatorToken)
    }
    
    return ZTrim(LeftValue)
  }
  
  Evaluate(Source) {
    return this.EvaluateExpression(this.Tokenize(Source))
  }
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。