Hey friends check this out:
Post.cpp:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main() {
 int n[100],i,j,top;
 char s[100],ch;
 clrscr();
 cout<<"\nEnter the Postfix expresssion:";
 cin>>s;
 for(i=0,top=-1;s[i]!='\0';i++)
 {
  if(s[i]>=48&&s[i]<=57)
  {
   switch(s[i])
   {
    case '0':n[++top]=0;
      break;
    case '1':n[++top]=1;
      break;
    case '2':n[++top]=2;
      break;
    case '3':n[++top]=3;
      break;
    case '4':n[++top]=4;
      break;
    case '5':n[++top]=5;
      break;
    case '6':n[++top]=6;
      break;
    case '7':n[++top]=7;
      break;
    case '8':n[++top]=8;
      break;
    case '9':n[++top]=9;
      break;
   }
  }
  else
  {
   if(top>=1)
   {
    switch(s[i])
    {
     case '+':n[top-1]=n[top-1]+n[top];
       break;
     case '-':n[top-1]=n[top-1]-n[top];
       break;
     case '*':n[top-1]=n[top-1]*n[top];
       break;
     case '/':n[top-1]=n[top-1]/n[top];
       break;
     case '^':n[top-1]=pow(n[top-1],n[top]);
       break;
     default:cout<<"\nSorry unavailable operator used";
    }
    top--;
    cout<<"\n"<<n[top];
   }
   else
   {
    cout<<"\nOperator entered incorrectly";
    getch();
    exit(1);
   }
  }
 }
 cout<<"\nThe result is:"<<n[top];
 getch();
}
Have Fun...................

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.