Guest User

Untitled

a guest
Jul 19th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Dim A(1 To 100, 1 To 100) As Double
  4. Dim Nrighe As Integer
  5. Dim Ncolonne As Integer
  6. Dim i As Integer
  7. Dim j As Integer
  8. Dim k As Integer
  9.  
  10. Private Sub Command1_Click()
  11.     Nrighe = txtNrighe.Text
  12.     Ncolonne = txtNcolonne.Text
  13.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf
  14.     For i = 1 To Nrighe
  15.         For j = 1 To Ncolonne
  16.             A(i, j) = InputBox("inserisci il valore a(" & i & "," & j & " ) ")
  17.             TxtRisposta.Text = TxtRisposta.Text & "  " & A(i, j)
  18.         Next j
  19.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf
  20.     TxtRisposta.SelStart = Len(TxtRisposta.Text)
  21.     Next i
  22. End Sub
  23.  
  24. Private Sub Command2_Click()
  25. Dim Trasposta(1 To 100, 1 To 100) As Double
  26. Dim M As Double
  27. Dim N As Double
  28.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf
  29.     M = Nrighe
  30.     N = Ncolonne
  31.     For i = 1 To N
  32.         For j = 1 To M
  33.             Trasposta(i, j) = A(j, i)
  34.             TxtRisposta.Text = TxtRisposta.Text & "  " & Trasposta(i, j)
  35.         Next j
  36.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf
  37.     TxtRisposta.SelStart = Len(TxtRisposta.Text)
  38.     Next i
  39. End Sub
  40.  
  41. Private Sub Command3_Click()
  42.     If Nrighe <> Ncolonne Then
  43.         MsgBox ("Per calcolare il determinante la matrice deve essere quadrata"), vbInformation
  44.         Exit Sub
  45.     End If
  46.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf & "Determinante=" & Determinante(A, Nrighe) & vbCrLf
  47.     TxtRisposta.SelStart = Len(TxtRisposta.Text)
  48. End Sub
  49.  
  50. Private Sub Command4_Click()
  51. Dim N As Integer
  52. Dim M As Integer
  53. Dim i1 As Integer
  54. Dim i2 As Integer
  55. Dim i3 As Integer
  56. Dim j1 As Integer
  57. Dim j2 As Integer
  58. Dim j3 As Integer
  59. Dim det As Double
  60. Dim Inversa(1 To 100, 1 To 100) As Double
  61. Dim B(1 To 100, 1 To 100) As Double
  62. Dim C(1 To 100, 1 To 100) As Double
  63.     If Nrighe <> Ncolonne Then
  64.         MsgBox "Per calcolare l'inversa la matrice dev'essere quadrata.", vbInformation
  65.         Exit Sub
  66.     End If
  67.     det = Determinante(A, Nrighe)
  68.     If det = 0 Then
  69.         MsgBox "La matrice ha determinante nullo.", vbExclamation
  70.     Else
  71.         For i1 = 1 To Nrighe
  72.             For j1 = 1 To Ncolonne
  73.             i3 = 0
  74.                 For i2 = 1 To Nrighe
  75.                     If i2 <> i1 Then
  76.                         i3 = i3 + 1
  77.                         j3 = 0
  78.                         For j2 = 1 To Ncolonne
  79.                             If j2 <> j1 Then
  80.                                 j3 = j3 + 1
  81.                                 B(i3, j3) = A(i2, j2)
  82.                             End If
  83.                         Next j2
  84.                     End If
  85.                 Next i2
  86.                 If (i1 + j1) Mod 2 = 0 Then
  87.                     C(i1, j1) = Determinante(B, Nrighe - 1)
  88.                 Else
  89.                     C(i1, j1) = -Determinante(B, Nrighe - 1)
  90.                 End If
  91.             Next j1
  92.         Next i1
  93.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf
  94.     For i = 1 To Nrighe
  95.         For j = 1 To Ncolonne
  96.             Inversa(i, j) = C(j, i) / det
  97.             TxtRisposta.Text = TxtRisposta.Text & "  " & Inversa(i, j)
  98.         Next j
  99.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf
  100.     TxtRisposta.SelStart = Len(TxtRisposta.Text)
  101.     Next i
  102.     End If
  103. End Sub
  104.  
  105. Private Sub Command5_Click()
  106. Dim E(1 To 100, 1 To 100) As Double
  107. Dim Controllo As Integer
  108. Dim Rango As Integer
  109. Dim P As Double
  110. Dim O As Double
  111. Dim N As Integer
  112. Dim M As Integer
  113. Dim i1 As Integer
  114. Dim j1 As Integer
  115. Dim i2 As Integer
  116.     N = Nrighe
  117.     M = Ncolonne
  118.     For i = 1 To N
  119.         For j = 1 To M
  120.             E(i, j) = A(i, j)
  121.         Next j
  122.     Next i
  123.     j = 0
  124.     For i = 1 To N
  125.         j = j + 1
  126.         If E(i, j) = 0 Then
  127.             For i1 = i + 1 To N
  128.                 If E(i1, j) <> 0 Then
  129.                     For j1 = 1 To M
  130.                         E(i, j1) = E(i, j1) + E(i1, j1)
  131.                         E(i1, j1) = E(i, j1) - E(i1, j1)
  132.                         E(i, j1) = E(i, j1) - E(i1, j1)
  133.                     Next j1
  134.                     i1 = N
  135.                 End If
  136.             Next i1
  137.         End If
  138.         For i1 = i + 1 To N
  139.             If E(i1, j) <> 0 Then
  140.                 P = E(i1, j)
  141.                 O = E(i, j)
  142.                 For j1 = j To M
  143.                     E(i, j1) = -E(i, j1) * P / O
  144.                     E(i1, j1) = E(i1, j1) + E(i, j1)
  145.                 Next j1
  146.             End If
  147.         Next i1
  148.     Next i
  149.     For i = 1 To N - 1
  150.         j = 0
  151.         Do
  152.             j = j + 1
  153.             Controllo = 1
  154.             If E(i + 1, j) = 0 Then
  155.                 If E(i, j) <> 0 Then
  156.                     Controllo = 0
  157.                 End If
  158.                 Else
  159.                     If E(i, j) / E(i + 1, j) <> E(i, j + 1) / E(i + 1, j + 1) Then
  160.                         Controllo = 0
  161.                     End If
  162.             End If
  163.         Loop Until Controllo = 0 Or j = M - 1
  164.         If Controllo = 1 And j = M - 1 Then
  165.             For i1 = i + 1 To N
  166.                 For j = 1 To M
  167.                     E(i1, j) = 0
  168.                 Next j
  169.                 i = N - 1
  170.             Next i1
  171.         End If
  172.     Next i
  173.     Rango = N
  174.     For i = N To 1 Step -1
  175.         j = 0
  176.         Controllo = 1
  177.         Do
  178.             j = j + 1
  179.             If E(i, j) <> 0 Then
  180.                 Controllo = 0
  181.             End If
  182.         Loop Until Controllo = 0 Or j = M
  183.         If Controllo = 1 Then
  184.             Rango = Rango - 1
  185.         End If
  186.     Next i
  187.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf
  188.     TxtRisposta.Text = TxtRisposta.Text & vbCrLf & "Rango=" & Rango & vbCrLf
  189.     TxtRisposta.SelStart = Len(TxtRisposta.Text)
  190. End Sub
  191.  
  192. Private Sub Command6_Click()
  193.     Unload Me
  194. End Sub
  195.  
  196. Public Function Determinante(Matrice() As Double, N As Integer) As Double
  197. Dim D(1 To 100, 1 To 100) As Double
  198. Dim S As Double
  199.     For i = 1 To N
  200.         For j = 1 To N
  201.             D(i, j) = Matrice(i, j)
  202.         Next j
  203.     Next i
  204.     If N = 1 And D(1, 1) = 0 Then
  205.         Determinante = 0
  206.         Exit Function
  207.     End If
  208.     For i = 1 To N
  209.         If D(i, i) = 0 Then
  210.             k = i
  211.             Do
  212.                 If i = N Then
  213.                     k = k - 1
  214.                 Else
  215.                     k = k + 1
  216.                 End If
  217.                 For j = 1 To N
  218.                     D(i, j) = D(i, j) + Matrice(k, j)
  219.                 Next j
  220.             Loop Until D(i, i) <> 0
  221.         End If
  222.     Next i
  223.     Determinante = 1
  224.     For i = 1 To N - 1
  225.         For j = i + 1 To N
  226.             S = -D(j, i) / D(i, i)
  227.             For k = 1 To N
  228.                 D(j, k) = D(j, k) + D(i, k) * S
  229.             Next k
  230.         Next j
  231.     Next i
  232.     For i = 1 To N
  233.         Determinante = Determinante * D(i, i)
  234.     Next i
  235. End Function
Add Comment
Please, Sign In to add comment