Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- char stack[100];
- int top = -1;
- void push(char element)
- {
- stack[++top] = element;
- }
- char pop()
- {
- return (stack[top--]);
- }
- int priority(char element)
- {
- if(element==-1)
- return 0;
- if(element=='(')
- return 1;
- if(element=='+' || element=='-')
- return 2;
- if(element== '*' || element == '/')
- return 3;
- }
- void main()
- {
- char infix[100], postfix[100], ch, element;
- int i = 0, j = 0;
- printf("Expression:\n");
- scanf("%s", infix);
- push(-1);
- while ((ch = infix[i++]) != '\0')
- {
- if (ch == '(')
- push(ch);
- else if (isalnum(ch))
- postfix[j++] = ch;
- else if (ch == ')')
- {
- while (stack[top] != '(')
- postfix[j++] = pop();
- }
- else
- {
- while (priority(stack[top]) >= priority(ch))
- postfix[j++] = pop();
- }
- }
- while (stack[top] != -1)
- postfix[j++] = pop();
- postfix[j] = '\0';
- printf("\nInfix: %s \nPostfix: %s\n", infix, postfix);
- getchar();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement