티스토리 뷰

5. 파이썬

[텐서플로] 계산기 예제

패스트코드블로그 2020. 5. 9. 14:10
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
import tensorflow as tf
class Calculator:
    num1 : int
    num2 : int
    opcode : str
 
    @property
    def num1(self) -> intreturn self._num1
    @num1.setter
    def num1(self, num1): self._num1 = num1
    @property
    def num2(self) -> intreturn self._num2
    @num2.setter
    def num2(self, num2): self._num2 = num2
    @property
    def opcode(self) -> intreturn self._opcode
    @opcode.setter
    def opcode(self, opcode): self._opcode = opcode
 
    def calcuate(self, num1, num2, opcode):
        entity = Calculator()
        entity.num1 = num1
        entity.num2 = num2
        entity.opcode = opcode
        service = Service(entity)
        if opcode == '+':
            result = service.add()
        if opcode == '-':
            result = service.subtract()
        if opcode == '*':
            result = service.multiply()
        if opcode == '/':
            result = service.divide()
        return result
 
class  Service:
    def __init__(self, payload):
        self._num1 = payload.num1
        self._num2 = payload.num2
 
    @tf.function
    def add(self):
        return tf.add(self._num1, self._num2)
 
    @tf.function
    def subtract(self):
        return tf.subtract(self._num1, self._num2)
 
    @tf.function
    def multiply(self):
        return tf.multiply(self._num1, self._num2)
 
    @tf.function
    def divide(self):
        return tf.divide(self._num1, self._num2)
 
if __name__ == '__main__':
    app = Calculator()
    num1 = int(input('첫번째 수 \n'))
    opcode = input('연산자 \n')
    num2 = int(input('두번째 수 \n'))
    result = app.calcuate(num1, num2, opcode)
    print(f'{num1} {opcode} {num2} = {result}')
cs

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함