Repository navigation
Expand file tree
/
Copy pathUASStrukdad.py
More file actions
57 lines (47 loc) · 1.63 KB
/
Copy pathUASStrukdad.py
File metadata and controls
57 lines (47 loc) · 1.63 KB
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
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
raise IndexError("Pop from an empty stack")
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
raise IndexError("Peek from an empty stack")
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
# Contoh penggunaan stack untuk konversi infix ke postfix
def infix_to_postfix(expression):
precedence = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3}
stack = Stack()
output = []
for char in expression:
if char.isalnum(): # Operand (alphanumeric characters)
output.append(char)
elif char == '(': # Left parenthesis
stack.push(char)
elif char == ')': # Right parenthesis
while not stack.is_empty() and stack.peek() != '(':
output.append(stack.pop())
stack.pop() # Pop the left parenthesis
else: # Operator
while (not stack.is_empty() and stack.peek() != '(' and
precedence[char] <= precedence[stack.peek()]):
output.append(stack.pop())
stack.push(char)
# Pop all the operators from the stack
while not stack.is_empty():
output.append(stack.pop())
return ''.join(output)
# Contoh penggunaan
expression = "a+b*(c^d-e)^(f+g*h)-i"
postfix = infix_to_postfix(expression)
print(f"Infix: {expression}")
print(f"Postfix: {postfix}")