C++ Program to Evaluate an Expression using Stacks

This C++ program, using a stack data strucure, computes value of postfix expression which pushes operands and pops these values on encountering an operator.

Here is the source code of the C++ program to display the value of the postfix expression given as input. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to Evaluate an Expression using Stacks
  3.  */
  4. #include <iostream>
  5. #include <conio.h>
  6. #include <string.h>
  7. using namespace std;
  8. struct node
  9. {
  10.     int data;
  11.     node *next;
  12. }*p = NULL, *top = NULL, *save = NULL, *ptr;
  13. void push(int x)
  14. {
  15.     p = new node;
  16.     p->data = x;
  17.     p->next = NULL;
  18.     if (top == NULL)
  19.     {
  20.         top = p;
  21.     }
  22.     else
  23.     {
  24.         save = top; 
  25.         top = p;
  26.         p->next = save;
  27.     }
  28. }
  29. char pop()
  30. {
  31.     if (top == NULL)
  32.     {
  33.         cout<<"underflow!!";
  34.     }
  35.     else
  36.     {
  37.         ptr = top;
  38.         top = top->next;
  39.         return(ptr->data);
  40.         delete ptr;
  41.     }
  42. }
  43. int main()
  44. {
  45.     char x[30];
  46.     int a, b;
  47.     cout<<"enter the balanced expression\n";
  48.     cin>>x;
  49.     for (int i = 0; i < strlen(x); i++)
  50.     {
  51.         if (x[i] >= 48 && x[i] <= 57)
  52.             push(x[i]-'0');
  53.         else if (x[i] >= 42 && x[i] <= 47)
  54.         {
  55.             a=pop();
  56.             b=pop();
  57.             switch(x[i])
  58.             {
  59.             case '+':
  60.                 push(a+b);
  61.                 break;
  62.             case '-':
  63.                 push(a-b);
  64.                 break;
  65.             case '*':
  66.                 push(a*b);
  67.                 break;
  68.             case '/':      
  69.                 push(a/b);
  70.                 break;
  71.             }
  72.         }
  73.     }
  74.     cout<<"ans is "<<pop()<<endl;
  75.     getch();
  76. }

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

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.