Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Ricerca dicotomica (binaria) - Form1.frm

Form1.frm

Caricato da: Albertking82
Scarica il programma completo

  1. '***************************************************************************
  2. '*   Copyright (C) 2007 by Veronese Alberto                                *
  3. '*
  4. '*                                                                         *
  5. '*   This program is free software; you can redistribute it and/or modify  *
  6. '*   it under the terms of the GNU General Public License as published by  *
  7. '*   the Free Software Foundation; either version 2 of the License, or     *
  8. '*   (at your option) any later version.                                   *
  9. '*                                                                         *
  10. '*   This program is distributed in the hope that it will be useful,       *
  11. '*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12. '*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13. '*   GNU General Public License for more details.                          *
  14. '*                                                                         *
  15. '*   You should have received a copy of the GNU General Public License     *
  16. '*   along with this program; if not, write to the                         *
  17. '*   Free Software Foundation, Inc.,                                       *
  18. '*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19. '***************************************************************************
  20.  
  21.  
  22.  
  23. Const n = 14
  24. Dim v(0 To n) As Integer
  25. Dim num As Integer
  26. Dim i As Integer, j As Integer, temp As Integer
  27. Dim item As Integer
  28. Dim m As Integer
  29. Dim confronti As Integer
  30. Dim flag As Boolean
  31. Dim f As Boolean
  32. Dim u As Integer
  33. Dim p As Integer
  34. Public pos As Integer
  35. Dim v2(0 To n) As Integer
  36.  
  37. Private Sub cmdcerca_Click()
  38.  
  39. On Error GoTo error
  40.  If flag Then
  41.  
  42.     pos = 0
  43.     confronti = 0
  44.     item = InputBox("Inserisci l'elemento da cercare", "Inserisci elemento")
  45.  
  46.       If Not IsNumeric(item) Then
  47.      
  48.          MsgBox "Devi inserire un numero intero", vbInformation, "Parametro errato"
  49.          Exit Sub
  50.          
  51.       End If
  52.      
  53.        For i = 0 To n
  54.  
  55.         If v2(i) = item Then
  56.            pos = i
  57.         End If
  58.    
  59.        Next i
  60.      
  61.       m = (0 + n) / 2
  62.  
  63.        If item < v(0) Or item > v(n) Then
  64.        
  65.           Txtric.Text = "Elemento non presente nel vettore"
  66.           Txtric.Text = Txtric.Text & vbCrLf & "confronti : 1"
  67.           Exit Sub
  68.          
  69.        End If
  70.  
  71.          If item = v(m) Then
  72.          
  73.             Txtric.Text = "Elemento presente nel vettore"
  74.             Txtric.Text = Txtric.Text & vbCrLf & "Confronti : 1"
  75.             Txtric.Text = Txtric.Text & vbCrLf & "Posizione : " & m
  76.             List2.ListIndex = m
  77.             Exit Sub
  78.            
  79.          End If
  80.          
  81.              p = 0
  82.              u = n
  83.              
  84.           While p <= u
  85.            
  86.             m = (p + u) \ 2
  87.              If v(m) < item Then
  88.              
  89.                 p = m + 1
  90.                 confronti = confronti + 1
  91.                
  92.                  If item = v(p) Then
  93.                     Txtric.Text = "Elemento presente nel vettore"
  94.                     Txtric.Text = Txtric.Text & vbCrLf & "Confronti : " & confronti
  95.                     Txtric.Text = Txtric.Text & vbCrLf & "Posizione : " & p
  96.                     confronti = 0
  97.                     List2.ListIndex = p
  98.                     List1.ListIndex = pos
  99.                     Exit Sub
  100.                  End If
  101.            
  102.              Else
  103.              
  104.                  u = m - 1
  105.                  confronti = confronti + 1
  106.                
  107.                  If item = v(u) Then
  108.                     Txtric.Text = "Elemento presente nel vettore"
  109.                     Txtric.Text = Txtric.Text & vbCrLf & "Confronti : " & confronti
  110.                     Txtric.Text = Txtric.Text & vbCrLf & "Posizione : " & u
  111.                     confronti = 0
  112.                     List2.ListIndex = u
  113.                     List1.ListIndex = pos
  114.                     Exit Sub
  115.                  End If
  116.              
  117.              End If
  118.              
  119.            Wend
  120.            Txtric.Text = item & " non è presente nel vettore"
  121.   Else
  122.     MsgBox "Il vettore deve prima essere ordinato", vbInformation, "Ordina il vettore"
  123.  End If
  124.  
  125.  Exit Sub
  126.  
  127. error:    MsgBox Err.Description
  128.  
  129. End Sub
  130.  
  131. Private Sub cmdgen_Click()
  132.  
  133. List1.Clear
  134.  
  135.  For i = 0 To n
  136.  
  137.   f = False
  138.   Randomize
  139.   num = Int(Rnd * 99) + 1
  140.  
  141.    For j = 0 To i
  142.    
  143.       If num = v(j) Then
  144.      
  145.        f = True
  146.        i = i - 1
  147.      
  148.       End If
  149.      
  150.    Next j
  151.    
  152.     If Not f Then
  153.     v(i) = num
  154.     End If
  155.  Next i
  156.  
  157.  For i = 0 To n
  158.   List1.AddItem v(i)
  159.  Next i
  160.  
  161.  For i = 0 To n
  162.   v2(i) = v(i)
  163.  Next i
  164.  
  165. End Sub
  166.  
  167. Private Sub cmdsort_Click()
  168.  
  169. List2.Clear
  170.  
  171.  For i = 0 To n - 1
  172.  
  173.   For j = i To n
  174.    If v(i) > v(j) Then
  175.     temp = v(j)
  176.     v(j) = v(i)
  177.     v(i) = temp
  178.    End If
  179.   Next j
  180.  
  181.  Next i
  182.  
  183.  For i = 0 To n
  184.  
  185.   List2.AddItem v(i)
  186.  
  187.  Next i
  188.  
  189.  flag = True
  190.  
  191. End Sub
  192.  
  193. Private Sub Form_Load()
  194.  flag = False
  195.  f = False
  196. End Sub
  197.  
  198. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  199.  MsgBox "Programma realizzato da Veronese Alberto - albertking82 il (5 novembre 2007)", vbInformation, "Creatore"
  200. End Sub