#include #include #include #include #include
int f(char); int r(char); void push(char); char pop(void);
char s[20]; int top=-1;
void main() { int rank=0,i=0,j=0; char next,c,infix[20],polish[20],temp; clrscr();
gets(infix); push('#'); strcpy(polish,NULL); next=infix[i];
while(next!='#') { while(f(next)<=f(s[top])) { temp=pop(); polish[j]=temp; j++; rank=rank+r(temp); if(rank<1) { printf(" I N V A L I D "); getch(); exit(0); } } push(next); i++; next=infix[i]; } while(s[top]!='#') { temp=pop(); polish[j]=temp; j++; rank=rank+r(temp); if(rank<1) { printf(" I N V A L I D "); getch(); exit(0); } } if(rank==1) { printf(" V A L I D "); puts(polish); //getch(); } else { printf("I N V A L I D"); exit(0); getch(); } getch(); }
// 4 f
int f(char c) { if(c=='+'|| c=='-') return (1); else if(c=='*'|| c=='/') return (2); else if(isalpha(c)) return (3); else if(c=='#') return (0); }
// 4 rank
int r(char c) { if(c=='+'||c=='-'||c=='*'||c=='/') return (-1); else return (1); }
// 4 push
void push(char c) {
top=top+1; s[top]=c; return(s[top]); }
// 4 pop char pop(void) { top=top-1; return(s[top+1]); }
|
| Author: m v lathkar | Member Level: Gold | Revenue Score:     |
The program uses an array s[20] as stack with top as push / pop point. However, the push / pop operation should be implemented by declaring
struct stack { char s[20]; int top; };
so that any direct manipulation of s[20] is avoided and the array as well as top element are accessed throuh struct stack variable
|