third version with gpt4 help

This commit is contained in:
NunoSempere 2024-05-10 19:07:58 +01:00
parent a2648c8b95
commit becd5cd741

24
main.go
View File

@ -8,16 +8,20 @@ import (
"strings" "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() { func main() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
var stored1, stored2 float64 // Variables to store the intermediate results var stored1, stored2 float64 // Variables to store the intermediate results
// First request
fmt.Println("Enter two floats separated by a space:") fmt.Println("Enter two floats separated by a space:")
input, _ := reader.ReadString('\n') input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input) input = strings.TrimSpace(input)
// Store the first response
tokens := strings.Split(input, " ") tokens := strings.Split(input, " ")
if len(tokens) != 2 { if len(tokens) != 2 {
fmt.Println("Please enter exactly two floats.") fmt.Println("Please enter exactly two floats.")
@ -32,32 +36,28 @@ func main() {
fmt.Printf("=> %.1f %.1f\n", stored1, stored2) fmt.Printf("=> %.1f %.1f\n", stored1, stored2)
// Subsequent requests
for { for {
fmt.Println("Enter another two floats separated by a space:") fmt.Println("Enter another two floats separated by a space:")
input, _ = reader.ReadString('\n') input, _ = reader.ReadString('\n')
input = strings.TrimSpace(input) if strings.TrimSpace(input) == "" {
if input == "" {
break // Exit if no input is given break // Exit if no input is given
} }
tokens := strings.Split(input, " ") tokens := strings.Split(strings.TrimSpace(input), " ")
if len(tokens) != 2 { if len(tokens) != 2 {
fmt.Println("Please enter exactly two floats.") fmt.Println("Please enter exactly two floats.")
continue continue
} }
firstNum, err1 := strconv.ParseFloat(tokens[0], 64) new1, err1 := strconv.ParseFloat(tokens[0], 64)
secondNum, err2 := strconv.ParseFloat(tokens[1], 64) new2, err2 := strconv.ParseFloat(tokens[1], 64)
if err1 != nil || err2 != nil { if err1 != nil || err2 != nil {
fmt.Println("Invalid input. Please ensure you enter two floats.") fmt.Println("Invalid input. Please ensure you enter two floats.")
continue continue
} }
// Example operation: Adding current input to previous result // Use the abstracted function for combining floats
stored1 += firstNum stored1, stored2 = combineFloats(stored1, stored2, new1, new2)
stored2 += secondNum
fmt.Printf("=> %.1f %.1f\n", stored1, stored2) fmt.Printf("=> %.1f %.1f\n", stored1, stored2)
} }