Advertisement
QwertyAvatar

Swift 12

Jan 16th, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.23 KB | Software | 0 0
  1. //
  2. //  main.swift
  3. //  Zad_12_1
  4. //
  5.  
  6.  
  7. import Foundation
  8.  
  9. protocol LiczbaZespolona {
  10.     var re1: Int {get}
  11.     var im1: Int {get}
  12.     var re2: Int {get}
  13.     var im2: Int {get}
  14.     func OperacjeNaLiczbachZespolonych(Re1: Int, Im1: Int, Re2: Int, Im2: Int)
  15. }
  16.  
  17. class Obliczenia : LiczbaZespolona {
  18.     var re1: Int = 0
  19.     var im1: Int = 0
  20.     var re2: Int = 0
  21.     var im2: Int = 0
  22.     init(re1: Int, im1: Int, re2: Int, im2: Int){
  23.         self.re1 = re1
  24.         self.im1 = im1
  25.         self.re2 = re2
  26.         self.im2 = im2
  27.     }
  28.     func OperacjeNaLiczbachZespolonych(Re1: Int, Im1: Int, Re2: Int, Im2: Int) {
  29.         let Rex: Int = Re1 + Re2
  30.         let Imx: Int = Im1 + Im2
  31.         let Rey: Int = Re1 - Re2
  32.         let Imy: Int = Im1 - Im2
  33.         if(Imx >= 0){
  34.             print("Suma liczb to: \(Rex) + \(Imx)i")
  35.         }
  36.         else{
  37.             print("Suma liczb to: \(Rex)\(Imx)i")
  38.         }
  39.         if(Imy >= 0){
  40.             print("Roznica liczb to: \(Rey) + \(Imy)i")
  41.         }
  42.         else{
  43.             print("Roznica liczb to: \(Rey)\(Imy)i")
  44.         }
  45.         if(Im1 >= 0){
  46.             print("Liczba 1 to: \(Re1) + \(Im1)i")
  47.         }
  48.         else{
  49.             print("Liczba 1 to: \(Re1)\(Im1)i")
  50.         }
  51.     }
  52. }
  53.  
  54. print("Podaj Re1:")
  55. guard let Re1 = Int(readLine()!) else
  56. {
  57.     fatalError("Zle")
  58. }
  59. print("Podaj Im1:")
  60. guard let Im1 = Int(readLine()!) else
  61. {
  62.     fatalError("Zle")
  63. }
  64. print("Podaj Re2:")
  65. guard let Re2 = Int(readLine()!) else
  66. {
  67.     fatalError("Zle")
  68. }
  69. print("Podaj Im2:")
  70. guard let Im2 = Int(readLine()!) else
  71. {
  72.     fatalError("Zle")
  73. }
  74. let z = Obliczenia(re1: Re1, im1: Im1, re2: Re2, im2: Im2)
  75. z.OperacjeNaLiczbachZespolonych(Re1: Re1, Im1: Im1, Re2: Re2, Im2: Im2)
  76.  
  77. /////////////////////////////////////////////////
  78.  
  79. //
  80. //  main.swift
  81. //  Zad_12_2
  82. //
  83.  
  84.  
  85. import Foundation
  86.  
  87. protocol LiczbaLosowa {
  88.     var i1: Int {get}
  89.     var i2: Int {get}
  90.     func generujLiczbe() -> Int
  91. }
  92.  
  93. class Obliczenia : LiczbaLosowa {
  94.     var i1: Int = 0
  95.     var i2: Int = 0
  96.     init(i1: Int, i2: Int){
  97.         self.i1 = i1
  98.         self.i2 = i2
  99.     }
  100.     func generujLiczbe() -> Int {
  101.         let randomNumber = Int.random(in: -10...10)
  102.         return randomNumber
  103.     }
  104.     func wyswietl(){
  105.         print("Liczba1: \(i1), oraz 2: \(i2)")
  106.     }
  107.     func potega(p: Int){
  108.         let a = pow(Decimal(i1), p)
  109.         print("Potega koncowa to: \(a)")
  110.     }
  111. }
  112.  
  113. print("Podaj i1:")
  114. guard let i1 = Int(readLine()!) else
  115. {
  116.     fatalError("Zle")
  117. }
  118. print("Podaj i2:")
  119. guard let i2 = Int(readLine()!) else
  120. {
  121.     fatalError("Zle")
  122. }
  123. let z = Obliczenia(i1: i1, i2: i2)
  124. print("Wygenerowana liczba: \(z.generujLiczbe())")
  125. z.wyswietl()
  126. z.potega(p: 3)
  127.  
  128. ///////////////////////////////////////////////////////
  129.  
  130. //
  131. //  main.swift
  132. //  Zad_12_3
  133. //  to-do
  134. //
  135.  
  136.  
  137. import Foundation
  138.  
  139. enum Blad: Error{
  140.     case dziel_0
  141.     case pierw_zly
  142. }
  143.  
  144. func lap(liczba: Int) throws {
  145.     guard(liczba < 0) else {
  146.         throw Blad.pierw_zly
  147.     }
  148.     guard(liczba == 0) else {
  149.         throw Blad.dziel_0
  150.     }
  151. }
  152.  
  153. let h: String = "k7kk+"
  154. do {
  155. try walidujHaslo(haslo: h)
  156. print("Poprawne hasło")
  157. } catch WalidacjaHasla.zaKrotkie {
  158. print("Podane haslo jest za krótkie")
  159. } catch WalidacjaHasla.zaDlugie {
  160. print("Podane hasło jest za długie")
  161. } catch WalidacjaHasla.symbolMatem {
  162. print("Hasło zawiera znak matematyczny")
  163. }
  164.  
  165.  
  166. protocol LiczbaLosowa {
  167.     var i1: Int {get}
  168.     var i2: Int {get}
  169.     func generujLiczbe() -> Int
  170. }
  171.  
  172. class Obliczenia : LiczbaLosowa {
  173.     var i1: Int = 0
  174.     var i2: Int = 0
  175.     init(i1: Int, i2: Int){
  176.         self.i1 = i1
  177.         self.i2 = i2
  178.     }
  179.     func generujLiczbe() -> Int {
  180.         let randomNumber = Int.random(in: -10...10)
  181.         return randomNumber
  182.     }
  183.     func wyswietl(){
  184.         print("Liczba1: \(i1), oraz 2: \(i2)")
  185.     }
  186.     func potega(p: Int){
  187.         let a = pow(Decimal(i1), p)
  188.         print("Potega koncowa to: \(a)")
  189.     }
  190. }
  191.  
  192. print("Podaj i1:")
  193. guard let i1 = Int(readLine()!) else
  194. {
  195.     fatalError("Zle")
  196. }
  197. print("Podaj i2:")
  198. guard let i2 = Int(readLine()!) else
  199. {
  200.     fatalError("Zle")
  201. }
  202. let z = Obliczenia(i1: i1, i2: i2)
  203. print("Wygenerowana liczba: \(z.generujLiczbe())")
  204. z.wyswietl()
  205. z.potega(p: 3)
  206.  
Tags: swift
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement