Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #define MAX_SIZE 20
- int stack[MAX_SIZE];
- int top = -1;
- int isEmpty() {
- return top == -1;
- }
- void push(int item) {
- if(top == MAX_SIZE-1) {
- printf("Error: Stack Overflow\n");
- return;
- }
- stack[++top] = item;
- }
- char pop() {
- if(isEmpty()) {
- printf("Error: Stack is Empty\n");
- return '$';
- }
- return stack[top--];
- }
- int precedence(char symbol) {
- if(symbol == '(') {
- return 0;
- } else if(symbol == '+' || symbol == '-') {
- return 1;
- } else if(symbol == '*' || symbol == '/') {
- return 2;
- } else if(symbol == '^') {
- return 3;
- }
- }
- int main() {
- char infix[20], x, temp;
- int i = 0;
- printf("Enter Infix Expression :\n");
- scanf("%s", infix);
- printf("Expression in Postfix Notation :\n");
- while(infix[i] != '\0') {
- x = infix[i];
- if(x == '(') {
- push(x);
- } else if(isalnum(x)) {
- printf("%c", x);
- } else if(x == ')') {
- while((temp = pop()) != '(') {
- printf("%c", temp);
- }
- } else {
- if(!isEmpty()) {
- while(precedence(stack[top]) >= precedence(x)) {
- printf("%c", pop());
- }
- }
- push(x);
- }
- i++;
- }
- while(!isEmpty()) {
- printf("%c", pop());
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement