发表回复 
写了一个计算器,支持四则运算等,只是精度有问题,请教
2014-09-26, 13 : 51 (这个帖子最后修改于: 2014-09-26 13 : 52 by 100730088.)
写了一个计算器,支持四则运算等,只是精度有问题,请教
123456789123456.22*2算出来的结算不对,好像位数超过12位计算精度就有问题,想大家帮我看看,谢谢。
代码: (全选)
/*
Name:Calcute
Version:2014.09.25
*/

#Persistent
#SingleInstance Force
SplitPath, A_AhkPath, ,Dir,
;Menu Tray, Icon, Shell32.dll, 97
;Menu Tray, Icon, %Dir%\Scripts\Resource\Calcute.ico,,1
Menu Tray, Icon, %SystemRoot%\system32\calc.exe

Menu, Tray, Click, 1
Menu, Tray, Tip, Calcute
Menu, Tray, Add, Calcute, Tray_Show
Menu, Tray, ToggleEnable, Calcute
Menu, Tray, Default, Calcute
Menu, Tray, Add
Menu, Tray, Add, 帮助(&H), Tray_Help
Menu, Tray, Add
Menu, Tray, Add, 重启(&R), Tray_Reload
Menu, Tray, Add, 退出(&X), Tray_Exit
Menu, Tray, NoStandard

file = %A_Temp%\Calcute-temp.ahk ; any unused filename

Prec = 0.12 ; default floating point precision

gui, font, S16 W700 Q5, Consolas
Gui Add, ComboBox, X0 Y0 W256 vExpr
Gui Add, Button, Default, OK ; button activated by Enter, Gui Show cuts it off

;Loop 9 ; Alt-number buttons for setting precision
Gui,+ToolWindow -Caption +Border
Gui Show, H32 W256 Center
;MouseGetPos, mouseX, mouseY
;mouseX := mouseX+10
;Gui Show, H32 W300 X%mouseX% Y%mouseY% ; calculator window: cut off unnecessary parts
Return

Tray_Show:
Menu, Tray, Show
Return
Tray_Reload:
Reload
Return
Tray_Exit:
ExitApp
Return

Tray_Help:
Msgbox, 0, Help, Win+J 显示程序
Return

$#J::
$NumLock::
/*
    MouseGetPos, mouseX, mouseY
    mouseX := mouseX+10
    Gui Show, H32 W300 X%mouseX% Y%mouseY% ; calculator window: cut off unnecessary parts
*/
    Gui Show, H32 W256 Center
Return

GuiEscape:
    Gui Show, Hide ; hide, keep data for next run
Return



ButtonOK: ; calculate
    GuiControlGet Expr,,Expr ; get Expr from ComboBox
    GuiControl,,Expr,%Expr% ; append Expr to internal ComboBox list
    Expr := RegExReplace(Expr, "[^\b\d`%\^\.\+\-\*\`/\(\)]") ;格式化表达式
    Expr := RegExReplace(Expr, "(\d*)(\.?)(\d+)%", "($0/100)") ;解决%运算-1
    Expr := RegExReplace(Expr, "%") ;解决%运算-2
    Expr := RegExReplace(Expr, "\^", "**") ;解决次幂运算
    ;MsgBox, %Expr%
    FileDelete %file% ; delete old temporary file -> write new
    FileAppend, #NoTrayIcon`nSetFormat Float`,%Prec%`nFileDelete %file%`n%pre%`nFileAppend `% %Expr%`,%file%,%file%
    RunWait, %A_AhkPath% %file% ; run AHK to execute temp script, evaluate expression
    FileRead, Result, %file% ; get result
    FileDelete, %file%
    ;IfInString, Result, `.
    ;    Result := RegExReplace(Result, "(\.0+$|0+$)")
    ;MsgBox, %Result%
    Result := Round(Result, 6)
    Result := RegExReplace(Result, "(?=\.)(.*?)\.?0*$","$1")
    GuiControl,,Expr,%Result% ; append Result to internal ComboBox list
    N += 2 ; count lines
    GuiControl Choose,Expr,%N% ; show Result
Return
查找这个用户的全部帖子
表示感谢 引用并回复 移动视图置页面顶端
[+] 1用户表示感谢100730088
2014-09-27, 01 : 32
RE: 写了一个计算器,支持四则运算等,只是精度有问题,请教
小界面很精致,还有历史功能。赞。

wiz,ahkscript,wiki,zhihu,ahk8,Github,多个QQ群,贴吧,
查找这个用户的全部帖子
表示感谢 引用并回复 移动视图置页面顶端
发表回复 


论坛跳转:


联系我们 | Autohotkey 中文站 | 回到顶部 | 回到正文区 | 精简(归档)模式 | RSS 聚合