-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
41 lines (35 loc) · 936 Bytes
/
main.swift
File metadata and controls
41 lines (35 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Foundation
// A simple CLI calculator
print("Welcome to Swift CLI Calculator!")
print("Enter first number: ", terminator: "")
let input1 = readLine() ?? "0"
print("Enter second number: ", terminator: "")
let input2 = readLine() ?? "0"
print("Choose operation (+, -, *, /): ", terminator: "")
let operation = readLine() ?? "+"
// Convert inputs to numbers
if let num1 = Double(input1), let num2 = Double(input2) {
var result: Double = 0
switch operation {
case "+":
result = num1 + num2
case "-":
result = num1 - num2
case "*":
result = num1 * num2
case "/":
if num2 != 0 {
result = num1 / num2
} else {
print("Error: Division by zero!")
exit(1)
}
default:
print("Invalid operation.")
exit(1)
}
print("Result: \(result)")
} else {
print("Invalid number input.")
exit(1)
}