1
0
Fork 0
pr3-ws202627/go/01-basics/calculator.go

74 lines
1.2 KiB
Go

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