AlastorESP

AutoIt - Calculadora muy basica

Mar 5th, 2011
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 6.74 KB | None | 0 0
  1. #cs ----------------------------------------------------------------------------
  2.  
  3.     AutoIt Version: 3.3.6.1
  4.     Author:         Alastor [ZKFA Team]
  5.  
  6.     Script Function:
  7.     Una calculadora muy simple.
  8.  
  9. #ce ----------------------------------------------------------------------------
  10.  
  11. #include <ButtonConstants.au3>
  12. #include <EditConstants.au3>
  13. #include <GUIConstantsEx.au3>
  14. #include <WindowsConstants.au3>
  15.  
  16. Global $Buffer = 0
  17. Global $Operacion = 0
  18.  
  19. #region #GUI Calculadora 1.2.kxf
  20. $GUI = GUICreate("Calculadora 1.2", 253, 236, 191, 153)
  21.  
  22. $Resultado = GUICtrlCreateInput("", 8, 8, 239, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL, $ES_READONLY, $ES_NUMBER))
  23. GUICtrlSetFont($Resultado, 10, 400, 0, "MS Sans Serif")
  24. GUICtrlSetColor($Resultado, 0x0000FF)
  25.  
  26. $Boton_Retroceso = GUICtrlCreateButton("Retroceso", 40, 40, 63, 29, $WS_GROUP)
  27. $Boton_CE = GUICtrlCreateButton("CE", 112, 40, 62, 29, $WS_GROUP)
  28. $Boton_C = GUICtrlCreateButton("C", 184, 40, 62, 29, $WS_GROUP)
  29.  
  30. $Boton_7 = GUICtrlCreateButton("7", 8, 80, 36, 29, $WS_GROUP)
  31. $Boton_8 = GUICtrlCreateButton("8", 56, 80, 36, 29, $WS_GROUP)
  32. $Boton_9 = GUICtrlCreateButton("9", 104, 80, 36, 29, $WS_GROUP)
  33. $Boton_Dividir = GUICtrlCreateButton("/", 152, 80, 36, 29, $WS_GROUP)
  34.  
  35. $Boton_4 = GUICtrlCreateButton("4", 8, 120, 36, 29, $WS_GROUP)
  36. $Boton_5 = GUICtrlCreateButton("5", 56, 120, 36, 29, $WS_GROUP)
  37. $Boton_6 = GUICtrlCreateButton("6", 104, 120, 36, 29, $WS_GROUP)
  38. $Boton_Multiplicar = GUICtrlCreateButton("*", 152, 120, 36, 29, $WS_GROUP)
  39.  
  40. $Boton_1 = GUICtrlCreateButton("1", 8, 160, 36, 29, $WS_GROUP)
  41. $Boton_2 = GUICtrlCreateButton("2", 56, 160, 36, 29, $WS_GROUP)
  42. $Boton_3 = GUICtrlCreateButton("3", 104, 160, 36, 29, $WS_GROUP)
  43. $Boton_Restar = GUICtrlCreateButton("-", 152, 160, 36, 29, $WS_GROUP)
  44.  
  45. $Boton_0 = GUICtrlCreateButton("0", 8, 200, 36, 29, $WS_GROUP)
  46. $Boton_Signo = GUICtrlCreateButton("+/-", 56, 200, 36, 29, $WS_GROUP)
  47. $Boton_Coma = GUICtrlCreateButton(",", 104, 200, 36, 29, $WS_GROUP)
  48. $Boton_Sumar = GUICtrlCreateButton("+", 152, 200, 36, 29, $WS_GROUP)
  49. $Boton_Igual = GUICtrlCreateButton("=", 199, 200, 44, 29, $WS_GROUP)
  50. GUICtrlSetFont($Boton_Igual, 10, 400, 0, "MS Sans Serif")
  51. #endregion #GUI Calculadora 1.2.kxf
  52.  
  53. Local $AccelKeys[18][2] =  [["{NUMPAD0}", $Boton_0], _
  54.                             ["{NUMPAD1}", $Boton_1], _
  55.                             ["{NUMPAD2}", $Boton_2], _
  56.                             ["{NUMPAD3}", $Boton_3], _
  57.                             ["{NUMPAD4}", $Boton_4], _
  58.                             ["{NUMPAD5}", $Boton_5], _
  59.                             ["{NUMPAD6}", $Boton_6], _
  60.                             ["{NUMPAD7}", $Boton_7], _
  61.                             ["{NUMPAD8}", $Boton_8], _
  62.                             ["{NUMPAD9}", $Boton_9], _
  63.                             ["{NUMPAD9}", $Boton_9], _
  64.                             ["{NUMPADSUB}", $Boton_Restar], _
  65.                             ["{NUMPADADD}", $Boton_Sumar], _
  66.                             ["{NUMPADMULT}", $Boton_Multiplicar], _
  67.                             ["{NUMPADDIV}", $Boton_Dividir], _
  68.                             ["{BACKSPACE}", $Boton_Retroceso], _
  69.                             ["{DELETE}", $Boton_CE], _
  70.                             ["{ENTER}", $Boton_Igual]]
  71.  
  72. GUISetAccelerators($AccelKeys, $GUI)
  73.  
  74. GUISetState(@SW_SHOW)
  75.  
  76. While 1
  77.     Switch GUIGetMsg()
  78.         Case $GUI_EVENT_CLOSE   ; Si se cierra la GUI
  79.             Exit 0              ; Salir con codigo de error 0
  80.  
  81.         Case $Boton_Retroceso                                   ; Boton para borrar el ultimo caracter
  82.             $TxtActual = GUICtrlRead($Resultado)                ; Se lee el numero actual
  83.             $TxtMenos1 = StringTrimRight($TxtActual, 1)         ; Se le quita el utlimo numero
  84.             GUICtrlSetData($Resultado, $TxtMenos1)              ; Se modifica
  85.  
  86.         Case $Boton_CE                      ; Boton para borrar todo
  87.             $Buffer = 0                     ; $Buffer se utiliza para guardar el numero anterior
  88.             $Operacion = 0                  ; $Operacion se utiliza para saber que operacion realizar
  89.             GUICtrlSetData($Resultado, "")  ; Se borra la input de resultados
  90.  
  91.         Case $Boton_C                       ; Boton para borrar solo la input de resultados
  92.             GUICtrlSetData($Resultado, "")
  93.  
  94.         Case $Boton_1                               ; Botones del 0 al 9 para agregar el numero correspondiente
  95.             GUICtrlSetData($Resultado, '1', 0)
  96.  
  97.         Case $Boton_2
  98.             GUICtrlSetData($Resultado, '2', 0)
  99.  
  100.         Case $Boton_3
  101.             GUICtrlSetData($Resultado, '3', 0)
  102.  
  103.         Case $Boton_4
  104.             GUICtrlSetData($Resultado, '4', 0)
  105.  
  106.         Case $Boton_5
  107.             GUICtrlSetData($Resultado, '5', 0)
  108.  
  109.         Case $Boton_6
  110.             GUICtrlSetData($Resultado, '6', 0)
  111.  
  112.         Case $Boton_7
  113.             GUICtrlSetData($Resultado, '7', 0)
  114.  
  115.         Case $Boton_8
  116.             GUICtrlSetData($Resultado, '8', 0)
  117.  
  118.         Case $Boton_9
  119.             GUICtrlSetData($Resultado, '9', 0)
  120.  
  121.         Case $Boton_0
  122.             GUICtrlSetData($Resultado, '0', 0)
  123.  
  124.         Case $Boton_Coma
  125.             GUICtrlSetData($Resultado, ',', 0)
  126.  
  127.         Case $Boton_Signo                                           ; Boton para modificar el signo actual (Negativo / Positivo)
  128.             $TxtActual = GUICtrlRead($Resultado)                    ; Se lee el valor actual
  129.  
  130.             $Signo = StringLeft($TxtActual, 1)                      ; Se lee el primer caracter
  131.             If $Signo == '-' Then                                   ; Si es negativo
  132.                 $CambioDeSigno = StringTrimLeft($TxtActual, 1)      ; Se borra el signo
  133.             Else                                                    ; Si es positivo (No tiene signo)
  134.                 $CambioDeSigno = '-' & $TxtActual                   ; Se agrega el signo
  135.             EndIf
  136.  
  137.             GUICtrlSetData($Resultado, $CambioDeSigno)
  138.  
  139.         Case $Boton_Dividir
  140.             If $Operacion == 0 Then                 ; Si no hay ninguna operacion definida
  141.                 $Buffer = GUICtrlRead($Resultado)   ; Se guarda el valor actual
  142.                 $Operacion = '/'                    ; Se define la operacion
  143.                 GUICtrlSetData($Resultado, "")      ; Se borra la entrada de resultados
  144.             Else
  145.                 $Operacion = '/'                    ; En caso contrario solo se modifica la operacion
  146.             EndIf
  147.  
  148.         Case $Boton_Multiplicar
  149.             If $Operacion == 0 Then
  150.                 $Buffer = GUICtrlRead($Resultado)
  151.                 $Operacion = '*'
  152.                 GUICtrlSetData($Resultado, "")
  153.             Else
  154.                 $Operacion = '*'
  155.             EndIf
  156.  
  157.         Case $Boton_Restar
  158.             If $Operacion == 0 Then
  159.                 $Buffer = GUICtrlRead($Resultado)
  160.                 $Operacion = '-'
  161.                 GUICtrlSetData($Resultado, "")
  162.             Else
  163.                 $Operacion = '-'
  164.             EndIf
  165.  
  166.         Case $Boton_Sumar
  167.             If $Operacion == 0 Then
  168.                 $Buffer = GUICtrlRead($Resultado)
  169.                 $Operacion = '+'
  170.                 GUICtrlSetData($Resultado, "")
  171.             Else
  172.                 $Operacion = '+'
  173.             EndIf
  174.  
  175.         Case $Boton_Igual
  176.             If $Operacion = Not 0 Then                                      ; Si hay una operacion definida
  177.                 $TxtActual = GUICtrlRead($Resultado)                        ; Se lee el valor actual
  178.  
  179.                 $AnteriorArreglado = StringReplace($Buffer, ',', '.')       ; Se reemplazan las comas por puntos (Para las operaciones decimales en autoit)
  180.                 $ActualArreglado = StringReplace($TxtActual, ',', '.')
  181.  
  182.                 Switch $Operacion
  183.                     Case '+'
  184.                         $Result = $AnteriorArreglado + $ActualArreglado
  185.  
  186.                     Case '-'
  187.                         $Result = $AnteriorArreglado - $ActualArreglado
  188.  
  189.                     Case '*'
  190.                         $Result = $AnteriorArreglado * $ActualArreglado
  191.  
  192.                     Case '/'
  193.                         $Result = $AnteriorArreglado / $ActualArreglado
  194.  
  195.                 EndSwitch
  196.  
  197.                 $ResultadoArreglado = StringReplace($Result, '.', ',')      ; Se vuelven a poner puntos en lugar de comas
  198.                 GUICtrlSetData($Resultado, $ResultadoArreglado)
  199.  
  200.                 $Buffer = 0
  201.                 $Operacion = 0
  202.             EndIf
  203.  
  204.     EndSwitch
  205. WEnd
Advertisement
Add Comment
Please, Sign In to add comment