Evaluate Postfix
Sketch of algorithm for evaluating a postfix string:
(1) Create stack s.
(2) For each token, x, in the postfix expression:
1 If x is T or F push it into the stack s.
2 Else if x is a unary operator
i If you do not have at least one operand in s, you should return
an error in the boolean (remember to free your data).
ii pop an operand, op1, from s
iii compute x op1 (see unary table)
iv push the result into s
v free op1 and x
3 Else if x is a binary operator
i If you do not have at least two operands in s, you should return
an error in the boolean (remember to free your data).
ii pop an operand, op2, from s
iii pop an operand, op1, from s
iv compute op1 op2 x (see binary table)
v push the result into s
vi free op1, op2, and x.
(3) If s contains more than one operand after all of the tokens are evaluated
you should return an error in the boolean (remember to free your data).
(4) Otherwise pop and return the only value in s.
(5) Be sure to free all remaining malloced data before leaving the function
(including when returning an error). Do not free the returned string.
Convert Postfix to Infix
Sketch of algorithm for converting a postfix strings to an infix strings:
(1) Create stack s.
(2) For each token, x, in the postfix expression:
1 If x is T or F push it into the stack s.
2 Else if x is a unary operator
i pop an operand, op1, from s
ii push the string “(x op1)” into s
iii free op1 and x
3 Else if x is a binary operator
i pop an operand, op2, from s
ii pop an operand, op1, from s
iii push the string “(op1 x op2)” into s
iv free op1, op2, and x
(3) You can assume that the postfix string is well formatted (feel free to
implement error checking if you would like).
(4) pop and return the value in s.
(5) Be sure to free all remaining malloced data before leaving the function