My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
Advertisements
|
convert postfix expression into prefix expression
Posted Date: 27 May 2008 Resource Type: Articles/Knowledge Sharing Category: Computer & Technology
|
Posted By: Vidya Member Level: Diamond Rating: Points: 1
|
|
|
|
Program : Program to convert postfix expression into prefix expression.
#include #include #include #define SIZE 30
typedef struct fix_tag { char s[SIZE][SIZE], dest[SIZE] ; char t1[2], t2[2] ; char ch1[SIZE], ch2[SIZE], ch3[SIZE] ; int i, top ; }postfix ;
/********** Function Declaration begins **********/ void initialization ( postfix * ) ; void expression ( postfix *, char * ) ; void push ( postfix *, char * ) ; void pop ( postfix *, char * ) ; void trans ( postfix * ) ; void display ( postfix ) ; /********** Function Declaration ends **********/
void main( ) { postfix PF ; char exp[SIZE] ;
clrscr( ) ;
initialization( &PF ) ;
printf (“\n\t\t Enter any postfix expression:” ) ; gets ( exp ) ;
expression ( &PF, exp ) ; trans ( &PF ) ;
printf ( “\n\t\tThe resultant Prefix expression is: “ ) ; display ( PF ) ;
getch( ) ; }
/********** Initialization of Structure variable **********/ /********** Function Definition begins **********/ void initialization ( postfix *p ) { p -> i = 0 ; p -> top = -1 ; strcpy ( p -> dest, “” ) ; } /********** Function Definition ends **********/
/********** String Copy **********/ /********** Function Definition begins **********/ void expression ( postfix *p, char *c ) { strcpy ( p -> dest, c ) ; } /********** Function Definition ends **********/
/********** Pushing expression **********/ /********** Function Definition begins **********/ void push ( postfix *p, char *str ) { if ( p -> top == SIZE - 1 ) printf ( “\nStack Overflow” ) ; else { p -> top++ ; strcpy ( p -> s[p -> top], str ) ; } } /********** Function Definition ends **********/
/********** Popping expression **********/ /********** Function Definition begins **********/ void pop ( postfix *p, char *a ) { if ( p -> top == -1 ) printf ( “\nStack is empty.” ) ; else { strcpy ( a, p -> s[p -> top] ) ; p -> top— ; } } /********** Function Definition ends **********/
/********** Postfix to Prefix **********/ /********** Function Definition begins **********/ void trans ( postfix *p ) { while ( p -> dest[p -> i] != ‘\0’ ) { if ( p -> dest[p -> i] == ‘ ‘) p -> i++ ; if( p -> dest[p -> i] == ‘%’ || p -> dest[p -> i] == ‘*’ || p -> dest[p -> i] == ‘-’ || p -> dest[p -> i] == ‘+’ || p -> dest[p -> i] == ‘/’ || p -> dest[p -> i] == ‘$’ ) { pop ( p, p -> ch2 ) ; pop ( p, p -> ch3 ) ; p -> t1[0] = p -> dest[ p -> i] ; p -> t1[1] = ‘\0’ ; strcpy ( p -> ch1, p -> t1 ) ; strcat ( p -> ch1, p -> ch3 ) ; strcat ( p -> ch1, p -> ch2 ) ; push ( p, p -> ch1 ) ; } else { p -> t1[0] = p -> dest[p -> i] ; p -> t1[1] = ‘\0’ ; strcpy ( p -> t2, p -> t1 ) ; push ( p, p -> t2 ) ; } p -> i++ ; } } /********** Function Definition ends **********/
/********** Displaying expression **********/ /********** Function Definition begins **********/ void display ( postfix p ) { char *t = p.s[0] ; while ( *t ) { printf ( “%c “, *t ) ; t++ ; } } /********** Function Definition ends **********/ ? OUTPUT Enter any postfix expression:AB+C/D- The resultant Prefix expression is: - / + A B C D
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
|