-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculadora.swift
More file actions
43 lines (40 loc) · 1.13 KB
/
Copy pathcalculadora.swift
File metadata and controls
43 lines (40 loc) · 1.13 KB
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
42
import Foundation
func realizarOperacao(_ operacao: String, entre num1: Double, e num2: Double) -> Double? {
switch operacao {
case "+":
return num1 + num2
case "-":
return num1 - num2
case "*":
return num1 * num2
case "/":
if num2 != 0 {
return num1 / num2
} else {
print("Erro: Divisão por zero!")
return nil
}
default:
print("Operação inválida!")
return nil
}
}
// Solicitação de entrada do usuário
print("Digite o primeiro número:")
if let num1 = Double(readLine() ?? "") {
print("Digite a operação (+, -, *, /):")
if let operacao = readLine(), "+-*/".contains(operacao), operacao.count == 1 {
print("Digite o segundo número:")
if let num2 = Double(readLine() ?? "") {
if let resultado = realizarOperacao(operacao, entre: num1, e: num2) {
print("Resultado: \(resultado)")
}
} else {
print("Número inválido!")
}
} else {
print("Operação inválida!")
}
} else {
print("Número inválido!")
}