commit becd5cd741dc9d83eaa8a2c72d36b7a54e66e91a
parent a2648c8b95f1f7f12d9eeb65b40c77e1220392b0
Author: NunoSempere <nuno.semperelh@protonmail.com>
Date: Fri, 10 May 2024 19:07:58 +0100
third version with gpt4 help
Diffstat:
| M | main.go | | | 24 | ++++++++++++------------ |
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git 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)
}