C++ | Evaluate Postfix expression

Hello peeps! Lord Hypersonic greets you. Today I am here with a new c++ program which can evaluate postfix expression of single digit numbers( numbers from 1 to 9)


Header files included:-

  • iostream: To use input and output stream (cout as output stream and cin for input stream).
  • stack: To use to create a stack of specific data-type and to use the member functions of the stack like push() and pop().
  • ctype.h OR cctype: To use the function isdigit() (this function is used to check, whether the given character is digit or alphabet).
  • conio.h: To use _getch() function.

Variables used:-

  • val of integer data-type.
  • ch of character data-type.
  • s of integer stack type.
  • expresion of string data-type.
  • len of integer data-type.
  • of integer data-type.
  • sc of integer data-type.
  • a of integer data-type.
  • x of integer data-type.
  • y of integer data-type.

Working:-

  • First, the user will be asked to enter the postfix expression. The expression will be stored in variable "expresion".
  • variable "len" is equal to the length of expresion.
  • A loop will be started from i = 0 to i =len.
  • After starting loop, the first thing it checks is that if there is space in "expresion" at index "i", if there is, then it will skip all the processing and move to the next index.
  • The second thing it does after getting into the loop is making the value of "ch" equal to the value of "expresion" at index "i", i.e., (ch=expresion[i]).
  • Now it checks the values of "ch". If ch is a digit, then "a" will be equal to "ch". As a is integer and ch is a character, making a=ch will make "a" equal to ASCII code of that character. Then it checks the ASCII code, and according to it provide value to "sc".
  • After assigning value to "sc", it will be pushed in stack "s" using push() function (s.push(sc)).
  • If "ch" is not a digit, then "x" will be equal to the top value of stack s ( x=s.top() ) after this, top element will be removed from stack using pop() function ( s.pop() ). Similarly, "y" will be equal to the top value of "s" (element next to the element assigned to "x"). And this element will also be removed using pop() function.
  • After doing this, it checks what is the value of ch,
    if it is (+) then val will be equal to x+y.
    if it is (-) then val will be equal to y-x.
    if it is (*) then val will be equal to  y*x.
    if it is (/) then val will be equal to y/x.
  • After that val will be pushed to stack s, using push() function (s.push(val)).
  • Following this, the final answer will be stored at the top of the stack s, at the end of the loop.
  • After all the processing loop, the final answer will be printed using top() function.
    cout<<"Expression evaluated to: "<<s.top()<<endl;
This is how you can create a program which can evaluate a postfix expression. The way you think may be different than me, your code may be different than me, if this is the case please post your code in comments so others can also get benefit from your code by knowing more possible ways of making the same program.



Source Code:-

/*

Program: Evaluation of Postfix expression
Description: This program evaluates the given postfix expression. It takes only a single digit number (i.e. 0-9).
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.in

*/

#include <iostream>
#include <stack>
#include <cctype>
#include <conio.h>

using namespace std;

int main()
{
    int val; char ch;
    stack<int> s;
    string expresion;
    cout<<"\nEnter postfix expression to evaluate: "; getline(cin,expresion); //asking for the postfix expression.
    int len=expresion.length(); // len is now equal to the length of the expression.
    for (int i=0; i<len; i++) // loop for evaluating the expression
    {
        if (expresion[i]==' ') continue; //checking for space, if there is space then move to next index
        ch=expresion[i];   // ch is now equal to the digit or operator at the index i in expression.
        if (isdigit(ch)) //checking if ch is a digit, if yes then processing further.
        {
            int sc;  //declaring interger sc
            int a=ch; // declaring and initializing a with ASCII code of character ch
            if(a==48) sc=0; //if a is equal to 48 then sc is 0
            if(a==49) sc=1; //if a is equal to 49 then sc is 1
            if(a==50) sc=2; //if a is equal to 50 then sc is 2
            if(a==51) sc=3; //if a is equal to 51 then sc is 3
            if(a==52) sc=4; //if a is equal to 52 then sc is 4
            if(a==53) sc=5; //if a is equal to 53 then sc is 5
            if(a==54) sc=6; //if a is equal to 54 then sc is 6
            if(a==55) sc=7; //if a is equal to 55 then sc is 7
            if(a==56) sc=8; //if a is equal to 56 then sc is 8
            if(a==57) sc=9; //if a is equal to 57 then sc is 9
            s.push(sc); //pusing sc to the stack s
        }
        if(!isdigit(ch))//if ch is not a digit then it will process like this.
        {
            int x=s.top(); s.pop();  //integer x will be equal to the to element of stack s, and then pop that element from stack.
            int y=s.top(); s.pop(); //interger y will be equal to the element next to the x, and then pop that element.
            if(ch=='+') //if ch is a plus sign then val will be equal to the sum of x and y
                val=x+y;
            if (ch=='-') // if ch is a minus sign, then val will be equal to the subtraction of y and x
                val=y-x;
            if (ch=='*') // if ch is equal to *, then val will be equal to the multiplication of x and y
                val=x*y;
            if (ch=='/') // if ch is equal to forward slash, hen val will be equal to y divided by x
                val=y/x;
            s.push(val); //pushing value of val in stack s
        }
    }
    cout<<"Expression evaluated to: "<<s.top()<<endl; //printing the value of top element of stack s
    _getch();
    return 0;

}
Here is the working of the program:- 

ENJOY 😉
 HAPPY CODING.

No comments

Powered by Blogger.