1
0
Fork 0

implemented the calculator exercise and moved "go.mod"

main
Oliver Stolle 2026-03-28 14:24:35 +00:00
parent b5345c8838
commit 90244f2511
2 changed files with 64 additions and 0 deletions

View File

@ -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.")
}