//1c
struct Equipo: Comparable {
    let puntos: Int
    let nombre: String

static func < (izquierdo: Equipo, derecho: Equipo) -> Bool {
    if(izquierdo.puntos == derecho.puntos){
        return izquierdo.nombre < derecho.nombre
    }
    return izquierdo.puntos < derecho.puntos
}
}
//2a

struct Cuadrado {
    var lado: Double
    var area : Double {
     get{
         return lado*lado
     }   
    }
}


var cuadrado = Cuadrado(lado: 4.0)
print(cuadrado.area) 
cuadrado.lado = 10.0
print(cuadrado.area) 

protocol Persona {
    var nombre: String {get}
    func encantada() -> Persona 
    func refrescada() -> Persona 
}

enum Pocion {
    case magica, refrescante, venenosa

    func esBebida(por persona: Persona) -> Persona? {
        switch self {
            case .magica:
                return persona.encantada()
            case .refrescante:
                return persona.refrescada()
            default:
                return nil
        }
    }
}
protocol A {
    var valor: Int {get set}
    func foo(a: Int) -> Int
}
protocol B {
    mutating func bar()
}
struct MiStruct: A, B {
    var valor: Int
    func foo(a :Int) -> Int{
        return a
    }
    mutating func bar(){
         self.valor = 67
    }

}
struct Circulo {
    var radio: Double
    static func + (izquierdo: Circulo, derecho: Circulo) -> Circulo{
        return Circulo(radio :izquierdo.radio + derecho.radio)
    }
}

let c1 = Circulo(radio: 5.0)
let c2 = Circulo(radio: 10.0)
let c3 = c1 + c2
print("El radio de la suma es: \(c3.radio)")
// Imprime: El radio de la suma es: 15.0

protocol P {
   var p: Int { get }
}
class A1: P {
   var p = 0
   var a1 = 0
}
class A2: P {
   var p = 1
   var a2 = 0
}

var array: [P] = [A1(), A2()]
for i in array {

    if let elema1 = i as? A1{
        print("p: \(elema1.p), a1: (elema1.a1)")
    }
    if let elema2 = i as? A2{
        print("p: \(elema2.p), a2: (elema1.a2)")
    }
}


protocol TieneVelocidad {
    func velocidadActual () -> Double
}

class Vehiculo {
    var velocidad = 0.0
    func velocidadActual() -> Double {
        return velocidad
    }
}

class Tren {
    static let velocidadEnMarcha = 300.0
    var pasajeros = 0
    var enMarcha = false
}

extension Vehiculo: TieneVelocidad {}

extension Tren: TieneVelocidad {
    func velocidadActual() -> Double {
        if enMarcha {
            return Tren.velocidadEnMarcha
        } else {
            return 0.0
        }
    }
}

var vehiculo1 = Vehiculo()
var tren1 = Tren()
tren1.enMarcha = true

let transportes: [TieneVelocidad] = [vehiculo1, tren1]

for i in transportes {
    print(i.velocidadActual())
}
struct Timer{
    static var pasosTotales: Double = 0
    var segundos: Double
    init(segundos: Double){
        self.segundos = segundos
    }
    mutating func paso(){
        self.segundos -= 1
        Timer.pasosTotales += 1
    } 
    static func + (izq: Timer, der: Timer) -> Timer{
        return Timer(segundos :izq.segundos + der.segundos)
    }
}
var t1 = Timer(segundos: 10)
var t2 = Timer(segundos: 5)
for _ in 0...4 {
    t1.paso()
}
for _ in 0...2 {
    t2.paso()
}
var t3 = t1 + t2
t3.paso()
print("Segundos del temporizador 1: \(t1.segundos)")
print("Segundos del temporizador 2: \(t2.segundos)")
print("Segundos del temporizador 3: \(t3.segundos)")
print("Pasos totales: \(Timer.pasosTotales)")
//5
protocol Figura{
    var centro : Punto {get set}
    var area:Double{get}
    var tamaño:Tamaño{get}
    func descripcion()->String
}
struct Punto {
    var x = 0.0, y = 0.0
}

struct Tamaño {
    var ancho = 0.0, alto = 0.0
}

struct Circulo {
    var centro = Punto()
    var radio = 0.0

    var area: Double {
        get {
            return Double.pi * radio * radio
        }
        set {
            // Despejamos el radio: r = √(Area / π)
            radio = (newValue / Double.pi).squareRoot()
        }
    }
}

struct Rectangulo {
    var origen = Punto()
    var tamaño = Tamaño()

    var centro: Punto {
        get {
            // El centro es el origen más la mitad del ancho y el alto
            let centroX = origen.x + (tamaño.ancho / 2.0)
            let centroY = origen.y + (tamaño.alto / 2.0)
            return Punto(x: centroX, y: centroY)
        }
        set {
            // Si nos dan un nuevo centro, el origen retrocede la mitad del tamaño
            origen.x = newValue.x - (tamaño.ancho / 2.0)
            origen.y = newValue.y - (tamaño.alto / 2.0)
        }
    }

    var area: Double {
        // Al ser de solo lectura, podemos omitir la palabra 'get'
        return tamaño.ancho * tamaño.alto
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: