关于面向对象程序设计的习题

img


定义栈类,使用列表体初始化栈,实现入栈(增加一个元素到尾部)、出栈(删除最后一个元素并返回元素)3个方法。实例化一个栈,将1和2入栈,输出出栈结果值。

class Stack():
    def __init__(self):
        self.s = []
    
    def inStack(self,n):
        self.s.append(n)

    def outStack(self):
        return self.s.pop()

s = Stack()
s.inStack(1)
s.inStack(2)
out1 = s.outStack()
out2 = s.outStack()
print(out1,out2)

结果

1 2

class zhai():
    def __init__(self):
        self.a = []

    def inside(self,goin):
        self.a.append(goin)

    def outside(self):
        b = self.a[-1]
        del self.a[-1]
        return b
c = zhai()
c.inside(1)
c.inside(2)
print(c.outside())
print(c.a)
        

class Stack:
    def __init__(self):
        self.data = []

    def add(self, n):
        self.data.append(n)

    def pop(self):
        return self.data.pop()

s = Stack()

s.add(1)
s.add(2)

print(s.pop())
print(s.pop())

# coding=utf-8

class NoMaxSizeError(Exception):
    """
    Error:
    The stack hasn't got a max size,
    but user still tries to know whether the stack is full or not
    """

    def __init__(self, errorMessage):
        super(NoMaxSizeError, self).__init__(errorMessage)


class StackFullError(Exception):
    """
    Error:
    The stack has already been full,
    but user still tries to push a item into the stack
    """

    def __init__(self, errorMessage):
        super(StackFullError, self).__init__(errorMessage)


class StackEmptyError(Exception):
    """
    Error:
    The stack is empty,
    but user still tries to pop the top item out of the stack
    """

    def __init__(self, errorMessage):
        super(StackEmptyError, self).__init__(errorMessage)


class Stack(object):

    def __init__(self, initialStack: list = (), maxSize: int = -1):
        """
        Initial function
        :param initialStack: the initial stack
        :param maxSize: the stack's max size
        """

        self.stack = list(initialStack)
        self.maxSize = maxSize

    def __str__(self):
        """
        >>> print(Stack(initialStack=["a", "b", "c"]))
        top | c |
            | b |
            | a | bottom
        """

        return f"""top | {''' |
    | '''.join(self.stack[:: -1])} | bottom"""

    __repr__ = __str__

    def getSize(self) -> int:
        return len(self.stack)

    def isEmpty(self) -> bool:
        """
        Judge if the stack is empty
        :return: whether the stack is empty or not
        """

        return bool(self.stack)

    def isFull(self) -> bool:
        """
        Judge if the stack is full
        :return: whether the stack is full or not
        """

        if self.maxSize == -1:
            raise NoMaxSizeError("This stack hasn't got a max size! ")

        return self.getSize() == self.maxSize

    def getStack(self) -> list:
        """
        Get this stack
        """

        return self.stack

    def push(self, item: any):
        """
        Push an item to the top of the stack
        """

        if self.getSize() == self.maxSize:
            raise StackFullError("This stack has already been full! ")

        self.stack.append(item)

    def pop(self):
        """
        Pop the top item out of the stack
        """

        if self.isEmpty():
            raise StackEmptyError("This stack is empty! ")

        self.stack.pop()

这个是我之前写过的一个栈。
push是入栈,
pop是出栈。其他功能也在里边。