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,
"/")
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-10 13:56:11