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.
Merge pull request mouredev#4078 from blackriper/main
Reto#28-go
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/go/blackriper.go
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,51 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// funcion para comprobar si es un numero | ||
func IsNumber(n string) bool { | ||
_, err := strconv.Atoi(n) | ||
if err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// funcion para validar si tiene algun operador | ||
func IsOperator(n string) bool { | ||
validOperators := []string{"+", "-", "*", "/", "%"} | ||
for _, exp := range validOperators { | ||
if n == exp { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// funcion para validar la expresion | ||
func ValidExpression(express string, isexpr *bool) { | ||
for _, cr := range express { | ||
if IsNumber(string(cr)) || IsOperator(string(cr)) || string(cr) == " " { | ||
*isexpr = true | ||
} else { | ||
*isexpr = false | ||
break | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
express := "5 + 6 / 7 * 8" | ||
var isexpr bool | ||
|
||
ValidExpression(express, &isexpr) | ||
|
||
if isexpr { | ||
fmt.Printf("the expression %v is a math expression \n", express) | ||
} else { | ||
fmt.Printf("the expression %v is not math expression \n", express) | ||
} | ||
} |