forked from steger/pr3-ws202526
51 lines
981 B
Go
51 lines
981 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
func main() {
|
|
const (
|
|
add = "add"
|
|
subtract = "subtract"
|
|
multiply = "multiply"
|
|
divide = "divide"
|
|
exit = "exit"
|
|
)
|
|
|
|
fmt.Println("Welcome to the Simple Calculator!")
|
|
for {
|
|
var operation string
|
|
fmt.Print("\nEnter operation (add, subtract, multiply, divide, exit): ")
|
|
fmt.Scan(&operation)
|
|
|
|
if operation == exit {
|
|
fmt.Println("Goodbye!")
|
|
break
|
|
}
|
|
|
|
var num1, num2 float64
|
|
fmt.Print("Enter the first number: ")
|
|
fmt.Scan(&num1)
|
|
fmt.Print("Enter the second number: ")
|
|
fmt.Scan(&num2)
|
|
|
|
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.")
|
|
}
|
|
}
|
|
}
|