Eng.Leithy
04-27-2010, 10:02 AM
Salam Alikom
http://www.dreamincode.net/forums/showtopic37428.htm
el link da mofeed awy leko hatla2o feeh ezay ne7awel mn el infix expression to (RPN) postfix expression
from 5+3*2
to: 532*+ ..........o
hatla2o fe methods keteer bs a7san tari2a testakhdmoha hia el stacks
ana 3ref tb3an en fe nas keter msh 3rfa y3ni eh stack bs da 7ga zy el arrays kda han2olha fel section el mogama3 isA
Algorithm infix2postfix(infix expression string, postfix expression string)
{
char *i,*p;
i = first element in infix expression
p = first element in postfix expression
while i is not null
{
while i is a space or tab
{
increment i to next character in infix expression;
}
if( i is an operand )
{
p = i;
increment i to next character in infix expression;
increment p to next character in postfix expression;
}
if( i is '(' )
{
stack_push(i);
increment i to next character in infix expression;
}
if( i is ')' )
{
while( element at top of stack != '(' )
{
p = element at top of stack;
increment p to next character in postfix expression;
}
increment i to next character in infix expression;
}
if( i is an operator )
{
if( stack is empty )
stack_push(i);
else
{
while priority(element at top of stack) >= priority(i)
{
p = element at top of stack;
increment p to next character in postfix expression;
}
stack_push(i);
}
increment i to next character in infix expression;
}
}
while stack is not empty
{
p = stack_pop()
increment p to next character in postfix expression;
}
p = '\0';
}
Alaa Leithy
http://www.dreamincode.net/forums/showtopic37428.htm
el link da mofeed awy leko hatla2o feeh ezay ne7awel mn el infix expression to (RPN) postfix expression
from 5+3*2
to: 532*+ ..........o
hatla2o fe methods keteer bs a7san tari2a testakhdmoha hia el stacks
ana 3ref tb3an en fe nas keter msh 3rfa y3ni eh stack bs da 7ga zy el arrays kda han2olha fel section el mogama3 isA
Algorithm infix2postfix(infix expression string, postfix expression string)
{
char *i,*p;
i = first element in infix expression
p = first element in postfix expression
while i is not null
{
while i is a space or tab
{
increment i to next character in infix expression;
}
if( i is an operand )
{
p = i;
increment i to next character in infix expression;
increment p to next character in postfix expression;
}
if( i is '(' )
{
stack_push(i);
increment i to next character in infix expression;
}
if( i is ')' )
{
while( element at top of stack != '(' )
{
p = element at top of stack;
increment p to next character in postfix expression;
}
increment i to next character in infix expression;
}
if( i is an operator )
{
if( stack is empty )
stack_push(i);
else
{
while priority(element at top of stack) >= priority(i)
{
p = element at top of stack;
increment p to next character in postfix expression;
}
stack_push(i);
}
increment i to next character in infix expression;
}
}
while stack is not empty
{
p = stack_pop()
increment p to next character in postfix expression;
}
p = '\0';
}
Alaa Leithy