Friday 24 November 2017

C++ Program to Evaluate an Expression using Stacks


Code:

#include     iostream
#include     conio.h
#include     string.h
using namespace std;
struct node
{
    int data;
    node *next;
}*p = NULL, *top = NULL, *save = NULL, *ptr;
void push(int x)
{
    p = new node;
    p->data = x;
    p->next = NULL;
    if (top == NULL)
    {
        top = p;
    }
    else
    {
        save = top; 
        top = p;
        p->next = save;
    }
}
char pop()
{
    if (top == NULL)
    {
        cout<<"underflow!!";
    }
    else
    {
        ptr = top;
        top = top->next;
        return(ptr->data);
        delete ptr;
    }
}
int main()
{
    char x[30];
    int a, b;
    cout<<"enter the balanced expression\n";
    cin>>x;
    for (int i = 0; i < strlen(x); i++)
    {
        if (x[i] >= 48 && x[i] <= 57)
            push(x[i]-'0');
        else if (x[i] >= 42 && x[i] <= 47)
        {
            a=pop();
            b=pop();
            switch(x[i])
            {
            case '+':
                push(a+b);
                break;
            case '-':
                push(a-b);
                break;
            case '*':
                push(a*b);
                break;
            case '/':      
                push(a/b);
                break;
            }
        }
    }
    cout<<"ans is "<
    getch();
}


Output:

enter the balanced expression
567+8-/
ans is -1


More C++ Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...