CONVERSION_INFIX_TO_PREFIX

INFIX TO PREFIX CONVERSION

HOME

/*PROGRAM
FOR THE CONVERSTION FROM

THE INFIX EXPRESSION TO
PREFIX EXPRESSION..*/

#include<iostream.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
int
j,q;

char
a,b=NULL;

class
stack //CLASS
DECLARATION

{
private:
   
char data,*expr,R[22];

  
 stack *next;

public:
  
 

  
 void evaluate(char *);

  
 void push(char);

  
 void pushexp(int); //MEMBER
FUNCTIONS

   
char pop();

  
 void show(int);

  
 int prio(char);

}
*top ;

void
stack::evaluate(char *c)

{
expr=c;
  
 

 
for(int i=0,j=-1;expr[i]!=”;i++)

 
{

 if(
isdigit(expr[i])||isalpha(expr[i]) )

 {
 R[++j]=expr[i];

 }
  
 

 else
  
     

{
  
         

if(top!=NULL)
  
         

{
  
       
   

b=pop();
push(b);
  
       
   

}
  
         

int
n1=prio(b);

int
n2=prio(expr[i]);

if(n1<=n2)
 push(expr[i]);
else
 {
 R[++j]=pop();
 push(expr[i]);
 }
 }
 }
 while(top!=NULL)

 R[++j]=pop();
    
    

}
char
stack::pop()

{
  
 char x=NULL;

  
 stack *temp;

  
 temp=top;

  
 x=temp->data;

  
 if(top==NULL)

  
     return(‘0’);

  
 else

  
 {


  
 top=temp->next;

 
  
 }

  
 return(x);    

}
  
 

int
stack::prio(char y)

{
  
 if(y==’$’)

  
     return(3);

  
 if(y==’*’||y==’/’)

  
     return(2);

  
 if(y==’+’||y==’-‘)

  
 return(1);

  
 else

  
     return(0);

  
 

}

void
stack::push(char ch)

{
 
stack *n,*temp;

 
n=new(stack);

 
n->data=ch;

 
n->next=NULL;

 
if(top==NULL)

  
   top=n;

 
else

 
{

  
   temp=top;

  
   n->next=temp;

  
   top=n;

 
}

 

}
void
stack::show(int q)

{
   
for(int i=q-1;i>=0;i–)

  
 {

  
     cout<<R[i];

  
 }

}

void
main()

{
  
 char s[22];

  
 top=NULL;

  
 stack k;

cout<<“enter
the INFIX expresion\n”;

  
 cin.getline(s,22);

  
 int q=strlen(s);

  
 char *e;

  
 e=strrev(s);

  
 k.evaluate(e);

cout<<“THE
PREFIX EXPRESION IS –>”;

   
k.show(q);

}
//PROGRAM
ENDS

/*
OUTPUT:
enter
the INFIX expresion

A+B*C-D/E
THE
PREFIX EXPRESION IS –>-+A*BC/DE

Press
any key to continue

*/

HOME

Advertisements

1 Comment »

  1. 1
    vijji Says:

    hi bhanu
    your blog is GOOD
    i think still it is in development
    but upto now it is O.K.


RSS Feed for this entry

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: