Advertisement
logix

Scompositore

Mar 27th, 2012
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.00 KB | None | 0 0
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_Compression=4
  3. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  4. #NoTrayIcon
  5. #include <IE.au3>
  6. #include <ButtonConstants.au3>
  7. #include <EditConstants.au3>
  8. #include <GUIConstantsEx.au3>
  9. #include <WindowsConstants.au3>
  10.  
  11. $IE = _IECreateEmbedded()
  12. If $IE = 0 Then
  13.     MsgBox(16, "Errore", "Impossibile inizializzare IE.")
  14.     Exit 1
  15. EndIf
  16.  
  17. $FormTitle = "Scompositore - GianluTB"
  18.  
  19. $Form1 = GUICreate($FormTitle, 465, 97, 192, 124)
  20. $Form_NumberInput = GUICtrlCreateInput("", 8, 8, 449, 21, BitOR($ES_CENTER, $ES_NUMBER))
  21. $Form_Scomponi = GUICtrlCreateButton("Scomponi", 8, 40, 75, 25)
  22. $Form_IEObject = GUICtrlCreateObj($IE, 90, 35, 350, 50)
  23. GUISetState(@SW_SHOW)
  24.  
  25. $ScomponiStato = True
  26.  
  27. While 1
  28.     If Int(GUICtrlRead($Form_NumberInput)) > 0 And $ScomponiStato = False Then
  29.         GUICtrlSetState($Form_Scomponi, $GUI_ENABLE)
  30.         $ScomponiStato = True
  31.     ElseIf Int(GUICtrlRead($Form_NumberInput)) < 1 And $ScomponiStato = True Then
  32.         GUICtrlSetState($Form_Scomponi, $GUI_DISABLE)
  33.         $ScomponiStato = False
  34.     EndIf
  35.  
  36.     $nMsg = GUIGetMsg()
  37.     Switch $nMsg
  38.         Case $GUI_EVENT_CLOSE
  39.             Exit
  40.         Case $Form_NumberInput
  41.         Case $Form_Scomponi
  42.             $n = GUICtrlRead($Form_NumberInput)
  43.             If $n > 0 Then
  44.                 ; Salvo in $NS l'array con il numero scomposto..
  45.                 $ns = Scomponi($n)
  46.  
  47.                 ; elimino la vecchia scomposizione
  48.                 FileDelete(@TempDir & "\r.htm")
  49.  
  50.                 ; la scrivo in r.htm
  51.                 FileWrite(@TempDir & "\r.htm", MakeString($ns))
  52.  
  53.                 ; la visualizzo con iexplore..
  54.                 _IENavigate($IE, @TempDir & "\r.htm", 0)
  55.  
  56.                 ; setto il focus sull'input per velocizzare :)
  57.                 ControlFocus($FormTitle, "", $Form_NumberInput)
  58.             Else
  59.                 MsgBox(48, "Errore", "Inserisci un numero valido!")
  60.             EndIf
  61.  
  62.     EndSwitch
  63. WEnd
  64.  
  65.  
  66. Func MakeString($numeroscomposto)
  67.     ; da un array bidimensionale, prepara una stringa
  68.     ; in html per visualizzare la scomposizione..
  69.     $string = ""
  70.     For $i = 0 To UBound($numeroscomposto) - 1
  71.         $string &= $numeroscomposto[$i][0] & "<sup>" & $numeroscomposto[$i][1] & "</sup>"
  72.         If $i < UBound($numeroscomposto) - 1 Then $string &= " * "
  73.     Next
  74.     Return $string
  75. EndFunc   ;==>MakeString
  76. Func Scomponi($n)
  77.  
  78.     Dim $r[1][2]
  79.     $numero = $n
  80.     $divisore = 2
  81.     While True
  82.         If Mod($numero, $divisore) == 0 Then
  83.             $added = False
  84.             For $i = 0 To UBound($r)-1
  85.                 If $r[$i][0] = $divisore Then
  86.                     $r[$i][1] += 1
  87.                     $added = True
  88.                 EndIf
  89.             Next
  90.             If $added = False Then
  91.                 ReDim $r[UBound($r)+1][2]
  92.                 $r[UBound($r)-1][0] = $divisore
  93.                 $r[UBound($r)-1][1] = 1
  94.             EndIf
  95.  
  96.             $numero /= $divisore
  97.         Else
  98.             $divisore = nextPrime($divisore)
  99.         EndIf
  100.  
  101.         If $numero = 1 Then
  102.             ExitLoop
  103.         EndIf
  104.  
  105.     WEnd
  106.     _ArrayDelete($r,0)
  107.     Return $r
  108. EndFunc
  109.  
  110. Func isPrime($n)
  111.     For $i = 1 To $n
  112.         If Mod($n,$i) == 0 Then
  113.             If $i <> 1 And $i <> $n Then
  114.                 Return False
  115.             EndIf
  116.         EndIf
  117.     Next
  118.     Return True
  119. EndFunc
  120.  
  121. Func nextPrime($n)
  122.     $i = $n
  123.     While True
  124.         $i += 1
  125.         If isPrime($i) Then
  126.             Return $i
  127.         EndIf
  128.     WEnd
  129. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement