Infix to Postfix 1

Description:              This program performs the conversion of infix expression into postfix expression.

 
Primary Inputs:        infix expression (with or without parentheses)
Primary Output:       corresponding postfix expression
Platform Used:          Turbo C++ version 3.0, Borland International Inc.
 

/*program performs the infix to postfix conversion*/

#include
#include
#define max 30

/*prototype declaration of a fuction*/

void pre(char x);

struct stack
{
char arr1[max],arr2[max];
}s;

int top1=0;
int top2=-1;

void push(char x)
{
if((x=='+')||(x=='-')||(x=='*')||(x=='/')||(x=='^')||(x=='(')||(x==')'))
pre(x);
else
{
top2++;
s.arr2[top2]=x;
}
}

void pre(char x)
{
if((s.arr1[top1]=='(')||(x=='('))
{
s.arr1[++top1]=x;
}
else if(x==')')
{
while(s.arr1[top1]!='(')
{
s.arr2[++top2]=s.arr1[top1--];
}
top1--;
}
else if((x=='+')||(x=='-'))
{
while(s.arr1[top1]!='(')
s.arr2[++top2]=s.arr1[top1--];

s.arr1[++top1]=x;
}
else if((x=='*')||(x=='/'))
{
while((s.arr1[top1]!='+')&&(s.arr1[top1]!='-')&&(s.arr1[top1]!='('))
s.arr2[++top2]=s.arr1[top1--];

s.arr1[++top1]=x;
}
else if(x=='^')
{
if(s.arr1[top1]=='^')
s.arr2[++top2]=s.arr1[top1--];

s.arr1[++top1]=x;
}
}

void main()
{
char exp[20];
int i=0;
clrscr();

printf("enter the exp. to be convert into postfix\n");

while((exp[i++]=getchar())!='\n');
exp[--i]=')';

exp[++i]='\0';
s.arr1[top1]='(';

for(i=0;exp[i]!='\0';i++)
{
push(exp[i]);
}
printf("\n%s",s.arr2);
getch();
}

 

Infix to Postfix
Rate this post

One comment on “Infix to Postfix

  1. Reply how to block porn May 20,2013 8:38 pm

    I think this site has some real great information for everyone :D.

Leave a Reply