0% found this document useful (0 votes)
64 views

Algorithm Infixtopostfix

The document describes algorithms for converting an infix expression to postfix notation and evaluating a postfix expression. It contains pseudocode for an infixtopostfix algorithm that uses a stack to convert an infix string to postfix by pushing and popping operators. It also includes an EvaluationForPostfix algorithm that uses a stack to evaluate a postfix expression by popping operands, performing operations, and pushing results.

Uploaded by

sami.yaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Algorithm Infixtopostfix

The document describes algorithms for converting an infix expression to postfix notation and evaluating a postfix expression. It contains pseudocode for an infixtopostfix algorithm that uses a stack to convert an infix string to postfix by pushing and popping operators. It also includes an EvaluationForPostfix algorithm that uses a stack to evaluate a postfix expression by popping operands, performing operations, and pushing results.

Uploaded by

sami.yaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Algorithm infixtopostfix(infix<String>){

This programme promt user to inter an infix


expression and convert it to postfix
Pre infix must be initialzed
Post

infix converted to postfix

Return String verbal (postfix)


1-initialzed String postFix ="";
2-char pop_char;
3-loop(counter smaller than length of infix)
//Iterates characters in infix
1- char ch=postinfix.charAt(i );
Converting from string to char

//

2- if(ch>='0'&&ch<='9')
//Check if
the char is number add it to postfix
1- Add postFix +=Character.toString(ch);
3-if(char ='(' )
1-pushStack(char);
4- else if (char=')')
matching '('

// save '(' on stack


// pop stack until

1-Set pop_char = popStack();


2-Loop (popStack(pop_char not equal
character "(" )
1-Set postFix +=pop_char;
2-Set

pop_char = popStack();

End loop
5-else
1-if (top not Equal -1 And Resulte from
Funcation precedence ( Send arr[top], Send
character of Infix ) Equal True)
1- Set pop_char = popStack();
2-Loop (The Call Funcation precedence
( send pop_char, send character of Infix (ch))
Equal True)
1-Set

postFix +=pop_char;

2-

if (top Equal -1)


1- break;

3-

Set

pop_char = popStack();

End loop
3-

pushStack(ch);

6- else
1pushStack(ch); //Stack is empty or
the operator has precedence

7-Loop(top bigger than or Equal 0)


1-Set pop_char = popStack();
2- Set postFix+=pop_char;

End loop
Algorithm precedence
(OperatorOFtop<char>, OperatorOfInfix
<char >){
This programme check the priority for
operators((,+,-,*,/) and receive parameter
from infixtopostfix program.
1-The first parameter (OperatorOFtop)
come from Stack(array).
2-The second parameter (OperatorOfInfix)
come from an infix expression after
convert it to character
Pre (OperatorOFtop And OperatorOfInfix)
must be initialzed
Post

Nothing

Return Boolean value (True or Flase)


1- Initialize integer proOftop=0;
2- Initialize integer proOfchar=0;
3-Initialize char [] precedence1 =
{ '(','+','-','*','/'};
4- Initialize int[] precedence = { 0, 1, 1, 2, 2,
2 };
5- loop (counter smaller than 4)
1- if (precedence1[counter ]
==OperatorOFtop)
1-set proOftop=counter ;

2-

if (precedence1[counter ]==Operator)
1-set

proOfchar=i;

6if(precedence[proOftop]>=precedence[proOf
char])
7-

return true;
8- else return false;

Algorithm EvaluationForPostinfix
(Postinfix<String>){
This programme promt user to inter
Postinfix expression , calculate it and
receive parameter from main application
program.
Pre Postinfix must be initialzed
Post

postfix calculated to postfix

Return String verbal (result)


1- Initialize integer result=0;
//
Integr variable to store result after arithmic
between to prrand
2-loop(counter smaller than length of
Postinfix) //Iterates characters in postinfix
1- char ch=postinfix.charAt(i );
Converting from string to char

//

// System.out.println("ch"+ch);
2- if(ch>='0'&&ch<='9')
//Check if
the char is number push it to stack

1- pushStack((char)(int)(ch-'0')); //we need


for value of character from ASCII code so
that we subtract from 0
2- else
oprand

// If not the char is

1-Set integer oprator1=popStack(); // the


elemet in top of array store in oprator1
2- Set integer oprator2=popStack();
3-switch(ch)
// Here will
arithmaic Executaion for tow oprators when
match it with char
1- case '+':result=oprator1+oprator2;
//adding operation for tow number
1- pushStack( (char) result);
2-

break;

3-case '-':result=oprator2-oprator1;
//subtract operation for tow number
1- pushStack( (char) result);
2- break;
1-

case
'*':result=oprator1*oprator2;//multiplicati
onoperation for tow number
1- pushStack( (char) result);
2- break;

1-

case
'/':result=oprator2/oprator1; ;//dividing
operation for tow number

1- pushStack( (char) result);


2-

break;

default:result=0;
End loop

result=popStack();

return result;
Evaluation

//Return

integer type the

Add pop_char to Stack


End loop
Return Stack
End infixtopostfix

You might also like