-
Notifications
You must be signed in to change notification settings - Fork 0
Homework 3 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Homework 3 #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // | ||
| // main.swift | ||
| // lesson2 | ||
| // | ||
| // Created by Vladislav Elkin on 15.07.2020. | ||
| // Copyright © 2020 Vladislav Elkin. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| //1.Написать функцию, которая определяет, четное число или нет. | ||
|
|
||
| func isEven(_ number: Int) -> Bool { | ||
| return number % 2 == 0 ? true : false; | ||
| } | ||
|
|
||
| print(isEven(10)); | ||
| print(isEven(5)); | ||
| print("\r\n===============================\r\n") | ||
|
|
||
| //2. Написать функцию, которая определяет, делится ли число без остатка на 3. | ||
|
|
||
| func function2(_ number: Int) -> Bool{ | ||
| return number % 3 == 0 ? true : false; | ||
| } | ||
|
|
||
| print(function2(9)); | ||
| print(function2(7)); | ||
| print("\r\n===============================\r\n") | ||
|
|
||
| //3. Создать возрастающий массив из 100 чисел. | ||
|
|
||
| var tArray: [Int] = []; | ||
| for i in 0...100 { | ||
| tArray.append(i) | ||
| } | ||
|
|
||
| print(tArray) | ||
| print("\r\n===============================\r\n") | ||
|
|
||
| //4. Удалить из этого массива все четные числа и все числа, которые не делятся на 3. | ||
|
|
||
| for(index, value) in tArray.enumerated(){ | ||
| if isEven(value) || !function2(value) { | ||
| tArray.remove(at: tArray.firstIndex(of: value)!); | ||
| } | ||
| } | ||
|
|
||
| print(tArray) | ||
|
|
||
| print("\r\n===============================\r\n") | ||
|
|
||
| //5. * Написать функцию, которая добавляет в массив новое число Фибоначчи, и добавить при помощи нее 100 элементов. | ||
|
|
||
| func fibonacci(_ n: Int64) -> Int64{ | ||
| if n == 1 || n == 2 { | ||
| return 1; | ||
| } | ||
|
|
||
| return fibonacci(n - 1) + fibonacci(n - 2); | ||
| } | ||
|
|
||
| var fibonacciArray: [Int64] = []; | ||
|
|
||
| for i in 1...101{ | ||
| fibonacciArray.append(fibonacci(Int64(i))); | ||
| } | ||
|
|
||
| print(fibonacciArray); | ||
|
|
||
| print("\r\n===============================\r\n") | ||
|
|
||
| /* | ||
| 6. * Заполнить массив из 100 элементов различными простыми числами. Натуральное число, большее единицы, называется простым, если оно делится только на себя и на единицу. Для нахождения всех простых чисел не больше заданного числа n, следуя методу Эратосфена, нужно выполнить следующие шаги: | ||
| */ | ||
|
|
||
| func isPrime (_ number: Int64) -> Bool { | ||
| if number < 2 { | ||
| return false; | ||
| } | ||
| for i in 2..<number { | ||
| if number % i == 0 { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
| func getPrimeArray (_ count: Int64) -> [Int64] { | ||
| var resultArray: [Int64] = []; | ||
| var i = 2 | ||
| while resultArray.count < count { | ||
| if isPrime(Int64(i)) { | ||
| resultArray.append(Int64(i)); | ||
| } | ||
| i += 1; | ||
| } | ||
|
|
||
| return resultArray; | ||
| } | ||
| print (getPrimeArray(100)); | ||
|
|
||
| print("\r\n===============================\r\n") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // | ||
| // main.swift | ||
| // lesson3 | ||
| // | ||
| // Created by Vladislav Elkin on 21.07.2020. | ||
| // Copyright © 2020 Vladislav Elkin. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| enum DoorState{ | ||
| case open, close; | ||
| } | ||
|
|
||
| enum EngineState{ | ||
| case start, stop; | ||
| } | ||
|
|
||
| enum VolumeState{ | ||
| case full, empty; | ||
| } | ||
|
|
||
| enum VolumeAction { | ||
| case add, get; | ||
| } | ||
|
|
||
| struct sportCar { | ||
| let brand: String; | ||
| let year: Int; | ||
| let trunkVolume: Double; | ||
| var engineState: EngineState; | ||
| var doorState: DoorState; | ||
| var volumeState: VolumeState; | ||
|
|
||
| mutating func changeDoorState (state: DoorState){ | ||
| self.doorState = state; | ||
| } | ||
|
|
||
| mutating func changeEngineState(state: EngineState){ | ||
| self.engineState = state; | ||
| } | ||
|
|
||
| mutating func changeVolume(action: VolumeAction, count: Double){ | ||
| switch action { | ||
| case .add: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно так |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. пустая строка |
||
| if volumeState == .full { | ||
| print("Trunk is full"); | ||
| } else { | ||
| if count >= trunkVolume { | ||
| volumeState = .full; | ||
| print("All added, Trunk is full"); | ||
| }else { | ||
| print("Added: \(count)"); | ||
| } | ||
| } | ||
|
|
||
| break; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в Swift |
||
|
|
||
| case .get: | ||
|
|
||
| if volumeState == .empty { | ||
| print("Trunk is empty"); | ||
| } else { | ||
|
|
||
| if count >= trunkVolume { | ||
| volumeState = .empty; | ||
| print("Trunk is empty"); | ||
| }else { | ||
| print("Get: \(count)"); | ||
| } | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| struct truckCar { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. с БольшойБуквы |
||
| let brand: String; | ||
| let year: Int; | ||
| let trunkVolume: Double; | ||
| var engineState: EngineState; | ||
| var doorState: DoorState; | ||
| var volumeState: VolumeState; | ||
|
|
||
| mutating func changeDoorState (state: DoorState){ | ||
| self.doorState = state; | ||
| } | ||
|
|
||
| mutating func changeEngineState(state: EngineState){ | ||
| self.engineState = state; | ||
| } | ||
|
|
||
| mutating func changeVolume(action: VolumeAction, count: Double){ | ||
| switch action { | ||
| case .add: | ||
|
|
||
| if volumeState == .full { | ||
| print("Trunk is full"); | ||
| } else { | ||
| if count >= trunkVolume { | ||
| volumeState = .full; | ||
| print("All added, Trunk is full"); | ||
| }else { | ||
| print("Added: \(count)"); | ||
| } | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| case .get: | ||
|
|
||
| if volumeState == .empty { | ||
| print("Trunk is empty"); | ||
| } else { | ||
|
|
||
| if count >= trunkVolume { | ||
| volumeState = .empty; | ||
| print("Trunk is empty"); | ||
| }else { | ||
| print("Get: \(count)"); | ||
| } | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var mersedes = sportCar(brand: "mersedes", year: 2010, trunkVolume: 200, engineState: .stop, doorState: .close, volumeState: .empty); | ||
|
|
||
| mersedes.changeDoorState(state: .open); | ||
| mersedes.changeVolume(action: .add, count: 200); | ||
|
|
||
| print(mersedes); | ||
|
|
||
|
|
||
| var scania = truckCar(brand: "Scania", year: 2009, trunkVolume: 1000, engineState: .stop, doorState: .open, volumeState: .full); | ||
|
|
||
| scania.changeDoorState(state: .close); | ||
| scania.changeEngineState(state: .start); | ||
|
|
||
| print(scania); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // | ||
| // main.swift | ||
| // test1 | ||
| // | ||
| // Created by Vladislav Elkin on 13.07.2020. | ||
| // Copyright © 2020 Vladislav Elkin. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| //1. Решить квадратное уравнение. | ||
|
|
||
| let a:Double = 1; | ||
| let b:Double = -8; | ||
| let c:Double = 15; | ||
| let discriminant = (b * b) - (4 * a * c); | ||
|
|
||
| if discriminant < 0 { | ||
| print("Корней нет!"); | ||
| }else if (discriminant == 0){ | ||
| let x = (-b + sqrt(discriminant)) / (2 * a); | ||
| print("x = \(x)"); | ||
| }else{ | ||
| let x1 = (-b + sqrt(discriminant)) / (2 * a); | ||
| let x2 = (-b - sqrt(discriminant)) / (2 * a); | ||
|
|
||
| print("x1 = \(x1), x2 = \(x2)"); | ||
| } | ||
|
|
||
|
|
||
| //2. Даны катеты прямоугольного треугольника. Найти площадь, периметр и гипотенузу треугольника. | ||
|
|
||
| let catA: Double = 5; | ||
| let catB: Double = 6; | ||
|
|
||
| let gipC = sqrt((catA * catA) + (catB * catB)); | ||
| let S = 0.5 * catB * catA; | ||
| let P = catB + catA + gipC; | ||
|
|
||
| print("P = \(P), S = \(S), C = \(gipC)") | ||
|
|
||
| //3. *Пользователь вводит сумму вклада в банк и годовой процент. Найти сумму вклада через 5 лет. | ||
|
|
||
| var deposit: Double = 1000; | ||
| var years: Double = 5; | ||
| let procent: Double = 7; | ||
|
|
||
| while years > 0 { | ||
| deposit += deposit * procent / 100; | ||
| years-=1; | ||
| } | ||
|
|
||
| print(deposit); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
с большой буквы
SportCar