Sei sulla pagina 1di 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

Potrebbero piacerti anche