Skip to content

Instantly share code, notes, and snippets.

@dedeexe
Last active September 10, 2015 04:04
Show Gist options
  • Save dedeexe/93cc2a555f2c0e6ee00e to your computer and use it in GitHub Desktop.
Save dedeexe/93cc2a555f2c0e6ee00e to your computer and use it in GitHub Desktop.
Kelvin / Celcius / Fahrenheit
//
//temperaturas.swift
//Autor....: dede.exe
//E-mail...: [email protected]
//Descrição: Convertendo temperaturas em Swift
//
class Medida
{
var valor : Float = 0.0
init(valor:Float)
{
self.valor = valor
}
}
class Temperatura : Medida
{
var kelvin : Temperatura {
return Temperatura(valor: 0.0)
}
var celcius : Temperatura {
return Temperatura(valor: 0.0)
}
var fahrenheit : Temperatura {
return Temperatura(valor: 0.0)
}
}
class Celcius : Temperatura
{
override var kelvin : Temperatura {
var valor = self.valor + 273.15
return Kelvin(valor: valor)
}
override var celcius : Temperatura {
return Celcius(valor: self.valor)
}
override var fahrenheit : Temperatura {
var valor = (self.valor * 1.8) + 32.0
return Fahrenheit(valor: valor)
}
}
class Fahrenheit : Temperatura
{
override var kelvin : Temperatura {
var valor = (( self.valor - 32.0 ) / 1.8) + 273.15
return Kelvin(valor: valor)
}
override var celcius : Temperatura {
var valor = (self.valor - 32.0) / 1.8
return Celcius(valor: valor)
}
override var fahrenheit : Temperatura {
return Fahrenheit(valor: self.valor)
}
}
class Kelvin : Temperatura
{
override var kelvin : Temperatura {
return Kelvin(valor: self.valor)
}
override var celcius : Temperatura {
var valor = self.valor - 273.15
return Celcius(valor: valor)
}
override var fahrenheit : Temperatura {
var valor = (self.valor - 273.15) * 1.8 + 32.0
return Fahrenheit(valor: self.valor)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment