Our Feeds

Sunday 20 July 2014

Ajith KP

In-fix to Post-fix using stack [C++]

          We have already discussed about stacks in previous post. There said stacks are used to evaluate mathematical statements. Now we can discuss how stacks are using evaluate mathematical statements.

          We are writing mathematical statements in in-fix form. An example is `A+B`. Here the operator `+` is embedded between two operands `A and B`. We know this format very well. But the computers are using other type of form called `post-fix` form. In this form the operator is fixed after the operands. The post-fix form of `A+B` is `AB+`. The advantage of using this format is that  computer can reduce complexity of  in parenthesis statements. The compiler pushes whole statement which is in post-fix into stack and pops single element from stack. If the popped element is operator then appropriate number of operands will popped out and do the operation. After finish the operation push the answer into stack. After whole elements is evaluated the answer will popped out to use later operations.


/*
    [][][]
    [][][]
    [][][]
    [][][]  TerminalCoders.Blogspot.Com
    [][][]
    [][][]
    [][][]
*/
 
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class stacks
{
    char stack[256];
    int topx;
    public:
    stacks()
    {
        topx = -1;
    }
    int empty()
    {
        if(topx==-1)
        {
            return 1;
        }
        return 0;
    }
    char top()
    {
        return(stack[topx]);
    }
    char pop()
    {
        return(stack[--topx]);
    }
    void push(char c)
    {
        stack[++topx] = c;
    }
};
int isOperator(char character)
{
    if (character == '+' || character == '-' || character == '*' || character == '/' || character=='^') {
        return 1;
    }    
    return 0;
}
int isOperand(char character)
{    
    if (isOperator(character) == 0 && character != '(' && character != ')') {
        return 1;
    }
    return 0;
}
int priority(char op) {
 switch(op)
 {
  case '+':
  case '-':
   return 1;
   break;
  case '*':
  case '/':
   return 2;
   break;
  case '^':
   return 3;
   break;
  return 0;
  break;
 }
}
int main()
{
    char post[256], input[256], *cptr, tmp[256];
    stacks stack;
    cout<<"Enter infix: ";
    strcpy(post, "");
    strcpy(tmp, "");
    cin>>input;
    cptr = input;
    while(*cptr!='\0')
    {
        if(isOperand(*cptr))
        {
            sprintf(tmp, "%c", *cptr);
            strcat(post, tmp);
        }
        else if(isOperator(*cptr))
        {
            while(stack.empty()==0 && stack.top()!='(' && priority(stack.top())>priority(*cptr))
            {
                sprintf(tmp, "%c", stack.top());
                strcat(post, tmp);
                stack.pop();
            }
            stack.push(*cptr);
        }
        else if(*cptr == '(')
        {
            stack.push(*cptr);
        }
        else if(*cptr==')')
        {
            while(stack.empty()==0)
            {
                if(stack.top()=='(')
                {
                    stack.pop();
                    break;
                }
                if(stack.top()==')')
                {
                    continue;
                }
                sprintf(tmp, "%c", stack.top());
                strcat(post, tmp);
                stack.pop();
            }
        }
        *cptr++;
    }
    while(stack.empty()==0)
    {
        sprintf(tmp, "%c", stack.top());
        strcat(post, tmp);
        stack.pop();
    }
    cout<<post<<endl;
    cin.get();
}