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.
|
Program to convert infix expression into postfix expression.
Posted Date: 26 May 2008 Resource Type: Articles/Knowledge Sharing Category: Education
|
Posted By: Vidya Member Level: Diamond Rating: Points: 1
|
|
|
|
Program : Program to convert infix expression into postfix expression.
#include #include #include #include #include #define SIZE 20
typedef struct stack_t { int top; char item[SIZE]; }stack;
/********** Function Declaration begins **********/ void create (stack *S); void push(stack *, char ch[]); void pop(stack *); void infix_to_postfix(); /********** Function Declaration ends **********/
int m,l; char A[40],c;
void main() { clrscr(); printf(“\n\t Program to covert infix expression into postfix expression; “); printf(“\n\t Enter your expression & to quit enter fullstop(.) :”); while((c=getc(stdin))!=’\n’) { A[m]=c; m++; } l=m; infix_to_postfix(); getch(); }
/********** Creating an empty stack **********/ /********** Function Definition begins **********/ void create(stack *S) {
S->top = 0 ; } /********** Function Definition ends **********/
/********** Pushing an element in stack **********/ /********** Function Definition begins **********/ void push(stack *S, char A[]) {
if( S->top >= SIZE) { printf(“\nStack is full”); } else {
S->item[S->top] = A[m]; S->top = S->top+1; }
} /********** Function Definition ends **********/
/********** Popping an element from stack **********/ /********** Function Definition begins **********/ void pop(stack *S) {
if (S->top < 0) { printf(“\n Stack is empty”); } else { if(S->top >=0) { S->top = S->top-1; if(S->item[S->top]!=’(‘) printf(“%c”,S->item[S->top]); } }
} /********** Function Definition ends **********/
/********** Infix to Postfix conversion **********/ /********** Function Definition begins **********/ void infix_to_postfix() { stack S; create(&S); m=0; while(m { switch(A[m]) { case ‘+’ : case ‘-’ : while(S.item[S.top-1] ==’-’ || S.item[S.top-1] ==’+’ ||S.item[S.top-1] ==’*’ ||S.item[S.top-1] ==’/’ ||S.item[S.top-1] ==’^’ && S.item[S.top-1] !=’(‘) pop(&S); push(&S,A); ++m; break; case ‘/’ : case ‘*’ : while(S.item[S.top-1] ==’*’ ||S.item[S.top-1] ==’/’ ||S.item[S.top-1] ==’^’ && S.item[S.top-1] !=’(‘) pop(&S); push(&S,A); ++m; break; case ‘^’ : push(&S,A); ++m; break; case ‘(‘ : push(&S,A); ++m; break; case ‘)’ : while(S.item[S.top-1]!=’(‘) pop(&S); pop(&S); ++m; break; case ‘.’ : while (S.top >= 0) pop(&S); exit(0); default : if(isalpha(A[m])) { printf(“%c”,A[m]); ++m; break; } else { printf(“\n some error”); exit(0); }
}
} } /********** Function Definition ends **********/
? OUTPUT Program to covert infix expression into postfix expression; Enter your expression & to quit enter fullstop(.) :A+B/C-D. ABC/+D-
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
Watch TV Channels
|