Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reto #28-go #4081

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/go/KevinED11.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"strconv"
)

func isNumber(character string) bool {
if _, err := strconv.Atoi(character); err != nil {
return false
}
return true
}

func isOperator(character string) bool {
validOperators := []string{"-", "+", "*", "/", "%"}

for _, op := range validOperators {
if op == character {
return true
}
}
return false

}

func validateExpression(expression string, isExpression *bool) {
whiteSpace := " "
for _, c := range expression {
character := string(c)
if isNumber(character) || isOperator(character) || character == whiteSpace {
*isExpression = true
continue
}

*isExpression = false
break
}
}

func main() {
expression1 := "5 + 6 / 7 - 4"
expression2 := "5 a 6"

var isExpression1, isExpression2 bool

validateExpression(expression1, &isExpression1)
validateExpression(expression2, &isExpression2)

fmt.Printf("The expression1 '%s' is valid - %t\n", expression1, isExpression1)
fmt.Printf("The expression2 '%s' is not valid - %t\n", expression2, isExpression2)

}