Das in Arrays umwandeln

  • Nabend

    Will ja jetz endlich mal bissl was mit arrays machen.

    So hab ich schnell en kleines beispiel-prog gemacht.
    En Rechner, den eigl. jeder in 5 Minuten hin bekommen sollte.

    So, jetz is der Code 71 Zeilen lang.
    Eigl. viel zu viel für so was einfaches.

    Wie also, könnte ich diesen Code nun mit Array kürzen?
    ( Kleine erklärung wäre super )

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <EditConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    #Region ### START Koda GUI section ###
    $Form1_1 = GUICreate("Rechner", 184, 186, 554, 408)
    $Input6 = GUICtrlCreateInput("", 16, 16, 110, 20, BitOR($ES_CENTER,$ES_AUTOHSCROLL, $ES_NUMBER))
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    $Button1 = GUICtrlCreateButton("1", 16, 48, 25, 25, 0)
    $Button2 = GUICtrlCreateButton("2", 48, 48, 25, 25, 0)
    $Button3 = GUICtrlCreateButton("3", 80, 48, 25, 25, 0)
    $Button4 = GUICtrlCreateButton("4", 16, 80, 25, 25, 0)
    $Button5 = GUICtrlCreateButton("5", 48, 80, 25, 25, 0)
    $Button6 = GUICtrlCreateButton("6", 80, 80, 25, 25, 0)
    $Button7 = GUICtrlCreateButton("7", 16, 112, 25, 25, 0)
    $Button8 = GUICtrlCreateButton("8", 48, 112, 25, 25, 0)
    $Button9 = GUICtrlCreateButton("9", 80, 112, 25, 25, 0)
    $Button10 = GUICtrlCreateButton("0", 48, 144, 25, 25, 0)
    $Button11 = GUICtrlCreateButton(".", 16, 144, 25, 25, 0)
    $Button12 = GUICtrlCreateButton("+", 120, 48, 41, 25, 0)
    $Button13 = GUICtrlCreateButton("-", 120, 80, 41, 25, 0)
    $Button14 = GUICtrlCreateButton("*", 120, 112, 41, 25, 0)
    $Button15 = GUICtrlCreateButton("/", 120, 144, 41, 25, 0)
    $Button16 = GUICtrlCreateButton("=", 80, 144, 25, 25, 0)
    $Button17 = GUICtrlCreateButton("Clear", 128, 16, 33, 20)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "1")
    Case $Button2
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "2")
    Case $Button3
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "3")
    Case $Button4
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "4")
    Case $Button5
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "5")
    Case $Button6
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "6")
    Case $Button7
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "7")
    Case $Button8
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "8")
    Case $Button9
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "9")
    Case $Button10
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "0")
    Case $Button11
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", ".")
    Case $Button12
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "+")
    Case $Button13
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "-")
    Case $Button14
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "*")
    Case $Button15
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", "/")
    Case $Button16
    $Input6Read = GUICtrlRead ($Input6)
    $Execute = Execute ($Input6Read)
    GUICtrlSetData ($Input6, $Execute)
    Case $Button17
    GUICtrlSetData ($Input6, "")
    EndSwitch
    WEnd

    [/autoit]

    Thx schonmal ^^

    MfG

    • Offizieller Beitrag

    Hi,
    ja mit einem Array kannst du kürzen. Erstelle alle Button-ID's in einem Array.
    In der While-Schleife verwendest du dann eine Schleife durch das Array um auf die Button-ID's zu prüfen.

    Spoiler anzeigen
    [autoit]

    ; Die ID's der Button in einem Array erstellen
    Global $aButton[17] = [ _
    GUICtrlCreateButton("1", 16, 48, 25, 25, 0), _
    GUICtrlCreateButton("2", 48, 48, 25, 25, 0), _
    GUICtrlCreateButton("3", 80, 48, 25, 25, 0), _
    GUICtrlCreateButton("4", 16, 80, 25, 25, 0), _
    GUICtrlCreateButton("5", 48, 80, 25, 25, 0), _
    GUICtrlCreateButton("6", 80, 80, 25, 25, 0), _
    GUICtrlCreateButton("7", 16, 112, 25, 25, 0), _
    GUICtrlCreateButton("8", 48, 112, 25, 25, 0), _
    GUICtrlCreateButton("9", 80, 112, 25, 25, 0), _
    GUICtrlCreateButton("0", 48, 144, 25, 25, 0), _
    GUICtrlCreateButton(".", 16, 144, 25, 25, 0), _
    GUICtrlCreateButton("+", 120, 48, 41, 25, 0), _
    GUICtrlCreateButton("-", 120, 80, 41, 25, 0), _
    GUICtrlCreateButton("*", 120, 112, 41, 25, 0), _
    GUICtrlCreateButton("/", 120, 144, 41, 25, 0), _
    GUICtrlCreateButton("=", 80, 144, 25, 25, 0), _
    GUICtrlCreateButton("Clear", 128, 16, 33, 20)]

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $aButton[15]
    GUICtrlSetData ($Input6, Execute (GUICtrlRead ($Input6)))
    Case $aButton[16]
    GUICtrlSetData ($Input6, "")
    EndSwitch
    ; in einer Schleife prüfen, welches Arrayelement mit $nMsg übereinstimmt
    For $i = 0 To UBound($aButton) -3 ; die letzten beiden brauchen nicht abgefragt werden
    If $nMsg = $aButton[$i] Then
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", ControlGetText($Form1_1, "", $aButton[$i]))
    ExitLoop
    EndIf
    Next
    WEnd

    [/autoit]
  • Hehe, super thx, werds mir gleich mal durchgucken.
    Hoffentlich hab ich das denne mal bald drauf mit diesen Arrays -.-

  • Kürzer krieg ichs nich :D

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <EditConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $Form1_1 = GUICreate("Rechner", 184, 186, 554, 408)
    $Input6 = GUICtrlCreateInput("", 16, 16, 110, 20, BitOR($ES_CENTER,$ES_AUTOHSCROLL, $ES_NUMBER))
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Local $y = 48, $x = 16, $width = 25
    Global $Button = StringSplit('1|4|7|.|2|5|8|0|3|6|9|=|+|-|*|/', '|', 2)
    For $i = 0 To Ubound($Button) - 1
    If $i = 12 Then
    $x = 120
    $width = 41
    EndIf
    $Button[$i] = GUICtrlCreateButton($Button[$i], $x, $y, $width, 25, 0)
    $y += 32
    If $y > 144 Then
    $y = 48
    $x += 32
    EndIf
    Next
    $Button17 = GUICtrlCreateButton("Clear", 128, 16, 33, 20)
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button[11]
    GUICtrlSetData ($Input6, Execute (GUICtrlRead ($Input6)))
    EndSwitch
    For $j = 0 To UBound($Button)-1
    If $nMsg = $Button[$j] AND $j <> 11 Then
    ControlCommand ($Form1_1, "", $Input6, "EditPaste", ControlGetText($Form1_1, "", $Button[$j]))
    ExitLoop
    EndIf
    Next
    WEnd

    [/autoit]
    • Offizieller Beitrag

    Es geht schon noch etwas kürzer: ;)

    Spoiler anzeigen
    [autoit]


    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <EditConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $Form1_1 = GUICreate("Rechner", 184, 186, 554, 408)
    $Input6 = GUICtrlCreateInput("", 16, 16, 110, 20, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_NUMBER))
    GUICtrlSetFont(-1, 10, 400, 0, "Arial")
    Global $Button = StringSplit('1|4|7|.|2|5|8|0|3|6|9|=|+|-|*|/', '|', 2)
    For $i = 0 To UBound($Button) - 1
    $Button[$i] = GUICtrlCreateButton($Button[$i], 16 + Int($i / 4) * 32 + ($i > 11) * 8, 48 + Mod($i, 4) * 32, 25 + ($i > 11) * 16, 25)
    Next
    $Button17 = GUICtrlCreateButton("Clear", 128, 16, 33, 20)
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button[11]
    GUICtrlSetData($Input6, Execute(GUICtrlRead($Input6)))
    EndSwitch
    For $j = 0 To UBound($Button) - 1
    If $nMsg = $Button[$j] And $j <> 11 Then
    ControlCommand($Form1_1, "", $Input6, "EditPaste", ControlGetText($Form1_1, "", $Button[$j]))
    ExitLoop
    EndIf
    Next
    WEnd

    [/autoit]
  • ich habs mal auf 11 Zeichen gekürzt :D

    Spoiler anzeigen
    [autoit]


    Run("calc")

    [/autoit]

    Sorry hab n Clown gefühstückt 8o

    Zitat

    Laughing Man

    "I thought, what I'd do was, I'd pretend I was one of those deaf-mutes"

  • Eine "einfache" Schleife um die vorher statischen Knöpfe, mit Hilfe einer dynamischen Formel darzustellen :)

    Guck dir einfach die Anordnung der 16 Knöpfe an und die Parameter für GUIctrlcreatebutton - oder veränder die Werte um somit z.B. den Abstand der Knöpfe zu ändern :thumbup:

    Zitat

    Laughing Man

    "I thought, what I'd do was, I'd pretend I was one of those deaf-mutes"

  • [autoit]


    Global $Button = StringSplit('1|4|7|.|2|5|8|0|3|6|9|=|+|-|*|/', '|', 2) ;hier werden die zeichen für deinen rechner definiert und per stringsplit aufgeteilt
    For $i = 0 To UBound($Button) - 1 ;jedes zeichen durchgehen und dafür ...
    $Button[$i] = GUICtrlCreateButton($Button[$i], 16 + Int($i / 4) * 32 + ($i > 11) * 8, 48 + Mod($i, 4) * 32, 25 + ($i > 11) * 16, 25) ; einen button erstellen und die nötigen variablen berechnen
    Next

    [/autoit]


    Vielleicht solltest du das zum Verständnis aufteilen, jeweils für $i einen wert einsetzen und dir das als msgbox mal anzeigen lassen?

    P.S. besonders viel Sinn scheint mir das nicht zu machen, cool ists aber trotzdem ;)
    Hab grad selbst versucht sowas für eine GUI zu erstellen und dafür muss man sich ganz schön das Gehirn verbiegen - mit Koda gehts doch einfacher :D

  • oha
    da scheint ja mal vieles zu gehn.

    Ich speicher mir die seite mal ab.
    ( Hab nen extra ordner dafür :rofl: )

    Dann kann ich mirs nach bedarf angucken.
    Thx auf jeden fall für die super antworten ^^
    auch wenn einige dafon nen IQ von 200 verraussetzen :rofl:

  • wow nice Oscar! nich übel =P

    hmm also da gibts eigendlich nich viel zu erklären, nur drauf kommen muss man!

    [autoit]

    Int($i / 4) - Dividiert eine Zahl durch 4 und schneidet alle Nachkommastellen ab.

    [/autoit][autoit][/autoit][autoit]

    ($i > 11)*8 - Wenn die Bedingung $i > 11 erfüllt ist, wird eine 1 zurückgegeben, wodurch somit 8 addiert werden.

    [/autoit][autoit][/autoit][autoit]

    Mod($i, 4) - Dividiert den größten gemeinsamen Vielfachen einer Zahl durch 4 und gibt den Rest der Zahl zurück.

    [/autoit]

    Beispiel: $i = 14

    [autoit]

    Int(14/4) => Int(3,5) => 3
    ($14 > 11)*8 => Bedingung erfüllt => 1*8
    Mod(14,4) => Größter gemeinsamer Vielfacher von 14 und 4 ist 12 => 14-12 = 2

    [/autoit]

    Respekt ;]