From becd5cd741dc9d83eaa8a2c72d36b7a54e66e91a Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Fri, 10 May 2024 19:07:58 +0100 Subject: [PATCH] third version with gpt4 help --- main.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 2a66f68..34d31da 100644 --- a/main.go +++ b/main.go @@ -8,16 +8,20 @@ import ( "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 - // First request fmt.Println("Enter two floats separated by a space:") input, _ := reader.ReadString('\n') input = strings.TrimSpace(input) - // Store the first response tokens := strings.Split(input, " ") if len(tokens) != 2 { fmt.Println("Please enter exactly two floats.") @@ -32,32 +36,28 @@ func main() { fmt.Printf("=> %.1f %.1f\n", stored1, stored2) - // Subsequent requests for { fmt.Println("Enter another two floats separated by a space:") input, _ = reader.ReadString('\n') - input = strings.TrimSpace(input) - - if input == "" { + if strings.TrimSpace(input) == "" { break // Exit if no input is given } - tokens := strings.Split(input, " ") + tokens := strings.Split(strings.TrimSpace(input), " ") if len(tokens) != 2 { fmt.Println("Please enter exactly two floats.") continue } - firstNum, err1 := strconv.ParseFloat(tokens[0], 64) - secondNum, err2 := strconv.ParseFloat(tokens[1], 64) + 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 } - // Example operation: Adding current input to previous result - stored1 += firstNum - stored2 += secondNum + // Use the abstracted function for combining floats + stored1, stored2 = combineFloats(stored1, stored2, new1, new2) fmt.Printf("=> %.1f %.1f\n", stored1, stored2) }