tokens => words

This commit is contained in:
NunoSempere 2024-05-11 11:08:24 +01:00
parent 073728e750
commit b1a4f2bd3c

38
f.go
View File

@ -44,14 +44,14 @@ func main() {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
tokens := strings.Split(input, " ")
words := strings.Split(input, " ")
error_msg_start := "Please enter two floats separated by a space, like: 1 10"
if len(tokens) != 2 {
if len(words) != 2 {
fmt.Println(error_msg_start)
return
}
old_low, err1 := strconv.ParseFloat(tokens[0], 64)
old_high, err2 := strconv.ParseFloat(tokens[1], 64)
old_low, err1 := strconv.ParseFloat(words[0], 64)
old_high, err2 := strconv.ParseFloat(words[1], 64)
if err1 != nil || err2 != nil {
fmt.Println(error_msg_start)
return
@ -65,21 +65,21 @@ EventForLoop:
if strings.TrimSpace(input) == "" {
continue EventForLoop
}
tokens := strings.Split(strings.TrimSpace(input), " ")
words := strings.Split(strings.TrimSpace(input), " ")
var new_low, new_high float64
var err1, err2 error
err1, err2 = nil, nil
switch tokens[0] {
switch words[0] {
case "*":
switch len(tokens) {
switch len(words) {
case 1:
fmt.Println("Can't multiply by nothing")
fmt.Println(error_msg_cont)
continue EventForLoop
case 2:
single_float, err1 := strconv.ParseFloat(tokens[1], 64)
single_float, err1 := strconv.ParseFloat(words[1], 64)
if err1 != nil {
fmt.Println("Trying to multiply by a scalar, but scalar is not a float")
fmt.Println(error_msg_cont)
@ -88,8 +88,8 @@ EventForLoop:
new_low = single_float
new_high = single_float
case 3:
new_low, err1 = strconv.ParseFloat(tokens[1], 64)
new_high, err2 = strconv.ParseFloat(tokens[2], 64)
new_low, err1 = strconv.ParseFloat(words[1], 64)
new_high, err2 = strconv.ParseFloat(words[2], 64)
if err1 != nil || err2 != nil {
fmt.Println(error_msg_cont)
fmt.Println("Trying to multiply by a distribution, but distribution is not specified as two floats")
@ -101,13 +101,13 @@ EventForLoop:
continue EventForLoop
}
case "/":
switch len(tokens) {
switch len(words) {
case 1:
fmt.Println("Can't divide by nothing")
fmt.Println(error_msg_cont)
continue EventForLoop
case 2:
single_float, err1 := strconv.ParseFloat(tokens[0], 64)
single_float, err1 := strconv.ParseFloat(words[0], 64)
if err1 != nil {
fmt.Println("Trying to divide by a scalar, but scalar is not a float")
fmt.Println(error_msg_cont)
@ -116,8 +116,8 @@ EventForLoop:
new_low = 1.0 / single_float
new_high = 1.0 / single_float
case 3:
new_low, err1 = strconv.ParseFloat(tokens[1], 64)
new_high, err2 = strconv.ParseFloat(tokens[2], 64)
new_low, err1 = strconv.ParseFloat(words[1], 64)
new_high, err2 = strconv.ParseFloat(words[2], 64)
if err1 != nil || err2 != nil {
fmt.Println("Trying to divide by a distribution, but distribution is not specified as two floats")
fmt.Println(error_msg_cont)
@ -127,11 +127,11 @@ EventForLoop:
fmt.Println("Trying to divide by something, but this something is neither a scalar nor a distribution")
}
default:
switch len(tokens) {
switch len(words) {
case 0:
continue EventForLoop
case 1:
switch tokens[0] {
switch words[0] {
case "i":
fmt.Printf("=> %.1f %.1f\n", old_low, old_high)
logmean_old, logstd_old := boundsToLogParams(old_low, old_high)
@ -140,7 +140,7 @@ EventForLoop:
case "e":
break EventForLoop
default:
single_float, err1 := strconv.ParseFloat(tokens[0], 64)
single_float, err1 := strconv.ParseFloat(words[0], 64)
if err1 != nil {
fmt.Println("Trying to multiply by a scalar, but scalar is not a float")
fmt.Println(error_msg_cont)
@ -150,8 +150,8 @@ EventForLoop:
new_high = single_float
}
case 2:
new_low, err1 = strconv.ParseFloat(tokens[0], 64)
new_high, err2 = strconv.ParseFloat(tokens[1], 64)
new_low, err1 = strconv.ParseFloat(words[0], 64)
new_high, err2 = strconv.ParseFloat(words[1], 64)
if err1 != nil || err2 != nil {
fmt.Println("Trying to multiply by a distribution, but distribution is not specified as two floats")
fmt.Println(error_msg_cont)