Advertisement
QwertyAvatar

Swift - przyklad kolosa II

Jan 23rd, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.71 KB | Software | 0 0
  1. /*Napisz program w jezyku Swift:
  2. 1. Utworz klase Osoba majaca pola prywatne: imie, nazwisko, waga, wzrost, pesel oraz typ enum obywatelstwo (case: hiszpanskie, polskie, niemieckie);
  3. 2. Klasa Osoba ma miec metody:
  4. a. inicjalizujaca dane;
  5. b. obliczajaca wiek na podstawie peselu oraz podanej daty (uzyj DateFormatter);
  6. c. wyswietlajaca dane;
  7. 3. Utworz klase Tenisista, dziedziczaca po klasie Osoba, majaca pola: rodzaj_gry (single, double, singleAndDouble - typ enum), liczba_punktow, obecna_pozycja, najwyzsza_pozycja oraz tablice krotek zawierajaca nazwe turnieju typu String i ilosc punktow typu int;
  8. 4. Tenisista ma miec funkcje:
  9. a. inicjalizujaca dane;
  10. b. wyswietlajaca dane klasy, nadpisuje Osoba;
  11. c. dodajace do tablicy kolejny rekord;
  12. 5. W programie ma nastapic utworzenie tablicy obiektow klasy Tenisista, a nastepnie wyswietlenie osoby ktora ma rodzaj_gry = double i obywatelstwo = hiszpanskie. Jesli nie bedzie takiej osoby, wyswietl komunikat: "Brak".*/
  13.  
  14. enum Citizenship {
  15.     case Spanish, Polish, German
  16. }
  17.  
  18. class Person {
  19.     private var name: String
  20.     private var surname: String
  21.     private var weight: Double
  22.     private var height: Double
  23.     private var pesel: String
  24.     private var citizenship: Citizenship
  25.  
  26.     init(name: String, surname: String, weight: Double, height: Double, pesel: String, citizenship: Citizenship) {
  27.         self.name = name
  28.         self.surname = surname
  29.         self.weight = weight
  30.         self.height = height
  31.         self.pesel = pesel
  32.         self.citizenship = citizenship
  33.     }
  34.  
  35.     func calculateAge(date: String) -> Int {
  36.         let dateFormatter = DateFormatter()
  37.         dateFormatter.dateFormat = "yyyy-MM-dd"
  38.         let birthdate = dateFormatter.date(from: date)
  39.         let currentDate = Date()
  40.  
  41.         let ageComponents = Calendar.current.dateComponents([.year], from: birthdate!, to: currentDate)
  42.         return ageComponents.year!
  43.     }
  44.  
  45.     func displayData() {
  46.         print("Name: \(name)")
  47.         print("Surname: \(surname)")
  48.         print("Weight: \(weight)")
  49.         print("Height: \(height)")
  50.         print("Pesel: \(pesel)")
  51.         print("Citizenship: \(citizenship)")
  52.     }
  53. }
  54.  
  55. enum GameType {
  56.     case single, double, singleAndDouble
  57. }
  58.  
  59. class TennisPlayer: Person {
  60.     private var gameType: GameType
  61.     private var points: Int
  62.     private var currentPosition: Int
  63.     private var highestPosition: Int
  64.     private var tournamentPoints: [(String, Int)]
  65.  
  66.     init(name: String, surname: String, weight: Double, height: Double, pesel: String, citizenship: Citizenship, gameType: GameType, points: Int, currentPosition: Int, highestPosition: Int, tournamentPoints: [(String, Int)]) {
  67.         self.gameType = gameType
  68.         self.points = points
  69.         self.currentPosition = currentPosition
  70.         self.highestPosition = highestPosition
  71.         self.tournamentPoints = tournamentPoints
  72.         super.init(name: name, surname: surname, weight: weight, height: height, pesel: pesel, citizenship: citizenship)
  73.     }
  74.  
  75.     override func displayData() {
  76.         super.displayData()
  77.         print("Game type: \(gameType)")
  78.         print("Points: \(points)")
  79.         print("Current position: \(currentPosition)")
  80.         print("Highest position: \(highestPosition)")
  81.         print("Tournament points: \(tournamentPoints)")
  82.     }
  83.  
  84.     func addTournamentPoints(tournament: String, points: Int) {
  85.         tournamentPoints.append((tournament, points))
  86.     }
  87. }
  88.  
  89. var tennisPlayers: [TennisPlayer] = []
  90.  
  91. // Add some sample data
  92. tennisPlayers.append(TennisPlayer(name: "Rafael", surname: "Nadal", weight: 85, height: 185, pesel: "12345678901", citizenship: .Spanish, gameType: .single, points: 10000, currentPosition: 2, highestPosition: 1, tournamentPoints: [("Wimbledon", 2000), ("US Open", 1500)]))
  93. tennisPlayers.append(TennisPlayer(name: "Roger", surname: "Federer", weight: 80, height: 185, pesel: "23456789012", citizenship: .Swiss, gameType: .double, points: 8000, currentPosition: 3, highestPosition: 1, tournamentPoints: [("Australian Open", 1000), ("French Open", 500)]))
  94. tennisPlayers.append(TennisPlayer(name: "Novak", surname: "Djokovic", weight: 75, height: 185, pesel: "34567890123", citizenship: .Serbian, gameType: .singleAndDouble, points: 6000, currentPosition: 1, highestPosition: 1, tournamentPoints: [("Roland Garros", 750), ("Wimbledon", 1250)]))
  95.  
  96. var found = false
  97. for player in tennisPlayers {
  98. if player.gameType == .double && player.citizenship == .Spanish {
  99. player.displayData()
  100. found = true
  101. break
  102. }
  103. }
  104. if !found {
  105. print("Brak")
  106. }
  107.  
  108. /*Napisz program w jezyku Swift:
  109. 1. Utworz klase Punkt zawierajaca wspolrzedna x i y punktu;
  110. 2. Utworz klase Dane2P majaca prywatne pole odcinek AB, zapisany w postaci krotki punktow klasy Punkt;
  111. 3. Klasa Dane2P posiada metody:
  112. a. inicjalizuje dane odcinka;
  113. b. oblicza dlugosc odcinka wg wzoru: d=sqrt((xB-xA)^2+(yB-yA)^2), przyjmuje 2 parametry w postaci punktow;
  114. c. oblicza wspolrzedne wektora zbudowanego na podstawie dwoch punktow przyjetych w parametrze: AB = [xB-xA, yB-yA];
  115. d. wyswietla dane odcinka;
  116. 4. W programie utworz tablice z minimum 6 odcinkami, przetestuj wszystkie metody klasy Dane2P dla kazdego z nich, oraz wyswietl trzy odcinki, z ktorych nie mozna zbudowac trojkata, lub informacje ze takowych nie ma (utworz to w funkcji).*/
  117.  
  118.  
  119. class Punkt {
  120.     var x: Double
  121.     var y: Double
  122.    
  123.     init(x: Double, y: Double) {
  124.         self.x = x
  125.         self.y = y
  126.     }
  127. }
  128.  
  129. class Dane2P {
  130.     private var odcinekAB: (Punkt, Punkt)
  131.    
  132.     init(punktA: Punkt, punktB: Punkt) {
  133.         self.odcinekAB = (punktA, punktB)
  134.     }
  135.    
  136.     func inicjalizujDane(punktA: Punkt, punktB: Punkt) {
  137.         self.odcinekAB = (punktA, punktB)
  138.     }
  139.    
  140.     func obliczDlugosc() -> Double {
  141.         let xA = odcinekAB.0.x
  142.         let yA = odcinekAB.0.y
  143.         let xB = odcinekAB.1.x
  144.         let yB = odcinekAB.1.y
  145.         return sqrt(pow(xB - xA, 2) + pow(yB - yA, 2))
  146.     }
  147.    
  148.     func obliczWspolrzedneWektora() -> (Double, Double) {
  149.         let xA = odcinekAB.0.x
  150.         let yA = odcinekAB.0.y
  151.         let xB = odcinekAB.1.x
  152.         let yB = odcinekAB.1.y
  153.         return (xB - xA, yB - yA)
  154.     }
  155.    
  156.     func wyswietlDane() {
  157.         print("Odcinek AB: (\(odcinekAB.0.x), \(odcinekAB.0.y)) - (\(odcinekAB.1.x), \(odcinekAB.1.y))")
  158.     }
  159. }
  160.  
  161. func testujMetody() {
  162.     let odcinek1 = Dane2P(punktA: Punkt(x: 2, y: 3), punktB: Punkt(x: 5, y: 7))
  163.     let odcinek2 = Dane2P(punktA: Punkt(x: -1, y: 0), punktB: Punkt(x: 4, y: 3))
  164.     let odcinek3 = Dane2P(punktA: Punkt(x: 3, y: -2), punktB: Punkt(x: 8, y: 1))
  165.     let odcinek4 = Dane2P(punktA: Punkt(x: 0, y: 0), punktB: Punkt(x: 3, y: 4))
  166.     let odcinek5 = Dane2P(punktA: Punkt(x: -2, y: -3), punktB: Punkt(x: 0, y: 0))
  167.     let odcinek6 = Dane2P(punktA: Punkt(x: 1, y: 2), punktB: Punkt(x: 2, y: 4))
  168.     let odcinki = [odcinek1, odcinek2, odcinek3, odcinek4, odcinek5, odcinek6]
  169. for odcinek in odcinki {
  170.     odcinek.wyswietlDane()
  171.     print("Dlugosc odcinka: \(odcinek.obliczDlugosc())")
  172.     print("Wspolrzedne wektora: \(odcinek.obliczWspolrzedneWektora())")
  173. }
  174.  
  175. var brakTrojkata = true
  176. for i in 0..<odcinki.count {
  177.     for j in i+1..<odcinki.count {
  178.         for k in j+1..<odcinki.count {
  179.             let dlugosc1 = odcinki[i].obliczDlugosc()
  180.             let dlugosc2 = odcinki[j].obliczDlugosc()
  181.             let dlugosc3 = odcinki[k].obliczDlugosc()
  182.             if (dlugosc1 + dlugosc2 > dlugosc3 && dlugosc1 + dlugosc3 > dlugosc2 && dlugosc2 + dlugosc3 > dlugosc1) {
  183.                 brakTrojkata = false
  184.                 print("Odcinek \(i+1), \(j+1), \(k+1) pozwala na utworzenie trojkata")
  185.             }
  186.         }
  187.     }
  188. }
  189. if brakTrojkata {
  190.     print("Nie ma odcinkow pozwalajacych na utworzenie trojkata")
  191. }
  192. }
  193.  
  194. testujMetody()
Tags: swift
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement