Jump to content

Full GUI Calculator in under 40 lines


baroquebob
 Share

Recommended Posts

I've been experimenting with creating a simple GUI calculator (with actual buttons and display), in the fewest possible lines of code.  So far, I'm at 38 lines (without white space).  Unfortunately the script does not have any error handling, nor does it recognize numeric keypresses from the keyboard, but it otherwise seems to work okay.  The code doesn't necessarily follow good programming practice, but I was just aiming for 'small'.  Just sharing in case somebody else might find it interesting, and maybe would like to add error handling or keyboard input, (of course, following my minimalist approach.  :-)  )  Any thoughts on keeping the functionality as-is, but making the script even smaller?

Opt("GUIOnEventMode", 1)
DIM $BA[] = ["7","8","9","+","C","4","5","6","-","SqRt","1","2","3","*",".","0","(",")","/","="]
Global $x = 8, $y = 50
GUICreate("Calc", 360, 330, 229, 118)
$D = GUICtrlCreateInput("", 8, 8, 344, 31, BitOR(0x00000080,2))
For $j = 0 to Ubound($BA) - 1 ;make the buttons
    btn($BA[$j])
Next
GUISetState(@SW_SHOW)
GUISetOnEvent(-3, "Bye")
While 1
    Sleep(25) ;Throttle
WEnd
Func btn($t) ;make button, associate event for each button press
    GUICtrlCreateButton($t, $x, $y, 65, 63)
    GUICtrlSetOnEvent(-1, "BP")
    GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
    $x += 70
    If $x > 350 Then ;move down and left for next row of buttons
        $x = 8
        $y += 70
    EndIf
EndFunc
Func BP() ;handle the button press
    $v = GuiCtrlRead(@GUI_CtrlId)
    If $v = "C" Then
        GuiCtrlSetData($D, "")
    ElseIf $v = "SqRt" Then ;Solve expression, then take sqrt.
        GuiCtrlSetData($D, Sqrt(Execute(GuiCtrlRead($D))))
    ElseIf $v = "=" Then ;solve the expression
        GuiCtrlSetData($D, Execute(GuiCtrlRead($D)))
    Else
        GuiCtrlSetData($D, GuiCtrlRead($D) & $v)
    EndIf
EndFunc
Func Bye()
    Exit
EndFunc

 

Link to comment
Share on other sites

... nice "Code golf". (if you like such puzzles, here you can find some funny code golf puzzles)

P.S. just for fun...
code reduced to 23 lines (apologyes for the 'brutalization' of your code :))

Local $0 = Opt("GUIOnEventMode", 1), $BA[] = ["7", "8", "9", "+", "C", "4", "5", "6", "-", "SqRt", "1", "2", "3", "*", ".", "0", "(", ")", "/", "="]
Local $hGui = GUICreate("Calc", 360, 330, 229, 118), $D = GUICtrlCreateInput("", 8, 8, 344, 31, BitOR(0x00000080, 2)), $1 = GUISetOnEvent(-3, "Bye")
For $j = 0 to UBound($BA) - 1 ;make the buttons
    $2 = GUICtrlCreateButton($BA[$j], Mod($j, 5) * 70 + 7, Int($j / 5) * 70 + 50, 65, 63) + GUICtrlSetOnEvent(-1, "BP") + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
Next
GUISetState(@SW_SHOW)
While Sleep(25) ;Throttle
WEnd
Func BP() ;handle the button press
    $v = GUICtrlRead(@GUI_CtrlId)
    If $v = "C" Then
        GUICtrlSetData($D, "")
    ElseIf $v = "SqRt" Then ;Solve expression, then take sqrt.
        GUICtrlSetData($D, Sqrt(Execute(GUICtrlRead($D))))
    ElseIf $v = "=" Then ;solve the expression
        GUICtrlSetData($D, Execute(GUICtrlRead($D)))
    Else
        GUICtrlSetData($D, GUICtrlRead($D) & $v)
    EndIf
EndFunc   ;==>BP
Func Bye()
    Exit
EndFunc   ;==>Bye

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

12 lines:

Local $0 = Opt("GUIOnEventMode", 1), $BA[] = ["7", "8", "9", "+", "C", "4", "5", "6", "-", "SqRt", "1", "2", "3", "*", ".", "0", "(", ")", "/", "="], $hGui = GUICreate("Calc", 360, 330, 229, 118), $D = GUICtrlCreateInput("", 8, 8, 344, 31, BitOR(0x00000080, 2)), $1 = GUISetOnEvent(-3, "Bye") * GUISetState(@SW_SHOW)
For $j = 0 to UBound($BA) - 1 ;make the buttons
    $2 = GUICtrlCreateButton($BA[$j], Mod($j, 5) * 70 + 7, Int($j / 5) * 70 + 50, 65, 63) + GUICtrlSetOnEvent(-1, "BP") + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
Next
While Sleep(100) ;Throttle
WEnd
Func BP() ;handle the button press
   $v = GUICtrlRead(@GUI_CtrlId) = "C" ? GUICtrlSetData($D, "") : GUICtrlRead(@GUI_CtrlId) = "SqRt" ? GUICtrlSetData($D, Sqrt(Execute(GUICtrlRead($D)))) : GUICtrlRead(@GUI_CtrlId)= "=" ? GUICtrlSetData($D, Execute(GUICtrlRead($D))) : GUICtrlSetData($D, GUICtrlRead($D) & GUICtrlRead(@GUI_CtrlId))
EndFunc   ;==>BP
Func Bye()
    Exit
EndFunc   ;==>Bye

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

9 lines:

Local $0 = Opt("GUIOnEventMode", 1), $BA[] = ["7", "8", "9", "+", "C", "4", "5", "6", "-", "SqRt", "1", "2", "3", "*", ".", "0", "(", ")", "/", "="], $hGui = GUICreate("Calc", 360, 330, 229, 118), $D = GUICtrlCreateInput("", 8, 8, 344, 31, BitOR(0x00000080, 2)), $1 = GUISetOnEvent(-3, "BP") * GUISetState(@SW_SHOW)
For $j = 0 To UBound($BA) - 1 ;make the buttons
    $2 = GUICtrlCreateButton($BA[$j], Mod($j, 5) * 70 + 7, Int($j / 5) * 70 + 50, 65, 63) + GUICtrlSetOnEvent(-1, "BP") + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
Next
While Sleep(100) ;Throttle
WEnd
Func BP() ;handle the button press
    If (GUICtrlRead(@GUI_CtrlId) = "C" ? GUICtrlSetData($D, "") : GUICtrlRead(@GUI_CtrlId) = "SqRt" ? GUICtrlSetData($D, Sqrt(Execute(GUICtrlRead($D)))) : GUICtrlRead(@GUI_CtrlId) = "=" ? GUICtrlSetData($D, Execute(GUICtrlRead($D))) : GUICtrlSetData($D, GUICtrlRead($D) & GUICtrlRead(@GUI_CtrlId))) And @GUI_CtrlId = -3 Then Exit
EndFunc   ;==>BP

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

down to 5 lines and the feeling at least 1 more should be possible with the ? operator on the GUIGetMsg(1):sweating:

Local $j=0, $2=0, $amsg[]=[0,0,0,0,0], $BA[] = ["7", "8", "9", "+", "C", "4", "5", "6", "-", "SqRt", "1", "2", "3", "*", ".", "0", "(", ")", "/", "="], $hGui = GUICreate("Calc", 360, 330, 229, 118), $D = GUICtrlCreateInput("", 8, 8, 344, 31, BitOR(0x00000080, 2)), $1 = GUISetState(@SW_SHOW),$2 = GUICtrlCreateButton($BA[0],7,50,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"), $2 = GUICtrlCreateButton($BA[1],77,50,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[2],147,50,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[3],217,50,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[4],287,50,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[5],7,120,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[6],77,120,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[7],147,120,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[8],217,120,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[9],287,120,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[10],7,190,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[11],77,190,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[12],147,190,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[13],217,190,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[14],287,190,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[15],7,260,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[16],77,260,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[17],147,260,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[18],217,260,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif"),$2 = GUICtrlCreateButton($BA[19],287,260,65, 63) + GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
Do
    $aMsg = GUIGetMsg(1) ;Get as array ; Should be possible with a ? somewhere
    if ($aMsg[2]=0 ? $2=0  : (GUICtrlRead($aMsg[0]) = "C" ? GUICtrlSetData($D, "") : GUICtrlRead($aMsg[0]) = "SqRt" ? GUICtrlSetData($D, Sqrt(Execute(GUICtrlRead($D)))) : GUICtrlRead($aMsg[0]) = "=" ? GUICtrlSetData($D, Execute(GUICtrlRead($D))) : GUICtrlSetData($D, GUICtrlRead($D) & GUICtrlRead($aMsg[0]))) and ($aMsg[0]=-3)) Then exit
Until $aMsg[0] = -3 ; $GUI_EVENT_CLOSE

 

Edited by junkew
Link to comment
Share on other sites

Beated my own expectation down to 3 lines :D and shortened in bytes (using the $fcBtn makes it shorter could have made it $f offcourse but goal was not to obfuscate)

local $fcBtn=GUICtrlCreateButton, $j=0, $2=0, $amsg[]=[0,0,0,0,0], $BA[] = ["7", "8", "9", "+", "C", "4", "5", "6", "-", "SqRt", "1", "2", "3", "*", ".", "0", "(", ")", "/", "="], $hGui = GUICreate("Calc", 360, 330, 229, 118), $f=GUISetFont(18, 400, 0, "MS Sans Serif"), $D = GUICtrlCreateInput("", 8, 8, 344, 31, BitOR(0x00000080, 2)), $1 = GUISetState(@SW_SHOW),$2 = $fcBtn($BA[0],7,50,65, 63) , $2 = $fcBtn($BA[1],77,50,65, 63) ,$2 = $fcBtn($BA[2],147,50,65, 63) ,$2 = $fcBtn($BA[3],217,50,65, 63) ,$2 = $fcBtn($BA[4],287,50,65, 63) ,$2 = $fcBtn($BA[5],7,120,65, 63) ,$2 = $fcBtn($BA[6],77,120,65, 63) ,$2 = $fcBtn($BA[7],147,120,65, 63) ,$2 = $fcBtn($BA[8],217,120,65, 63) ,$2 = $fcBtn($BA[9],287,120,65, 63) ,$2 = $fcBtn($BA[10],7,190,65, 63) ,$2 = $fcBtn($BA[11],77,190,65, 63) ,$2 = $fcBtn($BA[12],147,190,65, 63) ,$2 = $fcBtn($BA[13],217,190,65, 63) ,$2 = $fcBtn($BA[14],287,190,65, 63) ,$2 = $fcBtn($BA[15],7,260,65, 63) ,$2 = $fcBtn($BA[16],77,260,65, 63) ,$2 = $fcBtn($BA[17],147,260,65, 63) ,$2 = $fcBtn($BA[18],217,260,65, 63) ,$2 = $fcBtn($BA[19],287,260,65, 63)
Do
Until  ((assign("aMsg",GUIGetMsg(1))=1) and ($aMsg[2]=0 ? $2=0  : (GUICtrlRead($aMsg[0]) = "C" ? GUICtrlSetData($D, "") : GUICtrlRead($aMsg[0]) = "SqRt" ? GUICtrlSetData($D, Sqrt(Execute(GUICtrlRead($D)))) : GUICtrlRead($aMsg[0]) = "=" ? GUICtrlSetData($D, Execute(GUICtrlRead($D))) : GUICtrlSetData($D, GUICtrlRead($D) & GUICtrlRead($aMsg[0])))  and ($aMsg[0]=-3))=true) or ($aMsg[0] = -3)  ; $GUI_EVENT_CLOSE

I think I am out of tricks but maybe an MVP knows how to get it into a one liner

Edited by junkew
Title can change now to under 4 instead of 40 lines ;-)
Link to comment
Share on other sites

a 4 line version was also missing, here is one (... is few bytes less than the nice 3 lines version by @junkew)

Global $iMsg, $j, $BA[] = [7, 8, 9, '+', 'C', 4, 5, 6, "-", "SqRt", 1, 2, 3, '*', '.', 0, '(', ')', '/', '='], $hGui = GUICreate('Calc', 360, 330, 229, 118, 0x00C00000), $D = GUICtrlCreateInput('', 8, 8, 344, 31, BitOR(0x00000080, 2)), $1 = GUISetState(@SW_SHOW), $a = DllCallbackRegister('BP', 'none', 'hwnd;uint;uint_ptr;dword'), $b = DllCall('user32.dll', 'uint_ptr', 'SetTimer', 'hwnd', 0, 'uint_ptr', 0, 'uint', 10, 'ptr', DllCallbackGetPtr($a)), $exit = MsgBox(0, "Info", "hit 'esc' or click here to exit"), $TheEnd1 = DllCallbackFree($a), $TheEnd2 = DllCall('user32.dll', 'bool', 'KillTimer', 'hwnd', 0, 'uint_ptr', $b)
Func BP($hWnd, $iMsg2, $iTimerID, $iTime) ;handle the button press
    $j = (Assign('iMsg', GUIGetMsg(0))) * 0 ? 0 :($j < UBound($BA)) ?(GUICtrlCreateButton($BA[$j], Mod($j, 5) * 70 + 7, Int($j / 5) * 70 + 50, 65, 63) + GUICtrlSetFont(-1, 18, 400, 0, 'MS Sans Serif')) * 0 + $j + 1 : UBound($BA) + 0 * ($iMsg > 3 And $iMsg < 24 ?(GUICtrlRead($iMsg) = 'C' ? GUICtrlSetData($D, '') : GUICtrlRead($iMsg) = 'SqRt' ? GUICtrlSetData($D, Sqrt(Execute(GUICtrlRead($D)))) : GUICtrlRead($iMsg) = '=' ? GUICtrlSetData($D, Execute(GUICtrlRead($D))) :(StringInStr('0123456789()+-*/.', GUICtrlRead($iMsg))) > 0 ? GUICtrlSetData($D, GUICtrlRead($D) & GUICtrlRead($iMsg)) : 0) : 20)
