Esempio n. 1
0
def postfix(infix):
    s = stack()
    a = ''
    count = 0
    p = 0
    for i in infix:
        if i == '-':
            count += 1
            while not s.isEmpty():
                if s.peek() == '-' or s.peek() == '(':
                    break
                else:
                    a += s.pop()
            s.push('-')
        elif i == '+':
            count += 1
            while not s.isEmpty():
                if s.peek() == '+' or s.peek() == '-' or s.peek() == '(':
                    break
                else:
                    a += s.pop()
            s.push('+')
        elif i == '/':
            count += 1
            while not s.isEmpty():
                if s.peek() == '/' or s.peek() == '+' or s.peek(
                ) == '-' or s.peek() == '(':
                    break
                else:
                    a += s.pop()
            s.push('/')
        elif i == '*':
            count += 1
            if not s.isEmpty():
                while not s.isEmpty():
                    if s.peek() == '*' or s.peek() == '/' or s.peek(
                    ) == '+' or s.peek() == '-' or s.peek() == '(':
                        break
                    a += s.pop()
            s.push('*')
        elif i == '(':
            s.push(i)
        elif i == ')':
            while s.peek() != '(' and not s.isEmpty():
                a += s.pop()
            s.pop()
        else:
            if i != ' ':
                a += i

    while (not s.isEmpty()):
        a += s.pop()
    for x in a:
        if x >= 'a' and x <= 'z':
            p += 1

    print('Result postfix expression : ', a)
    print('Number of operation       : ', count)
    print('Number of operand         : ', p)
Esempio n. 2
0
def Postfix(word):
    p = stack()
    for x in word:
        if x >= '0' and x <= '9':
            p.push(ord(x) - 48)
        elif x == '+':
            num1 = p.pop()
            num2 = p.pop()
            sum1 = num2 + num1
            p.push(sum1)
        elif x == '*':
            num1 = p.pop()
            num2 = p.pop()
            sum1 = num2 * num1
            p.push(sum1)
        elif x == '-':
            num1 = p.pop()
            num2 = p.pop()
            sum1 = num2 - num1
            p.push(sum1)
    print(p.items)
Esempio n. 3
0
 def depart(self,i):
     if i in self.car.items:
         temp=stack()
         print('car ',i,'depart:')
         for x in range(self.car.size(),-1,-1):
             if x!=i:
                 t=self.car.pop()
                 print('\tpop ',x,end='')
                 temp.push(t)
             else:
                 print('\tpop ',x,end='')
                 self.car.pop()
                 break
         
         space=4-self.car.size()
         for x in range(temp.size()-1,-1,-1):
             print('\tpush',temp.items[x],end='')
             self.car.push(temp.items[x])
         print('\n\tspace left ',space)   
         print('Soi = ',self.car.items)
        
     else:
         print('car',i,'cannot depart : No car',i)
Esempio n. 4
0
from Class_Stack import stack

def match(pp,cc):
    if (pp=='{'and cc=='}')or(pp=='('and cc==')')or(pp=='['and cc==']'):
        return True
    else:
        return False

st1='( a+b-c *[d+e]/{f*(g+h) }'
st2='[ ( a+b-c }*[d+e]/{f*(g+h) }'
st3='( 3 + 2 ) / { 4**5 }'
st4='456'
s=stack()
error= False
for x in st3:
    if x=='{'or x=='[' or x=='(':
        s.push(x)
    elif x=='}'or x==']' or x==')':
        if  s.isEmpty()==True:
            error=True
        else:
            open=s.pop()
            if match(open,x)==False:
                error=True
                
if error:
    print('MISMATH')
elif s.isEmpty()==False:
    print('MISMATCH open paren. exceed')
else :
    print('MATCh')
Esempio n. 5
0
 def __init__(self):
      self.car=stack()