PROGRAM TO EVALUATE POSTFIX EXPRESSIONS.


#include<iostream.h>             //HEADER FILES
#include<string.h>
#include<ctype.h>
#include<process.h>
#include<math.h>
#define SIZE 50

class stack                      //STACK DEFINITION
{
float arr[SIZE];
int top;
public:                       //FUNCTIONS OF STACK CLASS
bool isempty();
bool isfull();
void push(float);
float pop();
void display();
void convert(char *);
stack()
{
top=-1;
}
};

bool stack::isempty()             // FUNCTION DEFINITION
{
return(top==-1);
}

bool stack::isfull()
{
return(top==(SIZE-1));
}
void stack::push(float ch)               // FUNCTION DEFINATION TO INSERT AN ITEM INTO STACK
{
if(isfull())
{
cout<<"\n STACK OVERFLOW";
return;
}
arr[++top]=ch;
}
float stack::pop()                       // FUNCTION DEFINITION TO DELETE AN ITEM FORM STACK
{
if(isempty())
return('?');
return(arr[top--]);
}
void stack::display()                    // FUNCTION DEFINATION TO DESPLAY STACK(STCK ITEMS)
{
cout<<endl;
if(isempty())
{
cout<<" STACK EMPTY";
return;
}
for(int i=0;i<=top;i++)
cout<<arr[i];
}

void stack::convert(char* array)                    // FUNCTION DEFNATION TO CONVERT INTO POSTFIX
{
int i,j,l;//,index=-1;
float a,b,num,dec;
l=strlen(array);
for(i=0;i<l;i++)
{
num=0;
dec=1;
if(array[i]==' ')
continue;
if(isdigit(array[i]))
{
for(j=i;array[j]!='.'&&array[j]!=' ';j++)
num=num*10+(array[j]-48);
if(array[j]=='.')
for(j++;array[j]!=' ';j++)
{
dec/=10;
num=num+dec*(array[j]-48);
}
i=j;
push(num);
continue;
}
if((i<l)&&(!isdigit(array[i])))
if(top<1)
{
cout<<"\n SYNTAX ERROR  EXTRA OPERATOR ENCOUNTERED";
exit(0);
}
b=pop();
a=pop();
switch(array[i])
{
case '+': a+=b;
push(a);
break;
case '-': a-=b;
push(a);
break;
case '*': a*=b;
push(a);
break;
case '/': if(b==0)
{
cout<<"\n MATH ERROR  DIVISION BY ZERO ENCOUNTERED";
exit(0);
}
else
{
a/=b;
push(a);
}
break;
case '%': if(b==0)
cout<<"\n MATH ERROR  DIVISION BY ZERO ENCOUNTERED";
else
{
a=(int)a%(int)b;
push(a);
}
break;
case '$': a=pow((int)a,(int)b);
push(a);
break;
}
}
cout<<"\n result ::"<<pop()<<endl;
}

void main()
{
stack postfix;
char exp[SIZE];
cout<<"\n ENTER THE POSTFIX EXPRESSION"
<<"\n USE SPACE BETWEEN OPERATOR AND OPERANDS:\n";
cin.getline(exp,SIZE);
postfix.convert(exp);
}

, , , ,

  1. #1 by AndrewBoldman at June 5th, 2009

    Hi, cool post. I have been wondering about this topic,so thanks for writing.

  2. #2 by admin at June 5th, 2009

    Thankz andrew. Your comment motivate us a lot to add more useful things in this blog. We are adding more interesting stuff so see you soon :).

  3. #3 by Kelly Brown at June 12th, 2009

    The best information i have found exactly here. Keep going Thank you

  4. #4 by CrisBetewsky at July 6th, 2009

    I’m glad that after surfing the web for uch a long time I have found out this information. I’m really lucky.

  5. #5 by KonstantinMiller at July 6th, 2009

    Hi! I like your srticle and I would like very much to read some more information on this issue. Will you post some more?

  6. #6 by essay writing at July 14th, 2009

    Hi. I like the way you write. Will you post some more articles?

  7. #7 by dmarco at August 18th, 2009

    Thanks for this

  8. #8 by asss at August 22nd, 2009

    I want to say - thank you for this

  9. #9 by optura at August 23rd, 2009

    It is the coolest site, keep so

  10. #10 by mdweb at August 24th, 2009

    Thanks for the contibution!!!

  11. #11 by viagr at August 24th, 2009

    Thanks so much!

  12. #12 by dafdaf at August 26th, 2009

    Thanks!

  13. #13 by raincity at August 27th, 2009

    u r godsent dude.. THANKS

  14. #14 by padova at August 27th, 2009

    Great thank You !
    Good job :)

  15. #15 by desert at September 2nd, 2009

    Thanks a bunch for this!

  16. #16 by creain at September 2nd, 2009

    Thanks for this!

  17. #17 by crapag at September 6th, 2009

    you have a great blog!!1

  18. #18 by soundtrack at March 18th, 2010

    Great work, webmaster, nice design!

  19. #19 by ?????? at April 19th, 2010

    nice job men:)

(will not be published)
  1. No trackbacks yet.