EndFunc   ;==>BP

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

All under 5 lines is just causes a headache :frantics:

I miss challenges like these here.
Unfortunately, I do not even have time to deal with them :(

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

Local $BA[] = ["Sin", "Tan", "Cos", "Exp", "Log", "7", "8", "9", "+", "C", "4", "5", "6", "-", "SqRt", "1", "2", "3", "*", ".", "0", "(", ")", "/", " = "]
$hGui = GUICreate("Calc", 360, 400, 229, 138, 0x10000000 + 0x20000000 + 0xC00000 + 0x80000) ;VISIBLE, MINIMIZEBOX, CAPTION, POPUP, SYSMENU
$D = GUICtrlCreateInput("", 8, 8, 344, 31, BitOR(0x00000080, 2)) ;$ES_AUTOHSCROLL, $ES_RIGHT
For $j = 0 To UBound($BA) - 1 ;make the buttons
    $BA[$j] = GUICtrlCreateButton($BA[$j], Mod($j, 5) * 70 + 7, Int($j / 5) * 70 + 50, 65, 63)
    $sB = GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
Next
While Execute(Assign("j", GUIGetMsg()) And $j <> -3)
    If $j > 0 And Assign("sB", GUICtrlRead($j)) And Call($sB, GUICtrlRead($D)) Then GUICtrlSetData($D, Call($sB, GUICtrlRead($D))) ;Sqrt; Tan; Cos..
    Execute($j - $D > 0xDEAD - @error And GUICtrlSetData($D, ($sB = "C" Or $sB = " = ") ? Execute(StringLeft($sB, 1) & GUICtrlRead($D)) : GUICtrlRead($D) & $sB))
WEnd

11 lines with a bit different method

Still 11 Lines but its a bit more concise back to dynamic 'C' and '=' buttons

and I showed how to add more named functions like Tan() Cos() Sin()

Edited by Bilgus
Adding Sin Tan Cos Exp Log
Link to comment
Share on other sites

Improved from junkew 's code into two lines
 




Hi Funs
Improved from junkew 's code into 2 line 
if not FileExists(@TempDir&"\Cal.au3") then FileWrite(@TempDir&"\Cal.au3",'local $fcBtn=GUICtrlCreateButton, $j=0, $2=0, $amsg[]=[0,0,0,0,0], $BA[] = ["7", "8", "9", "+", "C", "4", "5", "6", "-", "SqRt", "1", "2", "3", "*", ".", "0", "(", ")", "/", "="], $hGui = GUICreate("Calc", 360, 330, 229, 118), $f=GUISetFont(18, 400, 0, "MS Sans Serif"), $D = GUICtrlCreateInput("", 8, 8, 344, 31, BitOR(0x00000080, 2)), $1 = GUISetState(@SW_SHOW),$2 = $fcBtn($BA[0],7,50,65, 63) , $2 = $fcBtn($BA[1],77,50,65, 63) ,$2 = $fcBtn($BA[2],147,50,65, 63) ,$2 = $fcBtn($BA[3],217,50,65, 63) ,$2 = $fcBtn($BA[4],287,50,65, 63) ,$2 = $fcBtn($BA[5],7,120,65, 63) ,$2 = $fcBtn($BA[6],77,120,65, 63) ,$2 = $fcBtn($BA[7],147,120,65, 63) ,$2 = $fcBtn($BA[8],217,120,65, 63) ,$2 = $fcBtn($BA[9],287,120,65, 63) ,$2 = $fcBtn($BA[10],7,190,65, 63) ,$2 = $fcBtn($BA[11],77,190,65, 63) ,$2 = $fcBtn($BA[12],147,190,65, 63) ,$2 = $fcBtn($BA[13],217,190,65, 63) ,$2 = $fcBtn($BA[14],287,190,65, 63) ,$2 = $fcBtn($BA[15],7,260,65, 63) ,$2 = $fcBtn($BA[16],77,260,65, 63) ,$2 = $fcBtn($BA[17],147,260,65, 63) ,$2 = $fcBtn($BA[18],217,260,65, 63) ,$2 = $fcBtn($BA[19],287,260,65, 63)' &@CRLF & 'do' &@CRLF & 'Until (((assign("aMsg",GUIGetMsg(1))=1) and ($aMsg[2]=0 ? $2=0  : (GUICtrlRead($aMsg[0]) = "C" ? GUICtrlSetData($D, "") : GUICtrlRead($aMsg[0]) = "SqRt" ? GUICtrlSetData($D, Sqrt(Execute(GUICtrlRead($D)))) : GUICtrlRead($aMsg[0]) = "=" ? GUICtrlSetData($D, Execute(GUICtrlRead($D))) : GUICtrlSetData($D, GUICtrlRead($D) & GUICtrlRead($aMsg[0])))  and ($aMsg[0]=-3))=true) or ($aMsg[0] = -3))')
ShellExecute(@TempDir&"\Cal.au3",@TempDir)

 

Edited by lpduy
Link to comment
Share on other sites

13 minutes ago, Ascer said:

Chủ đề này là bullshit cuz tất cả chúng ta biết rằng điều quan trọng nhất là mã rõ ràng và dễ dàng phát hiện lỗi.

-1 cho cuộc thảo luận này.

But sometime we need small and smaller code to reduce file size!

If develop Autoit for mobile app that it's wolderfull

Link to comment
Share on other sites

@Ascer offcourse readability is great but challenges like this proof how well you know indepth your language of programming. In my code editor with color highlighting the short version is still readable and understandable. And if you want to write an obfuscator thes tricks are nice to know.

Link to comment
Share on other sites

4 hours ago, lpduy said:

Improved from junkew 's code into two lines

if that "trick" is allowed  then just one line it's enough

Local $1 = FileWrite(FileOpen(@TempDir & "\Cal.au3", 2), "Global $iMsg, $j, $BA[] = [7, 8, 9, '+', 'C', 4, 5, 6, '-', 'SqRt', 1, 2, 3, '*', '.', 0, '(', ')', '/', '='], $hGui = GUICreate('Calc', 360, 330, 229, 118, 0x00C00000), $D = GUICtrlCreateInput('', 8, 8, 344, 31, BitOR(0x00000080, 2)), $1 = GUISetState(@SW_SHOW), $a = DllCallbackRegister('BP', 'none', 'hwnd;uint;uint_ptr;dword'), $b = DllCall('user32.dll', 'uint_ptr', 'SetTimer', 'hwnd', 0, 'uint_ptr', 0, 'uint', 10, 'ptr', DllCallbackGetPtr($a)), $exit = MsgBox(0, 'Info', 'click here to exit'), $TheEnd1 = DllCallbackFree($a), $TheEnd2 = DllCall('user32.dll', 'bool', 'KillTimer', 'hwnd', 0, 'uint_ptr', $b)" & @CRLF & "Func BP($hWnd, $iMsg2, $iTimerID, $iTime)" & @CRLF & "$j = (Assign('iMsg', GUIGetMsg(0))) * 0 ? 0 :($j < UBound($BA)) ?(GUICtrlCreateButton($BA[$j], Mod($j, 5) * 70 + 7, Int($j / 5) * 70 + 50, 65, 63) + GUICtrlSetFont(-1, 18, 400, 0, 'MS Sans Serif')) * 0 + $j + 1 : UBound($BA) + 0 * ($iMsg > 3 And $iMsg < 24 ?(GUICtrlRead($iMsg) = 'C' ? GUICtrlSetData($D, '') : GUICtrlRead($iMsg) = 'SqRt' ? GUICtrlSetData($D, Sqrt(Execute(GUICtrlRead($D)))) : GUICtrlRead($iMsg) = '=' ? GUICtrlSetData($D, Execute(GUICtrlRead($D))) :(StringInStr('0123456789()+-*/.', GUICtrlRead($iMsg))) > 0 ? GUICtrlSetData($D, GUICtrlRead($D) & GUICtrlRead($iMsg)) : 0) : 20)" & @CRLF & "EndFunc"), $2 = Run(@AutoItExe & " " & @TempDir & "\Cal.au3")

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...