func algorithm(num1: Double, num2: Double, sign: Character ) -> Double {
var number:Double
switch sign {
case "+":
number = num1 + num2
case "-":
number = num1 - num2
case "*":
number = num1 * num2
case "/":
if num2 == 0 {
number = 0
} else {
number = num1 / num2
}
default:
number = 0
}
return number
}
//自己定义托付实现各种算法
let addNumber: (Double, Double, Character) -> Double = algorithm
addNumber(100, 10, "+")
let subtractNumber: (Double, Double, Character) -> Double = algorithm
subtractNumber(100, 10, "-")
let mulNumber: (Double, Double, Character) -> Double = algorithm
mulNumber(100, 100, "*")
let divideNumber: (Double, Double, Character) -> Double = algorithm
divideNumber(100, 10, "/")