포스트

1주차 과제

1주차 과제가 주어졌다.

과제는 다음과 같다.

1. Lv1

물론 Lv1 ~ Lv4까지 있지만.

Step by Step으로 하나씩 해보려고 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Calculator {
    
    func addOperation (_ x: Int, _ y: Int) -> Int {
        print(x+y)
        return x+y
    }
    
    func substractOperation (_ x: Int, _ y: Int) -> Int {
        print(x-y)
        return x-y
    }
    
    func multiOperation (_ x: Int, _ y: Int) -> Int {
        print(x*y)
        return x*y
    }
    
    func divideOperation (_ x: Int, _ y: Int) -> Int {
        print(x-y)
        return x/y
    }
    
}

일단은 이런식으로 class안에 function을 사용하여 구현하였다.

일단은 두수가 Int일때만을 고려하여 계산하였다.

2. Lv2

나머지를 구하게하는 기능을 추가 해보자.

1
2
3
4
func modOperation (_ x: Int, _ y: Int) -> Int {
        print(x%y)
        return x%y
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Calculator {
    
    func addOperation (_ x: Int, _ y: Int) -> Int {
        print(x+y)
        return x+y
    }
    
    func substractOperation (_ x: Int, _ y: Int) -> Int {
        print(x-y)
        return x-y
    }
    
    func multiOperation (_ x: Int, _ y: Int) -> Int {
        print(x*y)
        return x*y
    }
    
    func divideOperation (_ x: Int, _ y: Int) -> Int {
        print(x-y)
        return x/y
    }
    
    func modOperation (_ x: Int, _ y: Int) -> Int {
        print(x%y)
        return x%y
    }
    
}

위의 코드를 추가하였고 결과는 다음과 같다.

3. Lv3

지금은 각각의 기능이 Class안에 함수로 구현이 되어있는데 이걸이제는 각각의 클래스로 나누어 표현을 해야한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Calculator {
   
}

class AddOperation  {
   
}

class SubstractOperation {
    
}

class MutiplyOperation {

}

class DivideOperation  {

}

class ModOperation {

}

우선 각 클래스들을 만들어 주었다.

여기서 부터 뭔가 고민이 많아졌다

calculator 클래스를 초기화 하면서 내가 원하는 숫자를 미리 calculator(no1 : Int, no2 : Int) 이런식으로 할 건지

calculator.~ 이런식으로 나아가서 해결할지. 생각이 많아졌다.

일단은 이렇게 구현했다

보완

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// MARK: - 계산기 본체
class Calculator {
    
    let add = AddOperation()
    let substract = SubstractOperation()
    let multiply = MutiplyOperation()
    let divide = DivideOperation()
    let mod = ModOperation()
}

// MARK: - 덧셈
class AddOperation  {
    func operation (first : Int, second : Int) {
        print(first + second)
    }
}

// MARK: - 뺄셈
class SubstractOperation {
    func operation (first : Int, second : Int) {
        print(first - second)
    }
}

// MARK: - 곱셈
class MutiplyOperation {
    func operation (first : Int, second : Int) {
        print(first * second)
    }
}

// MARK: - 나눗셈
class DivideOperation  {
    func operation (first : Int, second : Int) {
        print(first / second)
    }
}

// MARK: - 나머지
class ModOperation {
    func operation (first : Int, second : Int) {
        print(first % second)
    }
}

// MARK: - Test
let calculator = Calculator()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// MARK: - 계산기 본체
class Calculator {
    
    let add = AddOperation()
    let substract = SubstractOperation()
    let multiply = MutiplyOperation()
    let divide = DivideOperation()
    let mod = ModOperation()
}

// MARK: - 덧셈
class AddOperation  {
    func operation (first : Int, second : Int) {
        print(first + second)
    }
    func operation (first : Int, second : Double) {
        print(Double(first) + second)
    }
    func operation (first : Double, second : Int) {
        print(first + Double(second))
    }
    func operation (first : Double, second : Double) {
        print(first + second)
    }
}

// MARK: - 뺄셈
class SubstractOperation {
    func operation (first : Int, second : Int) {
        print(first - second)
    }
    func operation (first : Int, second : Double) {
        print(Double(first) - second)
    }
    func operation (first : Double, second : Int) {
        print(first - Double(second))
    }
    func operation (first : Double, second : Double) {
        print(first - second)
    }
}

// MARK: - 곱셈
class MutiplyOperation {
    func operation (first : Int, second : Int) {
        print(first * second)
    }
    func operation (first : Int, second : Double) {
        print(Double(first) * second)
    }
    func operation (first : Double, second : Int) {
        print(first * Double(second))
    }
    func operation (first : Double, second : Double) {
        print(first * second)
    }
}

// MARK: - 나눗셈
class DivideOperation  {
    func operation (first : Int, second : Int) {
        print(first / second)
    }
    func operation (first : Int, second : Double) {
        print(Double(first) / second)
    }
    func operation (first : Double, second : Int) {
        print(first / Double(second))
    }
    func operation (first : Double, second : Double) {
        print(first / second)
    }
}

// MARK: - 나머지
class ModOperation {
    func operation (first : Int, second : Int) {
        print(first % second)
    }
    func operation (first : Int, second : Double) {
        print(second.truncatingRemainder(dividingBy: Double(first)))
    }
    func operation (first : Double, second : Int) {
        print(first.truncatingRemainder(dividingBy: Double(second)))
    }
    func operation (first : Double, second : Double) {
        print(first.truncatingRemainder(dividingBy: second))
    }
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.