65.9K
CodeProject is changing. Read more.
Home

Create Postfix from infix experssion.

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.67/5 (14 votes)

Aug 20, 2006

2 min read

viewsIcon

64810

downloadIcon

874

this article create postfix experssion from an infix experssion.

Introduction

Ali reza zareian.

This is a simple program that creates postfix expression from an infix expression. What is an infix expression?

 

 (a+b)/d is a simple one. The infix expression is regular for mathematic. it means when u want to calculate this u must

Add a with b at the first time then divided with d.

If  we change this expression to postfix. We have this one ab+d/.

What is the useful of postfix?

 At first we don't need parantes.Second we can write a program to calculate this expression easier than infix expression.

 

What is this program?

 This programe use a simple algoritm to change infix to postfix.Just sees the example

Before I should say I use stack in this program.

 

Infix Program =(a+b)/d 

 

 

Input     Stack        output

(             (

a            (                     a

+           (+                   a

b           (+                   ab

)                                  ab+

/            /                     ab+

d                                 ab+d

No         empty           ab+d/

 

  

Main method of this program!?

1)      private int isOperand(char  chrTemp);

2)      private int isOperator(char chrTemp);

3)      public string createPrifex();

 

 

1) isOperand check character if they are in this set.{'*','/','+','-','^','('}

      If true we add this character to stack.

 

2)isOperator check character if they are in this set.{'*','/','+','-','^'}

    This  method check the  stack  and  pop all the operator except '('.

 

3)createPrifix method create postfix from infix use isOperand and isOperator.

 

PesoCode

  

 For (the first infix character ) to  last character

{

   If  (is operand)   add to stack

   Elae

   If  (it is ')' )   pop  stack and send it to output

Else send other character to output

}

 

}