92 lines
		
	
	
		
			No EOL
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			No EOL
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include <stdio.h>
 | 
						|
#include <string.h>
 | 
						|
 | 
						|
#define MAX_SIZE 100
 | 
						|
 | 
						|
char stack[MAX_SIZE];
 | 
						|
int top = -1;
 | 
						|
 | 
						|
void push(char item)
 | 
						|
{
 | 
						|
    if (top >= MAX_SIZE - 1)
 | 
						|
    {
 | 
						|
        printf("Stack Overflow\n");
 | 
						|
        return;
 | 
						|
    }
 | 
						|
    stack[++top] = item;
 | 
						|
}
 | 
						|
 | 
						|
char pop()
 | 
						|
{
 | 
						|
    if (top < 0)
 | 
						|
    {
 | 
						|
        printf("Stack Underflow\n");
 | 
						|
        return -1;
 | 
						|
    }
 | 
						|
    return stack[top--];
 | 
						|
}
 | 
						|
 | 
						|
int precedence(char symbol)
 | 
						|
{
 | 
						|
    switch (symbol)
 | 
						|
    {
 | 
						|
    case '^':
 | 
						|
        return 3;
 | 
						|
    case '*':
 | 
						|
    case '/':
 | 
						|
        return 2;
 | 
						|
    case '+':
 | 
						|
    case '-':
 | 
						|
        return 1;
 | 
						|
    default:
 | 
						|
        return 0;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
void infixToPostfix(char infix[], char postfix[])
 | 
						|
{
 | 
						|
    int i, j = 0;
 | 
						|
    char symbol, next;
 | 
						|
    for (i = 0; i < strlen(infix); i++)
 | 
						|
    {
 | 
						|
        symbol = infix[i];
 | 
						|
        if ((symbol >= 'a' && symbol <= 'z') || (symbol >= 'A' && symbol <= 'Z') || (symbol >= '0' && symbol <= '9'))
 | 
						|
        {
 | 
						|
            postfix[j++] = symbol;
 | 
						|
        }
 | 
						|
        else if (symbol == '(')
 | 
						|
        {
 | 
						|
            push(symbol);
 | 
						|
        }
 | 
						|
        else if (symbol == ')')
 | 
						|
        {
 | 
						|
            while ((next = pop()) != '(')
 | 
						|
            {
 | 
						|
                postfix[j++] = next;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        else
 | 
						|
        {
 | 
						|
            while (top > -1 && precedence(stack[top]) >= precedence(symbol))
 | 
						|
            {
 | 
						|
                postfix[j++] = pop();
 | 
						|
            }
 | 
						|
            push(symbol);
 | 
						|
        }
 | 
						|
    }
 | 
						|
    while (top > -1)
 | 
						|
    {
 | 
						|
        postfix[j++] = pop();
 | 
						|
    }
 | 
						|
    postfix[j] = '\0';
 | 
						|
}
 | 
						|
 | 
						|
int main()
 | 
						|
{
 | 
						|
    char infix[MAX_SIZE], postfix[MAX_SIZE];
 | 
						|
    printf("Enter infix expression: ");
 | 
						|
    gets(infix);
 | 
						|
    infixToPostfix(infix, postfix);
 | 
						|
    printf("Postfix expression: %s\n", postfix);
 | 
						|
    return 0;
 | 
						|
} |