Advertisement
mikolajmki

swift_lab09

Dec 5th, 2022
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.40 KB | None | 0 0
  1. //
  2. //  main.swift
  3. //  lab09
  4. //
  5. //  Created by student on 05/12/2022.
  6. //
  7.  
  8. import Foundation
  9.  
  10. print("Hello, World!")
  11.  
  12. struct Complex {
  13.     var realPart: Int;
  14.     var imaginaryPart: Int;
  15.    
  16.     init(realPart: Int, imaginaryPart: Int) {
  17.         self.realPart = realPart
  18.         self.imaginaryPart = imaginaryPart
  19.     }
  20. }
  21.  
  22. struct rzutOszczepem {
  23.     var id: Int;
  24.     var proba1: Double;
  25.     var proba2: Double;
  26.     var proba3: Double;
  27.    
  28.     init(id: Int, proba1: Double, proba2: Double, proba3: Double) {
  29.         self.id = id
  30.         self.proba1 = proba1
  31.         self.proba2 = proba2
  32.         self.proba3 = proba3
  33.     }
  34. }
  35.  
  36. func complexOperations() {
  37.     var a: String = "1";
  38.     while (true) {
  39.         print("Wybierz dzialanie: ");
  40.         print("1-dodaj, 2-odejmij, 3-pomnoz, 0-wyjscie");
  41.         a = readLine()!;
  42.         if (a == "0") { break }
  43.         print("Podaj pierwsza lizcbe: ")
  44.         print("czesc rzeczywista = ");
  45.         guard let r: Int = Int(readLine()!) else {
  46.             fatalError("Nie liczba");
  47.         }
  48.         print("czesc urojona = ");
  49.         guard let i: Int = Int(readLine()!) else {
  50.             fatalError("Nie liczba");
  51.         }
  52.         var liczba1 = Complex(realPart: r, imaginaryPart: i);
  53.         print("\(liczba1.realPart) + \(liczba1.imaginaryPart)j");
  54.        
  55.         print("Podaj druga lizcbe: ")
  56.         print("czesc rzeczywista = ");
  57.         guard let r: Int = Int(readLine()!) else {
  58.             fatalError("Nie liczba");
  59.         }
  60.         print("czesc urojona = ");
  61.         guard let i: Int = Int(readLine()!) else {
  62.             fatalError("Nie liczba");
  63.         }
  64.         var liczba2 = Complex(realPart: r, imaginaryPart: i);
  65.         print("\(liczba2.realPart) + \(liczba2.imaginaryPart)j");
  66.         switch (a) {
  67.         case "1":
  68.             liczba1.realPart += liczba2.realPart;
  69.             liczba1.imaginaryPart += liczba2.imaginaryPart;
  70.         case "2":
  71.             liczba1.realPart -= liczba2.realPart;
  72.             liczba1.imaginaryPart -= liczba2.imaginaryPart;
  73.         case "3":
  74.             liczba1.realPart *= liczba2.realPart;
  75.             liczba1.imaginaryPart *= liczba2.imaginaryPart;
  76.         default:
  77.             break;
  78.         }
  79.         print("Wynik: \(liczba1.realPart) + \(liczba1.imaginaryPart)j");
  80.         print("");
  81.     }
  82. }
  83.  
  84. //complexOperations();
  85.  
  86. func rzutOszczepemFunc() {
  87.     var zawodnicy: [rzutOszczepem] = [];
  88.  
  89.     for i in 0...3 {
  90.         print("Podaj wyniki 3 prob kolejno: ");
  91.         guard let p1: Double = Double(readLine()!) else {
  92.             fatalError("Nie liczba");
  93.         }
  94.         guard let p2: Double = Double(readLine()!) else {
  95.             fatalError("Nie liczba");
  96.         }
  97.         guard let p3: Double = Double(readLine()!) else {
  98.             fatalError("Nie liczba");
  99.         }
  100.         zawodnicy.append(rzutOszczepem(id:i, proba1:p1, proba2:p2, proba3:p3));
  101.     }
  102.     zawodnicy.sort(by: {($0.proba1 * $0.proba2 * $0.proba3) / 3.0 < ($1.proba1 * $1.proba2 * $1.proba3) / 3.0});
  103.     print(zawodnicy);
  104.     print("Najlepszy zawodnik: ", zawodnicy[zawodnicy.count - 1]);
  105.     zawodnicy.remove(at: 0);
  106.     print(zawodnicy);
  107. }
  108.  
  109. func cars() {
  110.     var carsSet: Set<String> = [];
  111.     var nazwa: String;
  112.     for _ in 0...5 {
  113.         print("Podaj nazwe auta: ")
  114.         nazwa = readLine()!;
  115.         carsSet.insert(nazwa.lowercased());
  116.     }
  117.     print(carsSet);
  118.     print("Podaj model auta ktory nalezy usunac: ");
  119.     nazwa = readLine()!;
  120.     if (carsSet.contains(nazwa.lowercased())) {
  121.         carsSet.remove(nazwa.lowercased())
  122.     } else {
  123.         print("Zbior nie zawiera takiego elementu.")
  124.     }
  125.     print(carsSet);
  126. }
  127.  
  128. func student() {
  129.     var student: [Int:String] = [:];
  130.     var nazwisko: String;
  131.     for _ in 0...4 {
  132.         print("Podaj ID studenta: ");
  133.         guard let id = Int(readLine()!) else {
  134.             fatalError("Nie liczba");
  135.         }
  136.         print("Podaj nazwisko studenta: ");
  137.         nazwisko = readLine()!;
  138.         student[id] = nazwisko;
  139.     }
  140.     print(student);
  141.     print("Podaj nazwisko: ")
  142.     nazwisko = readLine()!;
  143.     for i in student {
  144.         if (i.value == nazwisko) {
  145.             print(i)
  146.         }
  147.     }
  148.     for i in student {
  149.         if (i.value == nazwisko) {
  150.             student.remove(at: student.index(forKey: i.key)!);
  151.             nazwisko = "";
  152.         }
  153.     }
  154.     print(student);
  155. }
  156.  
  157. //rzutOszczepemFunc()
  158. //cars()
  159. student()
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement