forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79eb053
commit 3534604
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/vbscript/arielposada.vbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
' Ejemplo de uso: | ||
' cscript arielposada.vbs | ||
' Crea una función que reciba una expresión matemática (String) | ||
' y compruebe si es correcta. Retornará true o false. | ||
' - Para que una expresión matemática sea correcta debe poseer | ||
' un número, una operación y otro número separados por espacios. | ||
' Tantos números y operaciones como queramos. | ||
' - Números positivos, negativos, enteros o decimales. | ||
' - Operaciones soportadas: + - * / % | ||
' Ejemplos: | ||
' "5 + 6 / 7 - 4" -> true | ||
' "5 a 6" -> false | ||
Option Explicit | ||
|
||
Function IsMathExpressionValid(expression) | ||
Dim regexPattern | ||
Dim objRegEx | ||
Dim objMatches | ||
|
||
regexPattern = "^(\s*-?\d+(\.\d+)?\s*[\+\-\*\/\%]\s*)*-?\d+(\.\d+)?\s*$" | ||
|
||
Set objRegEx = New RegExp | ||
objRegEx.Pattern = regexPattern | ||
objRegEx.Global = True | ||
|
||
Set objMatches = objRegEx.Execute(expression) | ||
|
||
If objMatches.Count >= 1 Then | ||
IsMathExpressionValid = True | ||
Else | ||
IsMathExpressionValid = False | ||
End If | ||
|
||
Set objMatches = Nothing | ||
Set objRegEx = Nothing | ||
End Function | ||
|
||
Dim testExpression1, testExpression2 | ||
testExpression1 = "5 + 6 / 7 - 4" | ||
testExpression2 = "5 a 6" | ||
|
||
WScript.Echo testExpression1 & " -> " & IsMathExpressionValid(testExpression1) | ||
WScript.Echo testExpression2 & " -> " & IsMathExpressionValid(testExpression2) | ||
|
||
|