Our Feeds

Thursday 15 October 2015

AJITH KP

Program for evaluation of Pre-fix Expression in CPP

Hi GuyZ,
     This is another question which you may want to solve in you graduation level/post-graduation level `Data structures and Algorithms`. The solution is given bellow.


Source Code

/*
    [][][]
    [][][]
    [][][]
    [][][]  TerminalCoders.Blogspot.Com
    [][][]
    [][][]
    [][][]
*/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
struct symtable
{
    char var;
    int val;
};
class stack
{
    int arr[1024];
    int ptr, i;
public:
    stack()
    {
        ptr = 0;
    }
    int isEmpty()
    {
        if(ptr==0)return 1;
        return 0;
    }
    int top()
    {
        return arr[ptr-1];
    }
    void push(int c)
    {
        arr[ptr++] = c;
    }
    int pop()
    {
        return arr[--ptr];
    }
    void disp()
    {
        for(i=0;i<ptr;i++)cout<<arr[i]<<" ";
        cout<<endl;
    }
};
class prefix
{
    int i, symLen, op1, op2;
    long int res;
    char pre[1024];
    symtable sym[1024];
    stack stk;
public:
    prefix()
    {
        symLen = 0;
    }
    void reverse(char *str, int l)
    {
        for(i=0;i<l/2;i++)
        {
            char tmp = str[i];
            str[i] = str[l-i-1];
            str[l-i-1] = tmp;
        }
    }
    int isInSymtab(char ch, int len)
    {
        for(i=0;i<len;i++)
        {
            if(sym[i].var==ch)return 1;
        }
        return 0;
    }
    int getVal(char ch, int len)
    {
        for(i=0;i<len;i++)
        {
            if(sym[i].var==ch)return sym[i].val;
        }
    }
    int isOperator(char ch)
    {
        if(ch=='+' || ch=='-' || ch=='/' || ch=='*' || ch=='^')return 1;
        return 0;
    }
    void read()
    {
        cout<<"Enter expression: ";
        cin.getline(pre, 1024);
        reverse(pre, strlen(pre));
    }
    void eval()
    {
        int sum, p;
        char str[2], *ptr = pre;
        while(*ptr!='\0')
        {
            if(isupper(*ptr))
            {
                if(isInSymtab(*ptr, symLen))
                {
                    stk.push(getVal(*ptr, symLen));
                }
                else
                {
                    cout<<"Enter value for "<<*ptr<<": ";
                    cin>>i;
                    stk.push(i);
                    //cout<<stk.top()<<endl;
                    sym[symLen].var = *ptr;
                    sym[symLen].val=i;
                    symLen++;
                }
            }
            else if(isdigit(*ptr))
            {
                sprintf(str, "%c", *ptr);
                sum = atoi(str);
                p = 10;
                *ptr++;
                while(isdigit(*ptr))
                {
                    sprintf(str, "%c", *ptr);
                    sum+=(atoi(str)*p);
                    p*=10;
                    *ptr++;
                }
                *ptr--;
                stk.push(sum);
            }
            else if(isOperator(*ptr))
            {
                op1 = stk.pop();
                op2 = stk.pop();
                switch(*ptr)
                {
                    case '+':
                        res = op1+op2;
                        break;
                    case '-':
                        res = op1-op2;
                        break;
                    case '/':
                        res = op1/op2;
                        break;
                    case '*':
                        res = op1*op2;
                        break;
                    case '^':
                        res = pow(op1, op2);
                        break;
                }
                stk.push(res);
            }
            *ptr++;
        }
        int ans = stk.pop();
        cout<<"Result: "<<ans<<endl;
    }
};
int main()
{
    prefix pre;
    pre.read();
    pre.eval();
    return 0;
}