Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 2nd, 2012  |  syntax: None  |  size: 7.13 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include<iostream>
  2. #include<sstream>
  3. #include<cctype>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string s;
  9.     while( getline( cin, s ) )
  10.     {
  11.         string oper;
  12.         stringstream ss;
  13.         char operators[1000] = {0};
  14.         int operands[1000] = {0};
  15.         int operators_length = 0;
  16.         int operands_length = 0;
  17.         ss.clear();
  18.         ss.str(s);
  19.        
  20.         while( ss >> oper )
  21.         {
  22.             if( isdigit(oper[0]) )
  23.             {
  24.                 int number = 0;
  25.                 for( int i = 0 ; i < oper.length() ; i++ )
  26.                 {
  27.                     number *= 10;
  28.                     number += oper[i]-'0';
  29.                 }
  30.                 operands[operands_length++] = number;
  31.             }
  32.             else
  33.             {
  34.                 switch( oper[0] )
  35.                 {
  36.                     case '+': case '-':
  37.                         while( operators_length > 0 )
  38.                         {
  39.                             if( operators[operators_length-1] == '+' )
  40.                             {
  41.                                 operands[operands_length-2] += operands[operands_length-1];
  42.                                 operands_length--;
  43.                                 operators_length--;
  44.                             }
  45.                             else if( operators[operators_length-1] == '-' )
  46.                             {
  47.                                 operands[operands_length-2] -= operands[operands_length-1];
  48.                                 operands_length--;
  49.                                 operators_length--;
  50.                             }  
  51.                             else if( operators[operators_length-1] == '*' )
  52.                             {
  53.                                 operands[operands_length-2] *= operands[operands_length-1];
  54.                                 operands_length--;
  55.                                 operators_length--;
  56.                             }  
  57.                             else if( operators[operators_length-1] == '/' )
  58.                             {
  59.                                 operands[operands_length-2] /= operands[operands_length-1];
  60.                                 operands_length--;
  61.                                 operators_length--;
  62.                             }    
  63.                             else if( operators[operators_length-1] == '%' )
  64.                             {
  65.                                 operands[operands_length-2] %= operands[operands_length-1];
  66.                                 operands_length--;
  67.                                 operators_length--;
  68.                             }
  69.                             else break;
  70.                         }
  71.                         operators[operators_length++] = oper[0];
  72.                     break;
  73.                     case '*': case '/': case '%':
  74.                         while( operators_length > 0 )
  75.                         {
  76.                             if( operators[operators_length-1] == '*' )
  77.                             {
  78.                                 operands[operands_length-2] *= operands[operands_length-1];
  79.                                 operands_length--;
  80.                                 operators_length--;
  81.                             }
  82.                             else if( operators[operators_length-1] == '/' )
  83.                             {
  84.                                 operands[operands_length-2] /= operands[operands_length-1];
  85.                                 operands_length--;
  86.                                 operators_length--;
  87.                             }
  88.                             else if( operators[operators_length-1] == '%' )
  89.                             {
  90.                                 operands[operands_length-2] %= operands[operands_length-1];
  91.                                 operands_length--;
  92.                                 operators_length--;
  93.                             }
  94.                             else break;
  95.                         }
  96.                         operators[operators_length++] = oper[0];
  97.                     break;
  98.                     case '(':
  99.                         operators[operators_length++] = oper[0];
  100.                     break;
  101.                     case ')':
  102.                         for( int i = operators_length-1; operators[i] != '(' ; i-- )
  103.                         {
  104.                             if( operators[i] == '+' )
  105.                             {
  106.                                 operands[operands_length-2] += operands[operands_length-1];
  107.                                 operands_length--;
  108.                                 operators_length--;
  109.                             }
  110.                             if( operators[i] == '-' )
  111.                             {
  112.                                 operands[operands_length-2] -= operands[operands_length-1];
  113.                                 operands_length--;
  114.                                 operators_length--;
  115.                             }  
  116.                             if( operators[i] == '*' )
  117.                             {
  118.                                 operands[operands_length-2] *= operands[operands_length-1];
  119.                                 operands_length--;
  120.                                 operators_length--;
  121.                             }  
  122.                             if( operators[i] == '/' )
  123.                             {
  124.                                 operands[operands_length-2] /= operands[operands_length-1];
  125.                                 operands_length--;
  126.                                 operators_length--;
  127.                             }    
  128.                             if( operators[i] == '%' )
  129.                             {
  130.                                 operands[operands_length-2] %= operands[operands_length-1];
  131.                                 operands_length--;
  132.                                 operators_length--;
  133.                             }      
  134.                         }
  135.                         operators_length--;
  136.                     break;
  137.                 }
  138.             }
  139.         }
  140.         for( operators_length-- ; operators_length >= 0 ; operators_length-- )
  141.         {
  142.             if( operators[operators_length] == '+' )
  143.             {
  144.                 operands[operands_length-2] += operands[operands_length-1];
  145.                 operands_length--;
  146.             }
  147.             if( operators[operators_length] == '-' )
  148.             {
  149.                 operands[operands_length-2] -= operands[operands_length-1];
  150.                 operands_length--;
  151.             }  
  152.             if( operators[operators_length] == '*' )
  153.             {
  154.                 operands[operands_length-2] *= operands[operands_length-1];
  155.                 operands_length--;
  156.             }  
  157.             if( operators[operators_length] == '/' )
  158.             {
  159.                 operands[operands_length-2] /= operands[operands_length-1];
  160.                 operands_length--;
  161.             }      
  162.             if( operators[operators_length] == '%' )
  163.             {
  164.                 operands[operands_length-2] %= operands[operands_length-1];
  165.                 operands_length--;
  166.             }        
  167.         }
  168.         cout << operands[0] << endl;
  169.     }
  170.     return 0;
  171. }