En la clase EvaluarFunciones
Protected Shared funciones() As String = { _
"Max(", "Min(", "Round(", "Log(", "Log10(", _
"Sqr(", "Sqrt(", "Mod(", "Pow(", _
"Sum(", "Subst(", "Mult(", "Div(", "DivInt(", _
"Percent(", "Porcentaje(", _
"Suma(", "Resta(", "Multiplica(", "Divide(", _
"Fix(", "Int(", "Floor(", "Ceiling(", "Rnd(", _
"Abs(", "Pi(", "E(", _
"Atn(", "Atan(", "Cos(", "Sin(", "Tan(", _
"Sec(", "Cotan(", "Cosec(", _
"Fact(", "Factorial(", _
"BinToDec(", "OctToDec(", "DecToOct(", _
"Exp(", "Sign(", "Sgn(", _
"Media(", "DesviacionTipica(", "RMS(", _
"mcd(", "gcd(", "mcm(", "lcm(", _
"factorMax(", "factorMin(", _
"esPrimo(", "Primos(", "Factores("}
Public Overloads Function Evalua(ByVal operador As String, _
ByVal ParamArray numeros() As Double) As String
If operador.EndsWith("[") OrElse operador.EndsWith("(") Then
operador = Left(operador, Len(operador) - 1)
End If
Select Case operador.ToLower
Case "+", "sum", "suma"
Return Suma(numeros).ToString
Case "-", "_", "subst", "resta"
Return Resta(numeros).ToString
Case "*", "mult", "multiplica", "x"
Return Multiplica(numeros).ToString
Case "/", "div", "divide" ', ":", "÷"
Return Divide(numeros).ToString
Case "\", "divint"
Return DivideEnteros(numeros).ToString
Case "^", "pow"
Return Math.Pow(numeros(0), numeros(1)).ToString
Case "sqrt", "sqr"
Return Math.Sqrt(numeros(0)).ToString
Case "log10"
Return Math.Log10(numeros(0)).ToString
Case "log"
If numeros.Length > 1 Then
Return Math.Log(numeros(0), numeros(1)).ToString
Else
Return Math.Log(numeros(0)).ToString
End If
Case "max"
Return Max(numeros).ToString
Case "min"
Return Min(numeros).ToString
Case "mod", "\u412?", "\u623?", "m"
Return (numeros(0) Mod numeros(1)).ToString
Case "%", "percent", "porcentaje"
Return Porcentaje(numeros(0), numeros(1)).ToString
Case "round"
If numeros.Length > 1 Then
'Dim d As Double
'd = Math.Round(numeros(0), CInt(numeros(1)))
Return Math.Round(numeros(0), CInt(numeros(1))).ToString
Else
Return Math.Round(numeros(0)).ToString
End If
Case "abs"
Return Math.Abs(numeros(0)).ToString
Case "atn", "atan"
Return Math.Atan(numeros(0)).ToString
Case "cos"
Return Math.Cos(numeros(0)).ToString
Case "sec"
Return (1 / Math.Cos(numeros(0))).ToString
Case "sin"
Return Math.Sin(numeros(0)).ToString
Case "cosec"
Return (1 / Math.Sin(numeros(0))).ToString
Case "tan"
Return Math.Tan(numeros(0)).ToString
Case "cotan"
Return (1 / Math.Tan(numeros(0))).ToString
Case "pi"
Return Math.PI.ToString
Case "e"
Return Math.E.ToString
' Nuevas instrucciones exp, sgn, sign v.30 (12/Oct/07)
Case "exp"
Return Math.Exp(numeros(0)).ToString
Case "sign", "sgn"
Return Math.Sign(numeros(0)).ToString
Case "floor"
Return Math.Floor(numeros(0)).ToString
Case "int"
Return Int(numeros(0)).ToString
Case "fix"
Return Fix(numeros(0)).ToString
Case "ceiling"
Return Math.Ceiling(numeros(0)).ToString
Case "fact", "factorial"
Return Factorial(numeros(0)).ToString
Case "rnd"
If numeros.Length > 1 Then
Return m_Rnd.Next(CInt(numeros(0)), CInt(numeros(1) + 1)).ToString
ElseIf numeros.Length = 1 Then
Return (m_Rnd.NextDouble * numeros(0)).ToString
Else
Return m_Rnd.NextDouble.ToString
End If
' Funciones de conversión de tipos v.16 (12/Oct/07)
' No se puede usar Hex ni devolver Bin porque el valor devuelto
' es Double...
Case "bintodec"
Return BinToDec(numeros(0).ToString).ToString
Case "octtodec"
'Return CDbl("&O" & numeros(0).ToString)
Return OctToDec(numeros(0))
Case "dectooct"
Return DecToOct(numeros(0))
' Nuevas funciones v.0.40 (17/Oct/07)
Case "media"
Return Media(numeros).ToString
Case "rms", "desviaciontipica"
Return DesviacionTipica(numeros).ToString
Case "mcd", "gcd"
Return mcd(numeros(0), numeros(1)).ToString
Case "mcm", "lcm"
Return mcm(numeros(0), numeros(1)).ToString
Case "factormax"
Return FactorMayor(numeros(0)).ToString
Case "factormin"
Return FactorMenor(numeros(0)).ToString
Case "esprimo"
Return EsPrimo(numeros(0)).ToString
Case "primos"
If numeros.Length > 1 Then
Return Primos(numeros(0), numeros(1))
Else
Return Primos(numeros(0))
End If
' factores v.0.42 (17/Oct/07)
Case "factores"
Return Factores(numeros(0))
' Si no se reconoce la función, devolver la suma
Case Else
Return Suma(numeros).ToString
End Select
End Function
''' <summary>
''' Media aritmética o promedio.
''' Se usa con la función Media.
''' </summary>
''' <param name="parametros"></param>
''' <returns></returns>
''' <remarks>
''' Código de Joaquín Delgado
''' </remarks>
Protected Function Media(ByVal ParamArray parametros() As Double) As Double
Dim t As Double = parametros(0)
For i As Integer = 1 To parametros.Length - 1
t = t + parametros(i)
Next
Return (t / parametros.Length)
End Function
''' <summary>
''' Desviación estándar (o desviación típica)
''' Se usa con la función RMS.
''' </summary>
''' <param name="parametros"></param>
''' <returns></returns>
''' <remarks>
''' Código de Joaquín Delgado
''' </remarks>
Protected Function DesviacionTipica(ByVal ParamArray parametros() As Double) As Double
Dim m As Double = Media(parametros)
Dim t As Double = 0
For i As Integer = 0 To parametros.Length - 1
t = t + (parametros(i) - m) ^ 2
Next
Return System.Math.Sqrt(t / parametros.Length)
End Function
''' <summary>
''' Máximo Común Divisor (mcd) / Greatest Common Divisor (gcd)
''' Devuelve el número mayor que divide exactamente a los dos valores indicados
''' (se tratarán como números enteros)
''' </summary>
''' <param name="a"></param>
''' <param name="b"></param>
''' <returns></returns>
''' <remarks>
''' Usando el algoritmo de Euclides
''' </remarks>
Protected Function mcd(ByVal a As Double, ByVal b As Double) As Double
Return mcd(CLng(a), CLng(b))
End Function
Protected Function mcd(ByVal a As Long, ByVal b As Long) As Long
While a > 0
Dim c As Long = a
a = b Mod a
b = c
End While
Return b
End Function
''' <summary>
''' Mínimo Común Múltiplo (mcm) / Least Common Multiple (lcm)
''' Es el producto de dos números dividido por el mcd
''' </summary>
''' <param name="a"></param>
''' <param name="b"></param>
''' <returns></returns>
''' <remarks>
'''
''' </remarks>
Protected Function mcm(ByVal a As Double, ByVal b As Double) As Double
Dim a1 As Long = CLng(a)
Dim b1 As Long = CLng(b)
Return mcm(a1, b1)
End Function
Protected Function mcm(ByVal a As Long, ByVal b As Long) As Long
Dim c As Long = mcd(a, b)
Dim t As Long = a * b
Return t \ c
End Function
''' <summary>
''' El factor mayor de un número (factorMax)
''' El número mayor que divide exactamente al número
''' Si no lo hay ninguno, devuelve cero (normalmente será primo)
''' </summary>
''' <param name="a"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Function FactorMayor(ByVal a As Long) As Long
Dim i As Long = CLng(Math.Sqrt(a)) + 1
While i > 2
If a Mod i = 0 Then
Return i
End If
i = i - 1
End While
Return 0
End Function
Protected Function FactorMayor(ByVal a As Double) As Double
Return FactorMayor(CLng(a))
End Function
''' <summary>
''' Devuelve el factor menor de un número
''' El número menor que lo divide exactamente
''' Si no hay, devuelve cero (normalmente será número primo)
''' </summary>
''' <param name="a"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Function FactorMenor(ByVal a As Long) As Long
' Si es divisible por dos
If a Mod 2 = 0 Then
Return 2
End If
' Comprobar desde el 3 hasta la raíz cuadrada
' seguir comprobando de dos en dos
Dim m As Long = CLng(Math.Sqrt(a)) + 1
Dim i As Integer = 3
While i < m
If a Mod i = 0 Then
Return i
End If
i = i + 2
End While
Return 0
End Function
Protected Function FactorMenor(ByVal a As Double) As Double
Return FactorMenor(CLng(a))
End Function
''' <summary>
''' Comprueba si el número es primo
''' Devuelve 0 si no lo es, 1 si es primo
''' El número 1 se considera como primo
''' </summary>
''' <param name="a"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Function EsPrimo(ByVal a As Long) As Long
If a = 1 OrElse a = 2 OrElse a = 3 Then
Return 1
End If
' Si es par, no es primo
If a Mod 2 = 0 Then
Return 0
End If
' Empezar a comprobar desde el 3
' hasta la raíz cuadrada del número
Dim m As Long = CLng(Math.Sqrt(a)) + 1
Dim i As Integer = 3
While i < m
If a Mod i = 0 Then
Return 0
End If
i = i + 2
End While
Return 1
End Function
Protected Function EsPrimo(ByVal a As Double) As Double
Return EsPrimo(CLng(a))
End Function
''' <summary>
''' Devuelve los números primos entre los dos valores indicados.
''' Los devuelve separados por comas
''' </summary>
''' <param name="desde"></param>
''' <param name="hasta"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Function Primos(ByVal desde As Long, ByVal hasta As Long) As String
Dim m As Long = hasta
If desde > m Then desde = m - 1
Dim sb As New StringBuilder
For i As Long = desde To m
If EsPrimo(i) = 1 Then
sb.AppendFormat("{0}, ", i)
End If
Next
Return sb.ToString().TrimEnd(", ".ToCharArray)
End Function
Protected Function Primos(ByVal desde As Double, ByVal hasta As Double) As String
Return Primos(CLng(desde), CLng(hasta))
End Function
''' <summary>
''' Devuelve los números primos desde 2 hasta el indicado
''' </summary>
''' <param name="hasta"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Function Primos(ByVal hasta As Long) As String
Return Primos(2, hasta)
End Function
Protected Function Primos(ByVal hasta As Double) As String
Return Primos(2, CLng(hasta))
End Function
''' <summary>
''' Devuelve todos los factores del número indicado
''' </summary>
''' <param name="n"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Function Factores(ByVal n As Long) As String
Dim sb As New StringBuilder
Dim m As Long = n \ 2
For i As Long = 2 To m
If n Mod i = 0 Then
sb.AppendFormat("{0}, ", i)
End If
Next
Return sb.ToString().TrimEnd(", ".ToCharArray)
End Function
Protected Function Factores(ByVal n As Double) As String
Return Factores(CLng(n))
End Function