Click here to Skip to main content
Click here to Skip to main content

Create Postfix from infix experssion.

By , 19 Aug 2006
 

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

}

 

}

  

 

 

 

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ali_reza_zareian
Software Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
Where is the truth?

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberMohamad K Ayash3 Oct '09 - 11:30 
Very Messy.
Lots of typos.
Weak content for a very classical problem in a 2nd course in programming.
GeneralIncorrect Solutionmembernaeem_libra8 Dec '08 - 19:50 
the code is not producing correct result. Precedence of the operator has not been maintained. e.g. 9+6/3-2+7 should yield 963/+2-7+ but it is producing something else..Try to fix out the problem.
GeneralRe: Incorrect Solutionmemberali_reza_zareian21 Jan '09 - 7:24 
With this algoritm you have to use Parentheses.
Generalhelp me i am not getting proper out put for infix to post evaln(main problem in evaluvation)memberkarthikragunath8712 Aug '08 - 19:05 
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
#define oper(x) (x=='+' || x=='-' || x=='*' || x=='/' || x=='~')
char expr[100],post[100],stack[100],a[100][100],b[100][100],c[100][100];
int input[100];
int top=-1,j=0,stk[10],max;
 
int precedence(char c)
{
if (c=='+' || c=='-')
return 1;
if (c=='*' || c=='/')
return 2;
if (c=='(')
return 3;
if(c=='~')
return 4;
return 0;
}
void push(char x)
{
stack[++top]=x;
}
int pop()
{
return stack[top--];
}
void main()
{
int i,length,length1,k=0,l=0,eval=0,counter=0,m=0,count;
char ch;
printf("Enter any expression\n\n");
scanf("%s",expr);
length=strlen(expr);


for(i=0;i<=length;i++)
{

if(oper(expr[i]))
{
post[j++]=' ';

while(precedence(expr[i])<precedence(stack[top]))
{
post[j++]=stack[top];

pop();
post[j++]=' ';

}
push(expr[i]);
}
else if(expr[i]=='\0')
{
while(top!=-1)
{
post[j++]=' ';

post[j++]=stack[top];

pop();
}
}
else
{
post[j++]=(expr[i]);
}

}
post[j]='\0';
printf("%s",post);
for(i=0;i<=length;i++)
{
if(oper(expr[i]))
{
continue;
}


else if(expr[i]=='\0')
{
break;
}
else
{

while(isalnum(expr[i]))
{
b[k][l]=expr[i];
c[k][l]=b[k][l];
l++;
i++;
}
b[k][l]='\0';
c[k][l]='\0';
k++;
l=0;
}

}

for(i=0;i<k;i++)
{

count=0;
for(j=0;j<i;j++)
{
if(strcmp(b[i],c[j])==0)
{
input[i]=input[j];

count++;
break;
}

}

if(count==0)
{
printf("\nEnter input for %s:",b[i]);
scanf("%d",&input[i]);
}
 
}

printf("\n");
printf("Input Table\n\n");
printf("Index\tValue\n\n");
for(i=0;i<k;i++)
{
count=0;
for(j=0;j<i;j++)
{
if(strcmp(b[i],c[j])==0)
{
count++;
break;

}
}
if(count==0)
{

printf("%s\t%d\n",b[i],input[i]);
}
 
}
top=-1;
for(i=0;i<k;i++)
{
stk[++top]=input[i];
}
length1=strlen(post);
 

for(i=0;i<length1;i++)
 
{

if(oper(post[i]))
{
ch=post[i];

switch(ch)
{
case '+':
eval=stk[top]+stk[top-1];


break;
 
case '*':
eval=stk[top]*stk[top-1];


break;
 
case '~':
eval=!(stk[top]);


break;
 
}
top=top-1;
stk[top]=eval;

}

 
}
printf("\nOutput:");
 

printf(" \n%d ", stk[top]);

 
getch();
}
GeneralBetter methodmemberPIEBALDconsult11 Jun '08 - 17:26 
May I suggest my article[^] as a better method.
GeneralWATCH OUT! This solution is wrong!!memberMember 451662817 Mar '08 - 13:14 
I am sorry, but your solution is not complete.
U havent solved the precedence logic of the operations!
 
following step is needed:
 
If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack. If top item has higher precedence over the scanned character Pop the stack else push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.
GeneralRe: WATCH OUT! This solution is wrong!!memberali_reza_zareian18 Mar '08 - 9:14 
Yes,u are right.
GeneralRe: WATCH OUT! This solution is wrong!!memberPKokkula27 Jul '09 - 2:16 
Ali,
 
This is an incorrect program. You should stop pop functionality when we come across single left Parenthesis.
Generalplz help mesussbehruoz guozali asl8 Dec '07 - 7:01 
???? ????? ???? ??????? ???????? ???? ?? ????? ????? ???? ?? ?? ????
????? ?????? ?????
infix to postfix
?? ????
c++
???????? ????
?? ????
Smile | :)
AnswerRe: plz help mememberali_reza_zareian21 Jan '09 - 7:07 
???????????

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 20 Aug 2006
Article Copyright 2006 by ali_reza_zareian
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid