Pushok

stack calc

Aug 2nd, 2011
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class PStack
  7. {
  8. public:
  9.  
  10.     PStack();
  11.  
  12.     void parse( char* expression );
  13.  
  14.     bool isoper( std::string str );
  15.     bool isfloat( char* );
  16.     void push( double val );
  17.     void docalc( std::string oper );
  18.  
  19.     double gettop();
  20.  
  21. private:
  22.  
  23.     double Stk[100];
  24.     double *It;
  25.     int opers_num;
  26.     const char** valid_opers;// = { "+", "-", "*", "/", "^" };
  27.  
  28. };
  29.  
  30. bool PStack::isfloat( char* str )
  31. {
  32.     char* ch = str;
  33.  
  34.     do
  35.     {
  36.        
  37.         if( *ch != '.' )
  38.         {
  39.             if( *ch > '9' || *ch < '0')
  40.             {
  41.                 return false;
  42.             }
  43.         }
  44.  
  45.     } while ( *(++ch) );
  46.  
  47.     return true;
  48. }
  49.  
  50. bool PStack::isoper( std::string str )
  51. {
  52.     for(int i = 0; i < 5; i++)
  53.     {
  54.         if( str == valid_opers[i] )
  55.         {
  56.             return true;
  57.         }
  58.     }
  59.  
  60.     return false;
  61. }
  62.  
  63.  
  64.  
  65. void PStack::parse( char* expression )
  66. {
  67.     char* pch = strtok(expression, " ");
  68.  
  69.     do
  70.     {
  71.  
  72.         if( !isoper( pch ) )
  73.         {
  74.             if( isfloat( pch ) )
  75.             {
  76.                 this->push(atof(pch));
  77.             }
  78.             else
  79.             {
  80.                 printf( "ERROR TYPE\n" );
  81.  
  82.                 return;
  83.             }
  84.         }
  85.         else
  86.         {
  87.             this->docalc( pch );
  88.         }
  89.  
  90.     } while (pch = strtok( NULL, " "));
  91. }
  92.  
  93. PStack::PStack()
  94. {
  95.     It = Stk;
  96.     opers_num = 5;
  97.     valid_opers = (const char**)malloc( sizeof( char* ) * opers_num);
  98.  
  99.     valid_opers[0] = "+";
  100.     valid_opers[1] = "-";
  101.     valid_opers[2] = "*";
  102.     valid_opers[3] = "/";
  103.     valid_opers[4] = "^";
  104. }
  105.  
  106. void PStack::push( double val )
  107. {
  108.     *It++ = val;
  109. }
  110.  
  111. void PStack::docalc( std::string oper )
  112. {
  113.     if( It - 2 >= Stk)
  114.     {
  115.         double vals[2] = {*(It-2), *(It-1)};
  116.  
  117.         It -= 2;
  118.  
  119.         if( oper == "+" )
  120.         {
  121.             this->push( vals[0] + vals[1] );
  122.         }
  123.         else if (oper == "-")
  124.         {
  125.             this->push( vals[0] - vals[1] );
  126.         }
  127.         else if (oper == "*")
  128.         {
  129.             this->push( vals[0] * vals[1] );
  130.         }
  131.         else if (oper == "/")
  132.         {
  133.             this->push( vals[0] / vals[1] );
  134.         }
  135.         else if (oper == "^")
  136.         {
  137.             this->push( pow(vals[0], vals[1]) );
  138.         }
  139.     }
  140. }
  141.  
  142. double PStack::gettop()
  143. {
  144.     return *( It - 1 );
  145.  
  146.     return 0;
  147. }
  148.  
  149. int main()
  150. {
  151.     PStack ps;
  152.     char ch[] = "1.0 2.0 1.0 + + 2.0 *";
  153.     ps.parse(ch);
  154.  
  155.     printf("%f", ps.gettop());
  156.     getchar();
  157. }
Advertisement
Add Comment
Please, Sign In to add comment