C Program to convert infix to postfix and evaluate postfix expression

By | 12.04.2017

Infix to postfix and evaluate postfix expression


Write a C Program to convert infix to postfix and evaluate postfix expression. Here’s simple Program to convert infix to postfix and evaluate postfix expression in C Programming Language.


What is Stack ?


Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack, the only element that can be removed is the element that was at the top of the stack, just like a pile of objects.


Basic Operations : :


  • push() − Pushing (storing) an element on the stack.
  • pop() − Removing (accessing) an element from the stack.
  • peek() − get the top data element of the stack, without removing it.
  • isFull() − check if stack is full.
  • isEmpty() − check if stack is empty.

Below is the source code for C Program to convert infix to postfix and evaluate postfix expression which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to convert infix to postfix and evaluate postfix expression  */

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

#define BLANK ' '
#define TAB '\t'
#define MAX 50

void push(long int symbol);
long int pop();
void infix_to_postfix();
long int eval_post();
int priority(char symbol);
int isEmpty();
int white_space(char );

char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;

int main()
{
        long int value;
        top=-1;
        printf("Enter infix : ");
        gets(infix);
        infix_to_postfix();
        printf("Postfix : %s\n",postfix);
        value=eval_post();
        printf("Value of expression : %ld\n",value);

        return 0;

}/*End of main()*/

void infix_to_postfix()
{
        unsigned int i,p=0;
        char next;
        char symbol;
        for(i=0;i<strlen(infix);i++)
        {
                symbol=infix[i];
                if(!white_space(symbol))
                {
                        switch(symbol)
                        {
                        case '(':
                                push(symbol);
                                break;
                        case ')':
                                while((next=pop())!='(')
                                        postfix[p++] = next;
                                break;
                        case '+':
                        case '-':
                        case '*':
                        case '/':
                        case '%':
                        case '^':
                                while( !isEmpty( ) &&  priority(stack[top])>= priority(symbol) )
                                        postfix[p++]=pop();
                                push(symbol);
                                break;
                        default: /*if an operand comes*/
                             postfix[p++]=symbol;
                        }
                }
        }
        while(!isEmpty( ))
                postfix[p++]=pop();
        postfix[p]='\0'; /*End postfix with'\0' to make it a string*/
}/*End of infix_to_postfix()*/

/*This function returns the priority of the operator*/
int priority(char symbol)
{
        switch(symbol)
        {
        case '(':
                return 0;
        case '+':
        case '-':
                return 1;
        case '*':
        case '/':
        case '%':
                return 2;
        case '^':
                return 3;
        default :
                return 0;
        }
}/*End of priority()*/

void push(long int symbol)
{
        if(top>MAX)
        {
                printf("Stack overflow\n");
                exit(1);
        }
        stack[++top]=symbol;
}/*End of push()*/

long int pop()
{
        if( isEmpty() )
        {
                printf("Stack underflow\n");
                exit(1);
        }
        return (stack[top--]);
}/*End of pop()*/
int isEmpty()
{
        if(top==-1)
                return 1;
        else
                return 0;
}/*End of isEmpty()*/

int white_space(char symbol)
{
        if( symbol == BLANK || symbol == TAB )
                return 1;
        else
                return 0;
}/*End of white_space()*/

long int eval_post()
{
        long int a,b,temp,result;
        unsigned int i;

        for(i=0;i<strlen(postfix);i++)
        {
                if(postfix[i]<='9' && postfix[i]>='0')
                        push(postfix[i]-'0');
                else
                {
                        a=pop();
                        b=pop();
                        switch(postfix[i])
                        {
                        case '+':
                                temp=b+a; break;
                        case '-':
                                temp=b-a;break;
                        case '*':
                                temp=b*a;break;
                        case '/':
                                temp=b/a;break;
                        case '%':
                                temp=b%a;break;
                        case '^':
                                temp=pow(b,a);
                        }
                        push(temp);
                }
        }
        result=pop();
        return result;
}/*End of eval_post */

OUTPUT : :


/*  C Program to convert infix to postfix and evaluate postfix expression  */

-----------------------------FIRST RUN--------------------------------------

Enter infix : (A+B)*(C+D)
Postfix : AB+CD+*
Stack underflow

Process returned 1

------------------------------SECOND RUN---------------------------------------

Enter infix : 7 8 + 3 2 + /
Postfix : 7832+/+
Value of expression : 8

Process returned 0

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4.4 16 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Adarsh

Above code is not satisfying for input consisting of more than two digits, like 10 + 6 * 2 is 22 but the output is 12.

Mayur

the code is not executing the console gets closed automatically. please solve the error