fermi/main.go

65 lines
1.7 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
// combineFloats takes previous stored floats and new floats, applies an operation, and returns the combined results.
func combineFloats(stored1, stored2, new1, new2 float64) (float64, float64) {
// Example operation: simply adding the new floats to the stored ones.
return stored1 + new1, stored2 + new2
}
func main() {
reader := bufio.NewReader(os.Stdin)
var stored1, stored2 float64 // Variables to store the intermediate results
fmt.Println("Enter two floats separated by a space:")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
tokens := strings.Split(input, " ")
if len(tokens) != 2 {
fmt.Println("Please enter exactly two floats.")
return
}
stored1, err1 := strconv.ParseFloat(tokens[0], 64)
stored2, err2 := strconv.ParseFloat(tokens[1], 64)
if err1 != nil || err2 != nil {
fmt.Println("Invalid input. Please ensure you enter two floats.")
return
}
fmt.Printf("=> %.1f %.1f\n", stored1, stored2)
for {
fmt.Println("Enter another two floats separated by a space:")
input, _ = reader.ReadString('\n')
if strings.TrimSpace(input) == "" {
break // Exit if no input is given
}
tokens := strings.Split(strings.TrimSpace(input), " ")
if len(tokens) != 2 {
fmt.Println("Please enter exactly two floats.")
continue
}
new1, err1 := strconv.ParseFloat(tokens[0], 64)
new2, err2 := strconv.ParseFloat(tokens[1], 64)
if err1 != nil || err2 != nil {
fmt.Println("Invalid input. Please ensure you enter two floats.")
continue
}
// Use the abstracted function for combining floats
stored1, stored2 = combineFloats(stored1, stored2, new1, new2)
fmt.Printf("=> %.1f %.1f\n", stored1, stored2)
}
}