Skip to content

Commit

Permalink
Merge pull request mouredev#4078 from blackriper/main
Browse files Browse the repository at this point in the history
Reto#28-go
  • Loading branch information
Roswell468 authored Jul 12, 2023
2 parents c8fe58d + 79e181d commit 8118a7b
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/go/blackriper.go
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)
}
}

0 comments on commit 8118a7b

Please sign in to comment.