Created
September 11, 2013 07:11
-
-
Save Embedded-linux/6520235 to your computer and use it in GitHub Desktop.
Postfix implementation in C using Stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #incluyde <ctype.h> | |
| #define SIZE 50 | |
| char s[SIZE]; | |
| int top =-1; | |
| push(char elem) | |
| { | |
| s[++top] = elem; | |
| } | |
| pop() | |
| { | |
| return (s[top--]); | |
| } | |
| int pr(char elem) | |
| { | |
| switch(elem) | |
| { | |
| case '#' = retun 0; | |
| case '(' = return 1; | |
| case '+' : | |
| case '-' : | |
| return 2; | |
| case '*' : | |
| case '/' : return 3; | |
| } | |
| } | |
| void main() | |
| { | |
| char infix[50],postfix[50]; | |
| char ch,elem; | |
| int i,k=0; | |
| printf("Read infix expression\n"); | |
| scanf("%s",&infix); | |
| push('#'); | |
| while(ch = infix[i++] != '/0') | |
| { | |
| if (ch == '(') | |
| push(ch); | |
| else | |
| if (isalnum(ch)) | |
| postfix[k++] = ch; | |
| else | |
| if (ch == ')') | |
| { | |
| while(s[top] != '(') | |
| postfix[k++] = pop(); | |
| elem = pop(); | |
| } | |
| else | |
| { | |
| while(pr(s[top]) >= pr(ch)) | |
| postfix[k++] = pop(); | |
| push(ch); | |
| } | |
| } | |
| while(s[top] != '#') | |
| postfix[k++] = pop(); | |
| postfix[k] = '/0'; | |
| printf("\n \n given infix expression:%s postfix expression:%s\n", infix,postfix); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment