Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. Public Class Form1
  2.  
  3. Dim mat(0, 0) As Integer 'Variável Global
  4.  
  5. Private Sub Btn_maior_Click(sender As Object, e As EventArgs) Handles btn_maior.Click
  6.  
  7. Dim maior As Integer = mat(0, 0)
  8. Dim coord_linhas, coord_colunas As Integer
  9.  
  10. For i As Integer = 0 To Convert.ToInt32(txt_linhas.Text) - 1
  11. For j As Integer = 0 To Convert.ToInt32(txt_colunas.Text) - 1
  12.  
  13. If mat(i, j) > maior Then 'Se o valor da matriz mat(i,j) for maior que o 'maior' então guarda as coordenadas e o valor
  14. maior = mat(i, j)
  15. coord_linhas = i
  16. coord_colunas = j
  17. End If
  18.  
  19. Next
  20. Next
  21.  
  22. MessageBox.Show("O maior valor é " & maior & vbNewLine & "As suas coordenadas são: " & coord_linhas + 1 & "," & coord_colunas + 1, "Maior Valor", MessageBoxButtons.OK, MessageBoxIcon.Information)
  23.  
  24. End Sub
  25.  
  26. Private Sub Btn_ler_Click(sender As Object, e As EventArgs) Handles btn_ler.Click
  27.  
  28. ReDim mat(txt_linhas.Text, txt_colunas.Text)
  29.  
  30. For i As Integer = 0 To Convert.ToInt32(txt_linhas.Text) - 1
  31. For j As Integer = 0 To Convert.ToInt32(txt_colunas.Text) - 1
  32.  
  33. Try 'Vai tentar fazer isto
  34.  
  35. mat(i, j) = InputBox(("Introduza o valor da posição " & i + 1 & "," & j + 1), "Leitura")
  36.  
  37. Catch ex As Exception 'Se **não** for bem-sucedido, vai para aqui
  38.  
  39. MessageBox.Show("Tem de introduzir um valor numérico", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error) 'Mensagem de Erro
  40. Return 'E retorna
  41.  
  42. End Try
  43. Next
  44. Next
  45.  
  46. btn_maior.Enabled = True
  47. btn_menor.Enabled = True
  48. btn_somaprincipal.Enabled = True
  49. btn_somasecundaria.Enabled = True
  50. btn_ver.Enabled = True
  51. btn_dsecundaria.Enabled = True
  52. btn_dprincipal.Enabled = True
  53.  
  54. End Sub
  55.  
  56. Private Sub Btnmenor_Click(sender As Object, e As EventArgs) Handles btn_menor.Click
  57.  
  58. Dim menor As Integer = mat(0, 0)
  59. Dim coord_linhas, coord_colunas As Integer
  60.  
  61. For i As Integer = 0 To Convert.ToInt32(txt_linhas.Text) - 1
  62. For j As Integer = 0 To Convert.ToInt32(txt_colunas.Text) - 1
  63.  
  64. If mat(i, j) < menor Then 'Se o valor da matriz mat(i,j) for menor q o 'menor' então guarda as coordenadas e o valor
  65. menor = mat(i, j)
  66. coord_linhas = i
  67. coord_colunas = j
  68. End If
  69.  
  70. Next
  71. Next
  72.  
  73. MessageBox.Show("O menor valor é " & menor & vbNewLine & "As suas coordenadas são: " & coord_linhas + 1 & "," & coord_colunas + 1, "Menor Valor", MessageBoxButtons.OK, MessageBoxIcon.Information)
  74.  
  75. End Sub
  76.  
  77. Private Sub Txtlinhas_TextChanged(sender As Object, e As EventArgs) Handles txt_linhas.TextChanged
  78.  
  79. If IsNumeric(txt_linhas.Text) Then
  80. txt_colunas.Text = Convert.ToInt32(txt_linhas.Text)
  81. btn_ler.Enabled = True
  82. Else
  83. btn_maior.Enabled = False
  84. btn_menor.Enabled = False
  85. btn_somaprincipal.Enabled = False
  86. btn_somasecundaria.Enabled = False
  87. btn_ver.Enabled = False
  88. btn_dsecundaria.Enabled = False
  89. btn_dprincipal.Enabled = False
  90. btn_ler.Enabled = False
  91. txt_colunas.Clear()
  92. txt_linhas.Clear()
  93. End If
  94.  
  95. End Sub
  96.  
  97. Private Sub txt_colunas_TextChanged(sender As Object, e As EventArgs) Handles txt_colunas.TextChanged
  98.  
  99. If IsNumeric(txt_colunas.Text) Then
  100. txt_colunas.Text = Convert.ToInt32(txt_colunas.Text)
  101. btn_ler.Enabled = True
  102. Else
  103. btn_maior.Enabled = False
  104. btn_menor.Enabled = False
  105. btn_somaprincipal.Enabled = False
  106. btn_somasecundaria.Enabled = False
  107. btn_ver.Enabled = False
  108. btn_dsecundaria.Enabled = False
  109. btn_dprincipal.Enabled = False
  110. btn_ler.Enabled = False
  111. txt_linhas.Clear()
  112. txt_colunas.Clear()
  113. End If
  114.  
  115. End Sub
  116.  
  117. Private Sub btn_ver_Click(sender As Object, e As EventArgs) Handles btn_ver.Click
  118.  
  119. txt_display.Clear()
  120. For i As Integer = 0 To Convert.ToInt32(txt_linhas.Text) - 1
  121. For j As Integer = 0 To Convert.ToInt32(txt_colunas.Text) - 1
  122. txt_display.Text += mat(i, j) & ", "
  123. Next
  124. Next
  125.  
  126. End Sub
  127.  
  128. Private Sub btn_dprincipal_Click(sender As Object, e As EventArgs) Handles btn_dprincipal.Click
  129.  
  130. txt_display.Clear()
  131. For i As Integer = 0 To Convert.ToInt32(txt_linhas.Text) - 1
  132. txt_display.Text += mat(i, i) & ", "
  133. Next
  134.  
  135. End Sub
  136.  
  137. Private Sub btn_dsecundaria_Click(sender As Object, e As EventArgs) Handles btn_dsecundaria.Click
  138.  
  139. Dim aux As Integer = Convert.ToInt32(txt_colunas.Text) - 1
  140.  
  141. txt_display.Clear()
  142. For i As Integer = 0 To Convert.ToInt32(txt_linhas.Text) - 1
  143.  
  144. txt_display.Text += mat(i, aux) & ", "
  145. aux -= 1
  146.  
  147. Next
  148.  
  149. End Sub
  150.  
  151. Private Sub btn_somaprincipal_Click(sender As Object, e As EventArgs) Handles btn_somaprincipal.Click
  152.  
  153. Dim soma As Integer = 0
  154.  
  155. txt_display.Clear()
  156. For i As Integer = 0 To Convert.ToInt32(txt_linhas.Text) - 1
  157. soma += mat(i + 1, i + 1)
  158. Next
  159.  
  160. txt_display.Text = "Soma dos elementos acima" & vbNewLine & "da Diagonal Principal = " & soma
  161.  
  162. End Sub
  163. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement