infix to postfix and prefix equation program in c programming
February 18, 2023

Infix to Postfix and Prefix Equation program in c

In this tutorial post we will write a Infix to postfix and prefix equation program in c programming language.

#define MAXSTK 10
#include<stdio.h>
#include<conio.h>
#include<string.h>

struct stack
{
    int top;
    char data[MAXSTK);
};

void push(struct stack *, chap);
char pop(struct stack *);
char peep(struct stack);
int isoperator(char);
int preced(char);
int intopost(char [], char []);
void intopre(char [],char []);

void main()
{

    char in[50],post[50],pre[50];
    clrscr();
    printf("Enter equation in infix form enclosed in brackets");
    gets(in);
    intopost(in, post);
    intopre(in,pre);
    printf("Eq. in post form is %s\nEq in pre form is %s",post,pre);
    getch();
}

void push(struct stack *p, char item)
{
    if(p->top==MAXSTK-1)
    {
        printf("Overflow");
        return;
    }

        p->top++;
        p->data[p->top] = item;

}

char pop(struct stack *p)
{
    char item;
    if(p->top == -1)
    {
        printf("Underflow");
        return(NULL);
    }
    item= p->data[p->top];
    p->top--;
    return(item);
}

char peep(struct stack s1)
{
    char item;
    if(s1.top == -1)
    {
        printf("Underflow");
        return(NULL);
    }
    item = s1.data[s1.top];
    return(item);
}

int isoperator(char ch)
{
    if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%')
        return(1);
    else
        return(0);
}

int preced(char op)
{
    switch(op)
    {
        case '+':
        case '-':
            return(1);
        case '*':
        case '/':
            return(2);
        default:
            return(0);
    }
}

void intopost(char in[], char post[])
{
    struct stack s1;
    int i,j;
    char ch;
    s1.top = -1;
    i = j = 0;
    while(in[i] != '\0')
    {
        if(in[i] == '(')
            push(&s1,in[i]);
        else if(in[i] == ')')
        {
            ch = pop(&s1);
            while(ch != '(')
            {
                post[j++] = ch;
                ch = pop(&s1);
            }
        }
        else if(isoperator(in[i]))
        {
            ch = peep(s1);
            while(ch != '(' && preced(ch) >= preced(in[i]))
            {
                post[j++] = pop(&s1);
                ch = peep(s1);
            }
        }
        else
            post[j++] = in[i];
        i++;
    }
    post[j] = '\0';
}

void intopre(char in[], char pre[])
{
    struct stack s1;
    int i,j;
    char ch;
    s1.top = -1;
    i = j = 0;

    while(in[i] != '\0')
        i++;
    i--;

    while(i >= 0)
    {
        if( in[i] == ')')
            push(&1, in[i]);
        else if(in[i] == '(')
        {
            ch = pop(&s1);
            while(ch != ')')
            {
                    pre[j++] = ch;
                    ch = pop(&s1);
            }
        }
        else if(isoperator(in[i]))
        {
            ch = peep(s1);
            while(ch != ')' && preced(ch) > preced(in[i]))
            {
                pre[j++] = pop(&s1);
                ch = peep(s1);
            }
            push(&s1, in[i]);
        }
        else
            pre[j++] = in[i];
        i--;
    }
    pre[j] = '\0';
    strev(pre);
}