Option Strict On
Public Class Form1
Dim lstValorMedAritmetica As New List(Of Double) 'Variable para los valores de la media aritmética
Dim indice As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myFont As New Font("Arial", 10, FontStyle.Bold)
RadioButton1.Checked = True
RadioButton1.Text = "Media Aritmética"
RadioButton2.Text = "Media Armónica"
RadioButton3.Text = "Media Geométrica"
RadioButton4.Text = "Moda"
RadioButton5.Text = "Mediana "
RadioButton1.Font = myFont
RadioButton2.Font = myFont
RadioButton3.Font = myFont
RadioButton4.Font = myFont
RadioButton5.Font = myFont
Me.Text = "Medidas de centralizaciónl"
Me.MaximizeBox = False
btAgregar.Text = "<< Agregar"
btCalcMed.Text = "Calcular"
txtValue.TextAlign = HorizontalAlignment.Right
End Sub
Private Sub btAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAgregar.Click
Try
'Si no hay nada en la lista borra el listbox y el resultado anterior
If lstValorMedAritmetica.Count = 0 Then
ListBox1.Items.Clear()
txtResultMedArit.Text = ""
End If
'Añade un valor a la lista
lstValorMedAritmetica.Add(CDbl(txtValue.Text))
'Añade un valor a listbox
ListBox1.Items.Add(txtValue.Text)
btCalcMed.Enabled = True
Catch ex As Exception
MessageBox.Show("Valor no válido", _
"Atención", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub btCalcMed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCalcMed.Click
'Calcula la media aritmética
If RadioButton1.Checked = True Then
txtResultMedArit.Text =
CStr(modEstadisticFunctions.MediaAritmetica(lstValorMedAritmetica).ToString("0.00"))
End If
'Calcula la media armónica
If RadioButton2.Checked = True Then
txtResultMedArit.Text =
CStr(modEstadisticFunctions.MediaArmonica(lstValorMedAritmetica).ToString("0.00"))
End If
'Calcula Media Geométrica
If RadioButton3.Checked = True Then
txtResultMedArit.Text =
CStr(modEstadisticFunctions.MediaGeometrica(lstValorMedAritmetica).ToString("0.00"))
End If
'Calcula moda
If RadioButton4.Checked = True Then
txtResultMedArit.Text = CStr(
modEstadisticFunctions.Moda(lstValorMedAritmetica))
End If
'Calcula mediana
If RadioButton5.Checked = True Then
txtResultMedArit.Text =
modEstadisticFunctions.Mediana(lstValorMedAritmetica)
End If
lstValorMedAritmetica.Clear()
btCalcMed.Enabled = False
End Sub
Private Sub txtArtimetica_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtValue.Click
txtValue.SelectAll()
End Sub
End Class
Module modEstadisticFunctions
''' <summary>
''' Función para calcular la media aritmética
''' </summary>
''' <param name="dblValue">Variable array List</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function MediaAritmetica(ByVal dblValue As List(Of Double)) As Double
Try
Dim Valor As Double
Dim md As Double
For I As Integer = 0 To dblValue.Count - 1
Valor += +dblValue(I)
Next
md = Valor / dblValue.Count
Return md
Catch ex As Exception
MessageBox.Show("Valor no válido", _
"Atención", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Return Nothing
End Try
End Function
''' <summary>
''' Función para calcular la media armónica
''' </summary>
''' <param name="dblValue">Variable array List</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function MediaArmonica(ByVal dblValue As List(Of Double)) As Double
Try
Dim Valor As Double
Dim md As Double
For I As Integer = 0 To dblValue.Count - 1
Valor += +(1 / dblValue(I))
Next
md = dblValue.Count / Valor
Return md
Catch ex As Exception
MessageBox.Show("Valor no válido", _
"Atención", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Return Nothing
End Try
End Function
''' <summary>
''' Función para calcular la media geométrica
''' </summary>
''' <param name="dblValue">Variable array List</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function MediaGeometrica(ByVal dblValue As List(Of Double)) As Double
Try
Dim Valor As Double = 1
Dim md As Double
For I As Integer = 0 To dblValue.Count - 1
Valor = Valor * dblValue(I)
Next
md = Valor ^ (1 / dblValue.Count)
Return md
Catch ex As Exception
MessageBox.Show("Valor no válido", _
"Atención", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Return Nothing
End Try
End Function
''' <summary>
''' Función para calcular la Moda
''' </summary>
''' <param name="dblValue">Variable array List</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Moda(ByVal dblValue As List(Of Double)) As Double
Try
Return dblValue.Max() 'Obtiene el número mayor
Catch ex As Exception
MessageBox.Show("Valor no válido", _
"Atención", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Return Nothing
End Try
End Function
''' <summary>
''' Función para calcular la Mediana
''' </summary>
''' <param name="dblValue">Variable array List</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Mediana(ByVal dblValue As List(Of Double)) As String
Try
Dim par As Double
Dim Madn As String = Nothing
dblValue.Sort()
If (dblValue.Count / 2) = CInt(dblValue.Count / 2) Then
par = CDbl(True)
Else
par = CDbl(False)
End If
Select Case par
Case CDbl(True) : Madn = String.Format("{0}-{1}",
CStr(dblValue(CInt((dblValue.Count / 2) - 1))),
CStr(dblValue(CInt(dblValue.Count / 2))))
Case CDbl(False) : Madn = String.Format("{0}",
CStr(dblValue(CInt(Conversion.Int(dblValue.Count / 2)))))
End Select
Return Madn
Catch ex As Exception
MessageBox.Show("Valor no válido", _
"Atención", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Return Nothing
End Try
End Function
End Module