Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.swift
- // interfejsy
- //
- // Created by student on 16/01/2023.
- //
- import Foundation
- //print("Hello, World!")
- //
- //let liczba = OperacjeNaLiczbachZespolonych(re: 2, im: 2);
- //
- //print(
- // liczba.wyswietlLiczbe()
- //)
- //
- //liczba.dodaj()
- //
- //print(
- // liczba.wyswietlLiczbe()
- //)
- //
- //liczba.odejmij()
- //
- //print(
- // liczba.wyswietlLiczbe()
- //)
- //let liczba = Liczby(l1: 2, l2: 2);
- //let pot = liczba.generujLiczbe();
- //print(pot);
- //let lpot = liczba.potega(pot: pot);
- //print(lpot);
- let l1: Int = 4;
- let l2: Int = 0;
- func podziel(a: Int, b: Int) throws {
- guard b != 0 else {
- throw Bledy.dzieleniePrzez0;
- }
- }
- func pierwiastek(a: Int) throws {
- guard a < 0 else {
- throw Bledy.pierwiastekNiedodatniej;
- }
- }
- do {
- try podziel(a: l1, b: l2);
- }
- catch Bledy.dzieleniePrzez0 {
- print("Dzielenie przez 0")
- }
- do {
- try pierwiastek(a: -1)
- } catch Bledy.pierwiastekNiedodatniej {
- print("Pierwiastek z niedodatniej")
- }
- //
- // Liczba.swift
- // interfejsy
- //
- // Created by student on 16/01/2023.
- //
- import Foundation
- enum Bledy: Error {
- case dzieleniePrzez0;
- case pierwiastekNiedodatniej;
- }
- protocol LiczbaLosowa {
- func generujLiczbe() -> Int;
- }
- class Liczby: LiczbaLosowa {
- var l1: Int;
- var l2: Int;
- init(l1: Int, l2: Int) {
- self.l1 = l1
- self.l2 = l2
- }
- func generujLiczbe() -> Int {
- return Int.random(in: 0...10);
- }
- func wyswietl() -> String {
- return "l1: \(self.l1) l2: \(self.l2)";
- }
- func potega(pot: Int) -> Decimal {
- return pow(Decimal(self.l1), pot);
- }
- }
- //
- // OperacjeNaLiczbachZespolonych.swift
- // interfejsy
- //
- // Created by student on 16/01/2023.
- //
- import Foundation
- protocol LiczbaZespolona {
- var re: Int {get}
- var im: Int {get}
- }
- class OperacjeNaLiczbachZespolonych: LiczbaZespolona {
- var re: Int;
- var im: Int;
- init(re: Int, im: Int) {
- self.re = re
- self.im = im
- }
- func dodaj() {
- print("Podaj czesc rzeczywista dodawania: ")
- guard let a: Int = Int(readLine()!) else {
- fatalError("Nie liczba");
- }
- print("Podaj czesc zespolona dodawania: ")
- guard let b: Int = Int(readLine()!) else {
- fatalError("Nie liczba");
- }
- self.re += a;
- self.im += b;
- }
- func odejmij() {
- print("Podaj czesc rzeczywista odejmowania: ")
- guard let a: Int = Int(readLine()!) else {
- fatalError("Nie liczba");
- }
- print("Podaj czesc zespolona odejmowania: ")
- guard let b: Int = Int(readLine()!) else {
- fatalError("Nie liczba");
- }
- self.re -= a;
- self.im -= b;
- }
- func wyswietlLiczbe() -> String {
- return "\(self.re) + \(self.im)j";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement