Objectives of this lecture
Review of Postfix (and Infix notation)
Prefix |
Infix |
Postfix |
* 3 4 |
3 * 4 |
3 4 * |
+ 3 * 4 5 |
3 + 4 * 5 |
3 4 5 * + |
* + 3 4 5 |
(3 + 4) * 5 |
3 4 + 5 * |
/*
Evaluates postfix expression of digit operands
Pre: postfix
is a string containing valid postfix string
Uses: Stack functions
*/
int
Eval(char *postfix)
{
StackEntry op1, op2, result;
Stack stack;
int i=0;
int len=strlen(postfix); // must put string.h in common.h
CreateStack(&stack);
for (i=0; i<len; i++)
{ if (postfix[i] != ' ')
if (postfix[i] >= '0' &&
postfix[i] <= '9')
Push(postfix[i] - '0', &stack);
else
{
Pop(&op2, &stack);
Pop(&op1, &stack);
switch (postfix[i])
{ case '+' : Push(op1+op2,
&stack); break;
case '-' : Push(op1-op2, &stack); break;
case '*' : Push(op1*op2, &stack); break;
case '/' : Push(op1/op2, &stack); break;
default : Error("nError in input");
}
}
}
Pop(&result, &stack);
return result;
}
Infix to Postfix Conversion (non - parenthesized)
Example
1: infix = a + b * c
character |
postfix |
stack |
a |
a |
empty |
+ |
a |
+ |
b |
ab |
+ |
* |
ab |
+* |
c |
abc |
+* |
Example
2: infix = a * b + c
character |
postfix |
stack |
a |
a |
empty |
* |
a |
* |
b |
ab |
* |
+ |
ab* |
+ |
c |
ab*c |
+ |
Pop the remaining operators and append to
postfix string à ab*c+
/*
Converts non-parenthesized infix string to postfix
Pre:
the infix contains a valid infix string
Post:
the resulting postfix is returned
Uses:
ctostr, an_operator, Higher, StackTop and Stack operations
*/
char
*Convert (char *infix)
{
char *postfix, ch;
StackEntry op;
Stack stack;
int i;
int len=strlen(infix);
CreateStack(&stack); // initialize
stack to empty
postfix[0] = '\0'; // initialize postfix to empty
for (i=0; i<len; i++)
{ ch=infix[i];
if (ch != ' ') //skip spaces
if (!an_operator(ch))
strcat(postfix, ctostr(ch));
else
{
while ( (!StackEmpty(&stack)) &&
Higher(StackTop(&stack), ch))
{ Pop(&op,
&stack);
strcat(postfix, ctostr(op));
}
Push(ch, &stack);
}
}
while (! StackEmpty(&stack))
{
Pop(&op, &stack);
strcat(postfix, ctostr(op));
}
return postfix;
}
//returns
true if op1 is of higher or equal precedence than op2
Boolean
Higher(char op1, char op2)
{ int prec1, prec2; //precedence level of op1 and op2
if (op1=='+' || op1=='-')
prec1=2;
else
prec1=3;
if (op2=='+' || op2=='-')
prec2=2;
else
prec2=3;
return (prec1 >= prec2);
}
//returns true if a
given character is an operator (+, -, *, /)
Boolean
an_operator(char ch);
// converts a character
to string
char *ctostr(char ch);
//returns the top item
of the stack without deleting it from the stack
StackEntry StackTop(Stack
*s); //add this to the stack ADT.
General Case of Parenthesized Expressions
operator |
precedence |
‘(‘ as op1 |
0 |
‘)’ as op2 |
1 |
+, - (always) |
2 |
*, / (always) |
3 |
‘(‘ as op2 |
4 |
/* Converts a general parenthesized infix string to
postfix
Pre: the infix contains a valid infix string
Post: the resulting postfix is returned
Uses: ctostr, an_operator, Higher, StackTop and Stack
operations
*/
char *Convert (char *infix)
{char *postfix, ch;
StackEntry op;
Stack stack;
int i;
int len=strlen(infix);
CreateStack(&stack); // initialize stack to empty
postfix[0] =
'\0'; // initialize postfix to empty
for (i=0;
i<len; i++)
{
ch=infix[i];
if (ch != '
') //skip spaces
if
(!an_operator(ch))
strcat(postfix,
ctostr(ch));
else
{ while ( (!StackEmpty(&stack)) &&
Higher(StackTop(&stack), ch))
{ Pop(&op, &stack);
strcat(postfix, ctostr(op));
}
if (ch ==
‘)’)
pop(&op, &stack); //pop
the corresponding left parenthesis
else
Push(ch, &stack);
}
}
while (!
StackEmpty(&stack))
{ Pop(&op, &stack);
strcat(postfix, ctostr(op));
}
return postfix;
}
Exercise: : Modify the functions an_operator and Higher as described above and write a main program to test these function. (Note: Need to compile these using the project tool).