Advertisement
mikolajmki

swift_lab13

Jan 16th, 2023
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.95 KB | None | 0 0
  1. //
  2. //  main.swift
  3. //  interfejsy
  4. //
  5. //  Created by student on 16/01/2023.
  6. //
  7.  
  8. import Foundation
  9.  
  10. //print("Hello, World!")
  11. //
  12. //let liczba = OperacjeNaLiczbachZespolonych(re: 2, im: 2);
  13. //
  14. //print(
  15. //    liczba.wyswietlLiczbe()
  16. //)
  17. //
  18. //liczba.dodaj()
  19. //
  20. //print(
  21. //    liczba.wyswietlLiczbe()
  22. //)
  23. //
  24. //liczba.odejmij()
  25. //
  26. //print(
  27. //    liczba.wyswietlLiczbe()
  28. //)
  29.  
  30. //let liczba = Liczby(l1: 2, l2: 2);
  31. //let pot = liczba.generujLiczbe();
  32. //print(pot);
  33. //let lpot = liczba.potega(pot: pot);
  34. //print(lpot);
  35.  
  36.  
  37. let l1: Int = 4;
  38. let l2: Int = 0;
  39.  
  40. func podziel(a: Int, b: Int) throws {
  41.     guard b != 0 else {
  42.         throw Bledy.dzieleniePrzez0;
  43.     }
  44. }
  45.  
  46. func pierwiastek(a: Int) throws {
  47.    
  48.     guard a < 0 else {
  49.         throw Bledy.pierwiastekNiedodatniej;
  50.     }
  51. }
  52.  
  53. do {
  54.     try podziel(a: l1, b: l2);
  55. }
  56. catch Bledy.dzieleniePrzez0 {
  57.     print("Dzielenie przez 0")
  58. }
  59.  
  60. do {
  61.     try pierwiastek(a: -1)
  62. } catch Bledy.pierwiastekNiedodatniej {
  63.     print("Pierwiastek z niedodatniej")
  64. }
  65.  
  66.  
  67. //
  68. //  Liczba.swift
  69. //  interfejsy
  70. //
  71. //  Created by student on 16/01/2023.
  72. //
  73.  
  74. import Foundation
  75.  
  76. enum Bledy: Error {
  77.     case dzieleniePrzez0;
  78.     case pierwiastekNiedodatniej;
  79. }
  80.  
  81. protocol LiczbaLosowa {
  82.     func generujLiczbe() -> Int;
  83. }
  84.  
  85. class Liczby: LiczbaLosowa {
  86.     var l1: Int;
  87.     var l2: Int;
  88.    
  89.     init(l1: Int, l2: Int) {
  90.         self.l1 = l1
  91.         self.l2 = l2
  92.     }
  93.    
  94.     func generujLiczbe() -> Int {
  95.         return Int.random(in: 0...10);
  96.     }
  97.    
  98.     func wyswietl() -> String {
  99.         return "l1: \(self.l1) l2: \(self.l2)";
  100.     }
  101.    
  102.     func potega(pot: Int) -> Decimal {
  103.         return pow(Decimal(self.l1), pot);
  104.     }
  105. }
  106.  
  107.  
  108. //
  109. //  OperacjeNaLiczbachZespolonych.swift
  110. //  interfejsy
  111. //
  112. //  Created by student on 16/01/2023.
  113. //
  114.  
  115. import Foundation
  116.  
  117. protocol LiczbaZespolona {
  118.     var re: Int {get}
  119.     var im: Int {get}
  120. }
  121.  
  122. class OperacjeNaLiczbachZespolonych: LiczbaZespolona {
  123.     var re: Int;
  124.     var im: Int;
  125.    
  126.     init(re: Int, im: Int) {
  127.         self.re = re
  128.         self.im = im
  129.     }
  130.    
  131.     func dodaj() {
  132.         print("Podaj czesc rzeczywista dodawania: ")
  133.         guard let a: Int = Int(readLine()!) else {
  134.             fatalError("Nie liczba");
  135.         }
  136.         print("Podaj czesc zespolona dodawania: ")
  137.         guard let b: Int = Int(readLine()!) else {
  138.             fatalError("Nie liczba");
  139.         }
  140.         self.re += a;
  141.         self.im += b;
  142.     }
  143.    
  144.     func odejmij() {
  145.         print("Podaj czesc rzeczywista odejmowania: ")
  146.         guard let a: Int = Int(readLine()!) else {
  147.             fatalError("Nie liczba");
  148.         }
  149.         print("Podaj czesc zespolona odejmowania: ")
  150.         guard let b: Int = Int(readLine()!) else {
  151.             fatalError("Nie liczba");
  152.         }
  153.         self.re -= a;
  154.         self.im -= b;
  155.     }
  156.    
  157.     func wyswietlLiczbe() -> String {
  158.         return "\(self.re) + \(self.im)j";
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement