zoukankan      html  css  js  c++  java
  • 自己定义托付实现各种算法

    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, "/")


  • 相关阅读:
    求转置矩阵问题
    喷水装置
    面试之谈
    迭代任意8种数据类型数组
    Session那点事
    随机验证码
    正则表达式:网页爬虫
    模拟上传服务端
    Properties集合的练习
    用递归算法对指定目录的深度遍历
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5065707.html
Copyright © 2011-2022 走看看