isama92

Swift - First

Mar 28th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.39 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. func divisibile(n1:Int, n2:Int)->Bool?{
  4.     if n1 % n2 == 0{
  5.         return true
  6.     }
  7.  
  8.     return nil
  9. }
  10.  
  11. let num1 = 957
  12. let num2 = 3
  13. if let safeNum = divisibile(n1: num1, n2: num2){
  14.     print("\(num1) is divisible by \(num2)")
  15. }else{
  16.     print(String(num1)+" is not divisible by le palle "+String(num2))
  17. }
  18.  
  19.  
  20.  
  21. func bisestile (n1: Int)->Bool?{
  22.     if  n1 % 400 == 0 || n1 % 4 == 0 &&  n1 % 100 != 0  {
  23.         return true
  24.     }
  25.     return nil
  26. }
  27.  
  28. let num3 = 9980
  29. if let safeNum = bisestile(n1: num3){
  30.     print("\(num3) è bisestile")
  31. }else{
  32.     print("\(num3) è non bisestile")
  33. }
  34.  
  35.  
  36.  
  37. func ripetute (str: String)->Bool{
  38.     var j:Int
  39.     let charString = Array(str.characters)
  40.     for i in 0...charString.count - 1 {
  41.         j = i + 1
  42.         while j < charString.count {
  43.             if charString[i] == charString[j]{
  44.                 return true
  45.             }
  46.             j = j + 1
  47.         }
  48.     }
  49.     return false
  50. }
  51.  
  52.     if ripetute(str: "precipitevolissivevolmente"){
  53.     print("Ci sono lettere ripetute")
  54. }else{
  55.     print("Non ci sono lettere ripetute")
  56. }
  57.  
  58.  
  59.  
  60. struct Rectangle{
  61.     var width: Int
  62.     var height: Int
  63.     var area: Int
  64.     init(width: Int, height: Int){
  65.         self.width = width
  66.         self.height = height
  67.         self.area = width * height
  68.     }
  69.  
  70. }
  71. extension Rectangle{
  72.     func fitsInside(_ other : Rectangle)-> Bool{
  73.         return (width < other.width) && (height < other.height)
  74.     }
  75.    
  76.     func isGreaterThan(_ otherRect: Rectangle) -> Bool{
  77.         return (self.area > otherRect.area)
  78.     }
  79. }
  80.  
  81. var rectangle1 = Rectangle( width: 2, height: 5)
  82. var rectangle2 = Rectangle( width: 30, height: 20)
  83.  
  84. print("il rettangolo con altezza\(rectangle1.width),e larghezza \(rectangle1.height), ha l' area di \(rectangle1.area)")
  85.  
  86. print("il rettangolo con altezza\(rectangle2.width),e larghezza \(rectangle2.height), ha l' area di \(rectangle2.area)")
  87.  
  88. if !rectangle2.fitsInside(rectangle1){
  89.     print("il secondo rettangolo non è contenuto nel primo")
  90. }else{
  91.     print("il secondo rettangolo è contenuto nel primo")
  92. }
  93.  
  94. if rectangle1.isGreaterThan(rectangle2) {
  95.     print("L'area del primo rettangolo è maggiore di quella del secondo")
  96. } else {
  97.     print("L'area del secondo rettangolo è maggiore di quella del primo")
  98. }
  99.  
  100.  
  101. class Fish{
  102.     var name: String
  103.     var type: String
  104.    
  105.     init(_ name: String){
  106.         self.name = name
  107.         self.type = "Fish"
  108.     }
  109.     func swim(){
  110.         print("I'm " + name + ", a " + type + " who is swimming!")
  111.     }
  112. }
  113.  
  114. class FlyingFish: Fish{
  115.     override init(_ name: String){//SOVRASCRIVO L'INIT DELLA CLASSE CREATA PRECEDENTEMENTE
  116.         super.init(name)
  117.         self.type = "Flying Fish"
  118.     }
  119.     func fly(){
  120.         print("I'm " + name + ", a " + type + " who is flying!")
  121.     }
  122. }
  123. var roger = Fish("Roger")
  124. var gaetano = FlyingFish("Gaetano")
  125.  
  126. roger.swim()
  127. gaetano.fly()
  128. gaetano.swim()
  129.  
  130. enum Alegnment{
  131.     case left, right, up, down
  132. }
  133.  
  134. let textAlignment = Alegnment.left
  135.  
  136. switch  textAlignment{
  137. case .left:
  138.     print("sono a sinistra")
  139. case .right:
  140.     print("sono a destra")
  141. case .up:
  142.     print("sono sopra")
  143. case .down:
  144.     print("sono sotto")
  145. //default:
  146. //    print("non ci sono")
  147. }
  148.  
  149.  
  150. class Triangolo{
  151.     enum TipoTriangolo{
  152.         case isoscele, scaleno, equilatero, generico
  153.     }
  154.     var lato1 : Int
  155.     var lato2 : Int
  156.     var lato3 : Int
  157.     var tipo: TipoTriangolo?
  158.     init(lato1: Int, lato2: Int, lato3: Int){
  159.         self.lato1 = lato1
  160.         self.lato2 = lato2
  161.         self.lato3 = lato3
  162.         self.tipo = getTriangoloTipo(lato1, lato2, lato3)
  163.     }
  164.        
  165.        
  166.     func getTriangoloTipo(_ lato1: Int, _ lato2: Int, _ lat3: Int)-> TipoTriangolo{
  167.         if (lato1 == lato2 && lato1 != lato3 && lato2 != lato3) || (lato1 != lato2 && lato1 == lato3 && lato2 != lato3) || (lato1 != lato2 && lato1 != lato3 && lato2 == lato3){
  168.             return TipoTriangolo.isoscele
  169.         }
  170.         else if lato1 != lato2 && lato2 != lato3 && lato1 != lato3{
  171.             return TipoTriangolo.scaleno
  172.         }
  173.         else if lato1 == lato2 && lato2 == lato3{
  174.             return TipoTriangolo.equilatero
  175.         }
  176.         return TipoTriangolo.generico
  177.     }
  178.  
  179. }
  180.  
  181.  
  182. var triangolo = Triangolo(lato1: 4, lato2: 4, lato3: 4)
  183.  
  184. if let safeTr = triangolo.tipo{
  185.         print(safeTr)
  186. }
Add Comment
Please, Sign In to add comment