• Source
    1. #include<iostream>
    2. #include<stdio.h>
    3. #include<ctype.h>
    4. #include<stack>
    5. using namespace std;
    6.  
    7. int main()
    8. {
    9.  
    10. int i, t1, t2, result;
    11. char st[50];
    12. stack<int> s;
    13. while(1){
    14. puts("Give postfix equation");
    15. scanf("%s",st);
    16. if(st[0]=='-') break;
    17.  
    18. for( i=0; st[i]!= NULL ;i++){
    19. if( isdigit( st[i] ) ){
    20. t1= st[i]-'0';
    21. s.push( t1 );
    22.  
    23. }
    24. else{
    25. t1= s.top();
    26. s.pop();
    27. t2= s.top();
    28. s.pop();
    29.  
    30. if( st[i]=='+')
    31. result= t1+t2;
    32. else if( st[i]=='-')
    33. result= t2-t1;
    34. else if( st[i]=='*')
    35. result= t2*t1;
    36. else if( st[i]=='/')
    37. result= t2/t1;
    38.  
    39. s.push( result );
    40.  
    41. }
    42. }
    43. printf("The result is : %d\n\n", s.top() );
    44. s.pop(); // clear stack
    45.  
    46. }
    47. return 0;
    48. }
    49. /*
    50. 56*23-+
    51. */