forked from steger/pr3-ws202627
Besser
parent
7d84b5be29
commit
96daf2f7e2
|
|
@ -58,80 +58,3 @@ 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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,74 @@
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue