From 90244f2511c03e250895bf95c7767fa6400ccfb2 Mon Sep 17 00:00:00 2001 From: Oliver Stolle 3024383 <3024383@stud.hs-mannheim.de> Date: Sat, 28 Mar 2026 14:24:35 +0000 Subject: [PATCH] implemented the calculator exercise and moved "go.mod" --- go/exercises/calculator.go | 64 ++++++++++++++++++++++++++++++++++ go/{00-hello-world => }/go.mod | 0 2 files changed, 64 insertions(+) create mode 100644 go/exercises/calculator.go rename go/{00-hello-world => }/go.mod (100%) diff --git a/go/exercises/calculator.go b/go/exercises/calculator.go new file mode 100644 index 0000000..e29a403 --- /dev/null +++ b/go/exercises/calculator.go @@ -0,0 +1,64 @@ +package main + +import "fmt" + +func main() { + + const add string = "add" + const sub string = "subtract" + const mul string = "multiply" + const div string = "divide" + const eql string = "equal" + + var result float64 = 0 + var temp_num_input float64 + var op_input string + + fmt.Println("Bite geben Sie eine Zahl ein:") + fmt.Scanf("%f", &result) + +main_loop: + for { + + fmt.Println("Bitte geben sie nun den entsprechenden Operator ein:") + fmt.Println("Mögliche Operatoren sind \"add\", \"subtract\", \"multiply\", \"divide\" und \"equal\".") + fmt.Println("Alternativ können sie auch \"+\", \"-\", \"*\", \"/\" oder \"=\" verwenden.") + fmt.Scanf("%s", &op_input) + + if op_input == eql || op_input == "=" { + fmt.Println("Ihr Ergebnis ist:", result) + break + } + + fmt.Println("Bitte geben Sie nun Ihre nächste Zahl ein:") + fmt.Scanf("%f", &temp_num_input) + + switch op_input { + + case add, "+": + result += temp_num_input + + case sub, "-": + result -= temp_num_input + + case mul, "*": + result *= temp_num_input + + case div, "/": + if temp_num_input == 0 { + fmt.Println("Es entstand ein \"division by zero\" error!") + fmt.Println("Die aktuelle Berechnung wird abgebrochen...") + break main_loop + } + + result /= temp_num_input + + default: + fmt.Println("Sie haben einen ungüligen Operator eingegeben!\r") + fmt.Println("Die aktuelle Berechnung wird abgebrochen...") + break main_loop + } + } + + fmt.Println("Für eine neue Berechnung starten Sie das Programm bitte neu.") +} diff --git a/go/00-hello-world/go.mod b/go/go.mod similarity index 100% rename from go/00-hello-world/go.mod rename to go/go.mod