fermi/main.go

65 lines
1.5 KiB
Go
Raw Normal View History

2024-05-10 18:05:03 +00:00
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
2024-05-10 18:07:36 +00:00
var stored1, stored2 float64 // Variables to store the intermediate results
2024-05-10 18:05:03 +00:00
// First request
fmt.Println("Enter two floats separated by a space:")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
2024-05-10 18:07:36 +00:00
// Store the first response
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)
2024-05-10 18:05:03 +00:00
// Subsequent requests
for {
fmt.Println("Enter another two floats separated by a space:")
input, _ = reader.ReadString('\n')
input = strings.TrimSpace(input)
if input == "" {
break // Exit if no input is given
}
tokens := strings.Split(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)
if err1 != nil || err2 != nil {
fmt.Println("Invalid input. Please ensure you enter two floats.")
continue
}
2024-05-10 18:07:36 +00:00
// Example operation: Adding current input to previous result
stored1 += firstNum
stored2 += secondNum
2024-05-10 18:05:03 +00:00
2024-05-10 18:07:36 +00:00
fmt.Printf("=> %.1f %.1f\n", stored1, stored2)
2024-05-10 18:05:03 +00:00
}
}