From 7d84b5be29003182ea8a0d665ffde48ba1b28967 Mon Sep 17 00:00:00 2001 From: ChaosLL1337 Date: Wed, 21 Oct 2026 06:47:19 +0000 Subject: [PATCH] fgh --- go/01-basics/calculator.go | 77 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/go/01-basics/calculator.go b/go/01-basics/calculator.go index 5240c30..92dcdfc 100644 --- a/go/01-basics/calculator.go +++ b/go/01-basics/calculator.go @@ -58,3 +58,80 @@ func main() { } } } + + + +package main + +import ( + "fmt" +) + +const ( + opAdd = "add" + opSubtract = "subtract" + opMultiply = "multiply" + opDivide = "divide" + opExit = "exit" +) + +var result float64 + +func add(a, b float64) { + result = a + b +} +func multiply(a, b float64) { + result = a * b +} +func subtract(a, b float64) { + result = a - b +} +func divide(a, b float64) { + result = a / b +} + +func main() { + var x float64 + var y float64 + + //TODO: implement according to README.md + + //the following code just demonstrates how to use fmt.Scan + + for { + fmt.Print("What would you like to do? (add|subtract|divide|multiply|exit)") + var str string + fmt.Scan(&str) + if str == opExit { + break + } else { + + fmt.Print("Please enter a float: ") + fmt.Scan(&x) + + fmt.Print("Please enter a float: ") + fmt.Scan(&y) + + switch str { + case opAdd: + add(x, y) + fmt.Println(result) + case opMultiply: + multiply(x, y) + fmt.Println(result) + case opDivide: + if y == 0 { + fmt.Println("Division by 0 is not possible") + continue + } + divide(x, y) + fmt.Println(result) + case opSubtract: + subtract(x, y) + fmt.Println(result) + default: + fmt.Println("Unknown operation") + } + } + } +} \ No newline at end of file