Пример #1
0
def evalInfixStack(expression):
    """ Evaluate a fully parenthesized infix expression using a stack.

    This function evaluates fully parenthesized  expressions involving
    single digit integers and the operators addition (+), subtraction (-), 
    multiplication (*), integer division (/), and modular division (%).
    
    A BadExpressionError will be raised if the expression contains unrecognized
    characters, or is not well formed.  Spaces and tab characters are ignored.
    
    
    Arguments: expression - a string containing a mathematical expression.
    
    Returns: an integer containing the value of the expression. 
    """ 

    tempchar1 = ""
    
    variables = Stack()
    
    tempchar2 = expression.split()
    characters = tempchar1.join(tempchar2)
    print characters

    for i in characters:

        if not realNum(i) and i not in "+-%*/()":
            
            raise BadExpressionError()
        else:
            
            if realNum(i) or i in "+-%*/":
                variables.push(i)

            if i is ')':
                var2 = variables.pop()
                op = variables.pop()
                var1 = variables.pop()
            
                value = evaluate(op, var1, var2)
                variables.push(value)

    
          
               
    return variables.peek()
            pop & append
         push onto Stack
    pop and append the rest of the Stack.
'''

from pyliststack import Stack

#input = "(((A+B)*(C-E))/(F+G))"
#input = "A*B+C/D"
#input = "A*(B+C)/D"
#input = "A*(B+C/D)"
#input = "((A * B) +(C / D))"
input = "A * (B + C) / D"
output = ""

stack = Stack()

def getpri(c):
    if c == '(' or c == ')':
        return 1
    if c == '+' or c == '-':
        return 2
    if c == '*' or c == '/':
        return 3
    return 0

print input

for ch in input:
    if ch >= 'A' and ch <= 'Z':
        output += ch
Пример #3
0
from pyliststack import Stack

PROMPT = "Enter and int value (<0 to ending:)"

myStack = Stack()
value = int(input(PROMPT))

while value >= 0:
    myStack.push(value)
    value = int(input(PROMPT))

while not myStack.isEmpty():
    value = myStack.pop()
    print(value)

# input 7 13 45 19 28 -1
Пример #4
0
 def peek(self,color=None):
     assert(not self.RBStack.isEmpty()),"Stack is Empty"
     if (color==None):
         return self.RBStack.peek().item
     tmpColor=color.lower()
     assert(tmpColor=='r' or tmpColor=='b' or tmpColor=='black' or tmpColor=='red'),"Invalid color"
     if(tmpColor=='r' or tmpColor=='red'):
         tmpStack=Stack()
         while(not self.RBStack.isEmpty()):
             tmpNode=self.RBStack.peek()
             if (tmpNode.color=='r' or tmpNode.color=='red'):
                 while(not tmpStack.isEmpty()):
                     self.RBStack.push(tmpStack.pop())
                 return tmpNode.item
                 break
             else:
                 tmpStack.push(self.RBStack.pop())
         while(not tmpStack.isEmpty()):
             self.RBStack.push(tmpStack.pop())
         assert(False),"Color item not found."
         
     else:
         tmpStack=Stack()
         while(not self.RBStack.isEmpty()):
             tmpNode=self.RBStack.peek()
             if (tmpNode.color=='b' or tmpNode.color=='black'):
                 while(not tmpStack.isEmpty()):
                     self.RBStack.push(tmpStack.pop())
                 return tmpNode.item
                 break
             else:
                 tmpStack.push(self.RBStack.pop())
         while(not tmpStack.isEmpty()):
             self.RBStack.push(tmpStack.pop())
         assert(False),"Color not found."
Пример #5
0
 def __init__(self):
     self.RBStack=Stack()
Пример #6
0
class RedBlackStack():
    
# Creates an empty red/black stack.

    def __init__(self):
        self.RBStack=Stack()
        
# Returns true if the red/black stack is empty of false otherwise.

    def isEmpty(self):
        return (self.RBStack.isEmpty())
    
# Returns the number of items in the stack.

    def __len__(self):
        return len(self.RBStack)
    
# Pushes a node that stores item and color onto top of stack.

    def push(self, item, color):
        tmpColor=color.lower()
        assert(tmpColor=='r' or tmpColor=='b' or tmpColor=='black' or tmpColor=='red'),"Invalid color"
        tmpNode=_Node(item,color)
        self.RBStack.push(tmpNode)
        
# Removes the last node placed on top of stack based on input color, and returns item. 
# If color is None, removes last node placed on stack and returns item. 

    def pop(self,color=None):
        assert(not self.RBStack.isEmpty()),"Stack is Empty"
        if (color==None):
            return self.RBStack.pop().item
        tmpColor=color.lower()
        assert(tmpColor=='r' or tmpColor=='b' or tmpColor=='black' or tmpColor=='red'),"Invalid color"
        if(tmpColor=='r' or tmpColor=='red'):
            tmpStack=Stack()
            while(not self.RBStack.isEmpty()):
                tmpNode=self.RBStack.pop()
                if (tmpNode.color=='r' or tmpNode.color=='red'):
                    while(not tmpStack.isEmpty()):
                        self.RBStack.push(tmpStack.pop())
                    return tmpNode.item
                    break
                else:
                    tmpStack.push(tmpNode)
            while(not tmpStack.isEmpty()):
                self.RBStack.push(tmpStack.pop())
            assert(False),"Color item not found."
            
        else:
            tmpStack=Stack()
            while(not self.RBStack.isEmpty()):
                tmpNode=self.RBStack.pop()
                if (tmpNode.color=='b' or tmpNode.color=='black'):
                    while(not tmpStack.isEmpty()):
                        self.RBStack.push(tmpStack.pop())
                    return tmpNode.item
                    break
                else:
                    tmpStack.push(tmpNode)
            while(not tmpStack.isEmpty()):
                self.RBStack.push(tmpStack.pop())
            assert(False),"Color not found."
            
# Returns last item on stack based on color input without removing respected node.
# If color is None, returns last item placed on stack.

    def peek(self,color=None):
        assert(not self.RBStack.isEmpty()),"Stack is Empty"
        if (color==None):
            return self.RBStack.peek().item
        tmpColor=color.lower()
        assert(tmpColor=='r' or tmpColor=='b' or tmpColor=='black' or tmpColor=='red'),"Invalid color"
        if(tmpColor=='r' or tmpColor=='red'):
            tmpStack=Stack()
            while(not self.RBStack.isEmpty()):
                tmpNode=self.RBStack.peek()
                if (tmpNode.color=='r' or tmpNode.color=='red'):
                    while(not tmpStack.isEmpty()):
                        self.RBStack.push(tmpStack.pop())
                    return tmpNode.item
                    break
                else:
                    tmpStack.push(self.RBStack.pop())
            while(not tmpStack.isEmpty()):
                self.RBStack.push(tmpStack.pop())
            assert(False),"Color item not found."
            
        else:
            tmpStack=Stack()
            while(not self.RBStack.isEmpty()):
                tmpNode=self.RBStack.peek()
                if (tmpNode.color=='b' or tmpNode.color=='black'):
                    while(not tmpStack.isEmpty()):
                        self.RBStack.push(tmpStack.pop())
                    return tmpNode.item
                    break
                else:
                    tmpStack.push(self.RBStack.pop())
            while(not tmpStack.isEmpty()):
                self.RBStack.push(tmpStack.pop())
            assert(False),"Color not found."