๐Ÿ“„ Python

11. ํด๋ž˜์Šค

sa1t 2022. 6. 9. 10:55
๋ฐ˜์‘ํ˜•
# ํด๋ž˜์Šค = ๊ฐ์ฒด ์ง€ํ–ฅ
# ํ‹€, ์„ค๊ณ„๋„, ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋†“๊ณ 
# ๋ณ€์ˆ˜๋ช…๋งŒ ๋ฐ”๊ฟ”์„œ ๋น„์Šทํ•œ ๋™์ž‘์„ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์‹คํ–‰ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค

result1 = 0
result2 = 0

def add1(num):       # ๋งค๊ฐœ๋ณ€์ˆ˜ num
    global result1
    result1 = result1 + num
    return result1

def add2(num):       # ๋งค๊ฐœ๋ณ€์ˆ˜ num
    global result2
    result2 = result2 + num
    return result2

print(add1(3))
print(add2(4))


##### ํด๋ž˜์Šค ์‚ฌ์šฉํ•  ๋•Œ
class Calculator :
    def __init__(self):     # self๋ฅผ init(์ดˆ๊ธฐํ™”) ์‹œํ‚จ๋‹ค
        self.result = 0     # ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์žฅ์น˜

    def add(self,num):  # ๋งค๊ฐœ๋ณ€์ˆ˜ num
        self.result += num
        return self.result

    def sub(self,num):
        self.result -= num
        return self.result


cal1 = Calculator()
cal2 = Calculator()

print(cal1.add(3))
print(cal1.add(4))
print(cal1.sub(4))

print(cal2.add(4))
print(cal2.add(7))
print(cal2.sub(7))


##### ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ pass => ์•„๋ฌด๊ตฌ๋ฌธ์„ ๋„ฃ์ง€ ์•Š์•„๋„ ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š์Œ
class Cookie :
    pass
a= Cookie()
b= Cookie()

### Quiz) ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ ์‚ฌ์น™์—ฐ์‚ฐ ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ
# class Cal :
# input ๊ฐ’ ์ž…๋ ฅ ๋ฐ›๊ธฐ
# if๋ฌธ ์‚ฌ์šฉ => +, -, * , /   //add(),sub(),mul(),div()   ๊ฐ์ฒด, ํ•จ์ˆ˜ ์‚ฌ์šฉ
while True:
    class Cal:
        def setdata(self,first,second,op):
            self.first=first
            self.op=op
            self.second=second

        first=int(input("์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : "))
        op=input("์—ฐ์‚ฐ์ž(+, -, *, / )๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ")
        second=int(input("๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : "))

        if op=="+":
            def add(self):
                result=self.first+self.second
                return result
            print("%d %s %d = %d " %(first,op,second,first+second))

        elif op=="-":
            def sub(self):
                result = self.first - self.second
                return result
            print("%d %s %d = %d " %(first,op,second,first-second))

        elif op=="*":
            def mul(self):
                result = self.first * self.second
                return result
            print("%d %s %d = %d " %(first,op,second,first*second))

        elif op=="/":
            def div(self):
                result = self.first / self.second
                return result
            print("%d %s %d = %d " %(first,op,second,first/second))

        else :
            print("์ž˜๋ชป ์ž…๋ ฅ ํ•˜์…จ์Šต๋‹ˆ๋‹ค")


## ํด๋ž˜์Šค ์ƒ์†
# ํด๋ž˜์Šค์—์„œ ํด๋ž˜์Šค๋กœ ์†์„ฑ์„ ๋ฌผ๋ ค ๋ฐ›๋Š”๊ฒƒ

## ์‚ฌ์น™์—ฐ์‚ฐ ๊ณ„์‚ฐ๊ธฐ
class Cal:
    def setdata(self,first,second):
        self.first=first
        self.second=second

    def add(self):
        result=self.first+self.second
        return result

    def sub(self):
        result=self.first-self.second
        return result
    def mul(self):
        result=self.first*self.second
        return result
    def div(self):
        result=self.first/self.second
        return result

a=Cal()
a.setdata(2,3)
print(a.add())

class Morecal(Cal):
    def pow(self):
        result=self.first ** self.second
        return result

class Safecal(Cal):
    def div(self):
        if self.second ==0:
            return 0
        else:
            result=self.first/self.second
            return result
d=Safecal()
d.setdata(5,2)
print(d.div())



b=Morecal()
b.setdata(4,2)
print(b.add())

๋ฐ˜์‘ํ˜•