The script was coded with Python3
#1 PostfixToInfix(post_string, stack, n) Recursive function
INPUT
post_string: string, input postfix expression
stack: stack, stores and holds changing infix expression
n: index, counts up as we read in symbolds from post_string
OUTPUT
infix_string
The string is read from LEFT to right.
#2 InfixToPostfix(infix_string, stack, n, output) Recursive function: calls itself and isOperand() and isBalanced()
INPUT
infix_string: string, input infix expression
stack: stack, holds operators and delimiters
n: index for reading input infix_string
output: appends new sections as delimiters are matched
postfix expression
OUTPUT
string, postfix expression
CALLS
isOperand(): checks if sybmol is a letter
isBalanced(): checks if delimiters are balanced
This function is used as an intermediary function to rewrite from infix to
a different form. The function can handle delimiters ()/{}/[] and will check if they are balanced. After checking, it changes all open delimiters to ( and all closed delimiters to ).
#3 PrefixToInfix(strExpression, stack, n) Recursive function
INPUT
strExpression: string, input prefix expression
stack: stack, holds operators and delimiters
n: index for reading strExpression.
OUTPUT: string, postfix expression
The string is read RIGHT to left, n decrements until n=0
#4 PrefixToPostfix(strExpression)
Function: calls PrefixToInfix() and InfixToPostfix()
INPUT
strExpression: string, input prefix expression
OUTPUT
string, postfix expression
CALLS
PrefixToInfix() and InfixToPostfix()
The function translates prefix to infix, then infix to postfix.
#5 PostfixToPrefix(strExpression, stack,n)
Function: calls PostfixToInfix() and InfixToPrefix()
INPUT
strExpression: string, input prefix expression
stack: passes stack() to PostfixToInfix()
n: index
OUTPUT
string, prefix expression
CALLS
PrefixToInfix(), InfixToPostfix()
The function translates postfix to infix, then infix to prefix.
#5 Other functions
isOperand(char)
is uppercase letter or lowercase letter?
return boolean: TRUE=letter FALSE=not letter
isBalanced(string)
uses stacks() to read string and match delims
Stack class
Stack
LIFO list of elements. The stack is built by pushing to the top of the stack or
popping the top element out. There are additional methods to peek the top
element without popping, check if the stack is empty. Lastly, there inheritated
methods to get the type, len, str, and print.
stack = stack()
a = element
Inheritated methods
len(stack): number of elements in stack
type(stack): type = stack
print(stack): prints elements in stack
str(stack): string
New methods
stack.push(a)
add element <a> to top of stack
stack.pop()
remove top element
Error if stack is empty
stack.peek()
return top element
stack remains the same
Error if stack is empty
stack.isEmpty()
uses len(stack)
return boolean: TRUE=empty / FALSE=not empty