forked from steger/pr3-ws202526
solution calculator
parent
bbf606e2d5
commit
e5d32762ca
|
|
@ -5,18 +5,46 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
const (
|
||||||
|
add = "add"
|
||||||
|
subtract = "subtract"
|
||||||
|
multiply = "multiply"
|
||||||
|
divide = "divide"
|
||||||
|
exit = "exit"
|
||||||
|
)
|
||||||
|
|
||||||
//TODO: implement according to README.md
|
fmt.Println("Welcome to the Simple Calculator!")
|
||||||
|
for {
|
||||||
|
var operation string
|
||||||
|
fmt.Print("\nEnter operation (add, subtract, multiply, divide, exit): ")
|
||||||
|
fmt.Scan(&operation)
|
||||||
|
|
||||||
//the following code just demonstrates how to use fmt.Scan
|
if operation == exit {
|
||||||
|
fmt.Println("Goodbye!")
|
||||||
var str string
|
break
|
||||||
fmt.Print("Please enter a string: ")
|
}
|
||||||
fmt.Scan(&str)
|
|
||||||
|
var num1, num2 float64
|
||||||
var x float64
|
fmt.Print("Enter the first number: ")
|
||||||
fmt.Print("Please enter a float: ")
|
fmt.Scan(&num1)
|
||||||
fmt.Scan(&x)
|
fmt.Print("Enter the second number: ")
|
||||||
|
fmt.Scan(&num2)
|
||||||
fmt.Printf("You entered '%s' and %f\n", str, x)
|
|
||||||
|
switch operation {
|
||||||
|
case add:
|
||||||
|
fmt.Printf("Result: %.2f\n", num1+num2)
|
||||||
|
case subtract:
|
||||||
|
fmt.Printf("Result: %.2f\n", num1-num2)
|
||||||
|
case multiply:
|
||||||
|
fmt.Printf("Result: %.2f\n", num1*num2)
|
||||||
|
case divide:
|
||||||
|
if num2 == 0 {
|
||||||
|
fmt.Println("Error: Division by zero is not allowed!")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Result: %.2f\n", num1/num2)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
fmt.Println("Invalid operation. Please try again.")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue