Swift-----運算符重載

1. 現有一個向量,咱們重載它的「+」、「-」、「*」、「+=」運算符。swift

 注意: 「=」是不能夠進行重載的。post

struct Vector3 {
    
    var x: Double = 0.0
    var y: Double = 0.0
    var z: Double = 0.0

}

// 重載加號(+)
func + (left: Vector3, right: Vector3) -> Vector3 {
    return Vector3(x: left.x + right.x, y: left.y + right.y, z: left.z + right.z)
}

// 重載減號(-)
func - (left: Vector3, right: Vector3) -> Vector3 {
    return Vector3(x: left.x - right.x, y: left.y - right.y, z: left.z - right.z)
}

// 重載乘號(*)
func *(left: Vector3, right: Vector3) -> Double {
    return left.x * right.x + left.y * right.y + left.z * right.z
}

// 重載負號(-),只有一個參數  因爲不知道「-」是放在參數的左側仍是右側,故須要添加關鍵字prefix,表明「-」是在參數的左側
prefix func - (value: Vector3) -> Vector3 {
    return Vector3(x: -value.x, y: -value.y, z: -value.z)
}

// 重載 +=  因爲是把該操做會使left值發生改變,故left須要用到關鍵字 inout;因爲「+」方法在以前定義過,故在這個方法中能夠直接使用「+」方法。
func += (left: inout Vector3, right: Vector3){
    left = left + right
}

var v1 = Vector3(x: 1, y: 1, z: 1)
let v2 = Vector3(x: 3, y: 4, z: 5)

v1 + v2  // {x: 4, y: 5, z: 6}
v1 - v2  // {x: -2, y: -3, z: -4}
v1 * v2  // 12
-v1    // {x: -1, y: -1, z: -1}

v1 += v2  
v1    // {x: 4, y: 5, z: 6}

 2. 重載比較運算符spa

func == (left: Vector3, right: Vector3) -> Bool {
    return left.x == right.x && left.y == right.y && left.z == right.z
}

func != (left: Vector3, right: Vector3) -> Bool {
    return !(left == right)
}

func > (left: Vector3, right: Vector3) -> Bool {
    
    if left.x != right.x {return left.x > right.x}
    if left.y != right.y {return left.y > right.y}
    if left.z != right.z {return left.z > right.z}
    
    return false
}

func >= (left: Vector3, right: Vector3) -> Bool {
    return left > right || left == right
}

func < (left: Vector3, right: Vector3) -> Bool {
    return !(left >= right)
}

var v1 = Vector3(x: 1, y: 1, z: 1)
let v2 = Vector3(x: 3, y: 4, z: 5)

v1 == v2  // false
v1 != v2  // true
v1 > v2   // false
v1 >= v2  // false
v1 < v2   // true

 3 自定義運算符code

(1)對向量定義一個「+++」運算符,相似Int中的「++」運算符blog

// 對於系統中沒有的運算符,須要經過 operator 來定義操做符,prefix表示操做符應在參數的前面
prefix
operator +++ prefix func +++ (vector: inout Vector3) -> Vector3 { let vec = vector vector += Vector3(x: 1.0, y: 1.0, z: 1.0) return vec } // postfix表示操做符應在參數的後面 postfix operator +++ postfix func +++ (vector: inout Vector3) -> Vector3 { vector += Vector3(x: 1.0, y: 1.0, z: 1.0) return vector } var vector1 = Vector3(x: 1, y: 2, z: 3) // {x: 1, y: 2, z: 3} vector1+++                   // {x: 2, y: 3, z: 4} vector1                     // {x: 2, y: 3, z: 4} var vector2 = Vector3(x: 1, y: 2, z: 3) // {x: 1, y: 2, z: 3} +++vector2                   // {x: 1, y: 2, z: 3} vect                      // {x: 2, y: 3, z: 4}

(2) 自定義一箇中間運算符 infixip

struct Vector3 {
    
    var x: Double = 0.0
    var y: Double = 0.0
    var z: Double = 0.0
    
    func length() -> Double {
        return sqrt(x * x + y * y + z * z)
    }
}

// 重載乘號(*)
func *(left: Vector3, right: Vector3) -> Double {
    return left.x * right.x + left.y * right.y + left.z * right.z
}

var vector3 = Vector3(x: 1, y: 2, z: 3)
var vector4 = Vector3(x: 2, y: 5, z: -3)

/*
在 swift 2 中自定義操做符
infix operator ^ {
    associativity left    // 左結合
    precedence 145    // 加法是140, 乘法是150. 默認是140。表示優先級高於加法,低於乘法
}
*/

// 在Swift 3 中上面的定義會出現警告信息:Operator should no longer be declared with body;use a precedence group instead
// 在Swift 3 中應該以下自定義操做符
infix operator ^: ATPrecedence
precedencegroup ATPrecedence {
    associativity: left                 // 表示左結合
    higherThan: AdditionPrecedence      // AdditionPrecedence表示加法,優先級高於加法
    lowerThan: MultiplicationPrecedence // MultiplicationPrecedence表示乘法,優先級低於乘法
}

func ^(left: Vector3, right: Vector3) -> Double {
    return acos((left * right) / (left.length() * right.length()))
}

vector3 ^ vector4
infix operator **: BTPrecedence
precedencegroup BTPrecedence {
    associativity: left        // 左結合
    higherThan: AdditionPrecedence  // 優先級高於加法運算符
}

func **(x: Double, y: Double) -> Double {
    return pow(x, y)
}

2 ** 3 ** 2     // 64
1 + 2 ** 3 ** 2 // 65
infix operator **: BTPrecedence
precedencegroup BTPrecedence {
    associativity: right      //右結合
    lowerThan: AdditionPrecedence // 優先級低於加法運算符
}

func **(x: Double, y: Double) -> Double {
    return pow(x, y)
}

2 ** 3 ** 2     // 512
1 + 2 ** 3 ** 2  //19683
相關文章
相關標籤/搜索