Wednesday, 2 August 2017

Postfix expression evaluation like: xy+


Problem Statement:
Postfix expression evaluation like: xy+     (x=100, y=200) i:e 100200+

//Program By : Mr. Nitin M Shivale 9096454130 if any suggestion please!!!!
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
//declaration of class for postfix expression evaluation.
class stack
{
 int top;           //by default private (data memebers)
 int stk[20];
 char inp[20];
 int x,i;
 public:
 stack()     //constructor to init data members
 {
  top=-1;
  x=i=0;
 }
 void getdata();    //memeber functions..
 void show();
 int isempty();
 int isfull();
 void push(int);
 int pop();
 int posteval();
}; // end of class stack declaration.
/*************************************************
Fun name: posteval()
return type:integer
no of arg:NULL
purpose: To eval postfix expression.
**************************************************/

int stack::posteval()
{
 int t1,t2,t3;
 for(i=0;inp[i]!='\0';i++)
 {
  if(isalpha(inp[i]))  //to check whether operand or operator.
  {
   cout<<"\nEnter the value of "<<inp[i];
   cin>>x;
   push(x);    //if operand push it into stack.
  }
  else
  {
   t1=pop();  //if operator pop 2 operand from stack.
   t2=pop();
   switch(inp[i])  //as per the operator switch into case & perform operation
   {
    case '+':t3=t1+t2;
         push(t3);
         break;
    case '-':t3=t1-t2;
         push(t3);
         break;
    case '*':t3=t1*t2;
         push(t3);
         break;
    case '/':t3=t1/t2;
         push(t3);
         break;
   }//end of switch
  }//end of else
 }//end of for loop
 return pop(); //after evaluation return the final result
}

int stack::pop() //To return the stack value.
{
 int z;
 z=stk[top];
 top--;
 return z;
}

int stack::isempty()  //To check the stack empty case
{
 if(top==-1)
  return 0;
 else
  return 1;
}

void stack::push(int n) //push the value inside the stack.
{
 top++;
 stk[top]=n;
}
void stack::getdata()   //To accept the postfix expression from user.
{
 cout<<"\nEnter the expression:";
 cin>>inp;
}
int stack::isfull() //To check stack full condition.
{
 if(top==20)
  return 1;
 else
  return 0;
}

void stack::show()//To display the postfix expression
{
 cout<<"\n Postfix Expression Evaluation are as follows...";
 while(inp[i]!='\0')
 {
  cout<<inp[i];
  i++;
 }
}
int main()  //Program execution starts from here
{
 int res;
 cout<<"\nPostfix Evaluation....!!!";
 stack s1;      //objecct creation
 s1.getdata();
 s1.show();   //member function calling.
 res=s1.posteval();
 cout<<"\nThe final result of exp is:"<<res;
 getch();
 return 0;
}

data structures and algorithms Web Developer

1 comment:

  1. Hii can you please provide sppu assignment 19 which is based on doubly linked list. Thank you

    ReplyDelete