Advertisement
tamerSabek

18032019_1

Mar 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.84 KB | None | 0 0
  1. import Foundation
  2.  
  3. class Rational: CustomStringConvertible{
  4.     var n:Int
  5.     var d:Int
  6.  
  7.     init?(numerator n:Int, denominator d:Int){
  8.         if d==0{
  9.             return nil
  10.         }
  11.         self.n = n
  12.         self.d = d
  13.     }
  14.  
  15.     var description:String{
  16.         return "\(self.n)/\(self.d)"
  17.     }
  18. }
  19.  
  20. let opR = Rational(numerator: 1, denominator:0)
  21. print(r)
  22.  
  23. print(opR)
  24.  
  25. if let r = opR{
  26.     print(r)
  27. }
  28.  
  29. if opR != nil{
  30.     let r = opR!
  31.     print(r)
  32. }
  33.  
  34. func calcValue(opR: Rational?) -> Double?{
  35.     if opR == nil
  36.     {
  37.          return nil
  38.     }
  39.     let r = opR!
  40.     return Double(r.n) / Double(r.d)
  41.  }
  42.  
  43. func calcValue(opR: Rational?) -> Double?{
  44.     guard let r = opR else{
  45.         return nil
  46.     }
  47.     return Double(r.n) / Double(r.d)
  48. }
  49.  
  50.     let num:Double? = calcValue(opR) ?? 0
  51.  
  52.     print(num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement