Advertisement
yuawn

Boss_attack_B

Mar 29th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define pb push_back
  3. #define err (s.size() == 0x11 || s.size() == 0x0b)
  4. using namespace std;
  5.  
  6.  
  7. typedef enum{
  8.     OPERATOR,
  9.     INT,
  10.     LINEENTER
  11. } Symbol;
  12.  
  13.  
  14. Symbol sym;
  15. int t = 0 , num , only_int = 0 , aaa = 0;
  16. char op;
  17. bool error = 0 , error2 = 0 , error3 = 0 , zero = 0;
  18. vector<string> p;
  19. string s;
  20.  
  21.  
  22. bool is_op( char c ){ return c == '+' || c == '-' || c == '*' || c == '/'; }
  23. bool is_num( char c ){ return c >= '0' && c <= '9'; }
  24.  
  25. bool is_int(){
  26.     int tmp = t;
  27.     if( (s[t] == '+' || s[t] == '-') && is_num( s[t+1] ) ) return 1;
  28.     else if( is_num( s[t] ) ) return 1;
  29.     return 0;
  30. }
  31.  
  32.  
  33. Symbol peek(){
  34.     while( s[t] == ' ' && ++t );
  35.     if( is_int() ) return INT;
  36.     else if( is_op( s[t] ) ) return OPERATOR;
  37.     else if( s[t] == '\n' || s[t] == '\0' ) return LINEENTER;
  38. }
  39.  
  40. int match( Symbol sym ){
  41.     while( s[t] == ' ' && ++t );
  42.     if( sym == OPERATOR ){
  43.         op = s[t++];
  44.         return 2147483647;
  45.     }
  46.     else if( sym == INT ){
  47.         if( !is_int() ) error = 1;
  48.         int neg = 1 , sum = 0;
  49.         if( s[t] == '-' ) ++t , neg = -1;
  50.         if( s[t] == '+' ) ++t;
  51.         while( is_num( s[t] ) ){
  52.             sum *= 10;
  53.             sum += s[t++] - 48;
  54.         }
  55.         num = sum * neg;
  56.     }
  57.     else if( sym == LINEENTER ){
  58.     }
  59. }
  60.  
  61. bool ReadFormula(){
  62.     stringstream ss;
  63.     ss << s;
  64.     string tmp;
  65.     while( ss >> tmp ){
  66.         for( int i = 0 ; i < tmp.size() ; ++i ){
  67.             //cout << tmp[i] << endl;
  68.             if( !is_op( tmp[i] ) && !is_num( tmp[i] ) ){
  69.                 int j = i - 1 , k = i + 1;
  70.                 bool bb = 1;
  71.                 while( !is_num( tmp[j] ) && !is_op( tmp[j] ) && j > -1 ) --j;
  72.                 if( is_num( tmp[j] ) ){
  73.                     while( is_num( tmp[j] ) || ( (tmp[j] == '+' || tmp[j] == '-') && bb ) ) {
  74.                         if( tmp[j] == '+' || tmp[j] == '-' ) bb = 0;
  75.                         --j;
  76.                     }
  77.                 }
  78.                 while( !is_num( tmp[k] ) && !is_op( tmp[k] ) && k < tmp.size() ) ++k;
  79.                 while( is_num( tmp[k] ) || is_op( tmp[k] ) ) ++k;
  80.                 cout << "Error: Unknown token ";
  81.                 for( int w = j + 1 ; w < k ; ++w ) cout << tmp[w];
  82.                 puts("");
  83.                 return 0;
  84.             }
  85.         }
  86.     }
  87.     return 1;
  88. }
  89.  
  90. int exp(){
  91.     if( peek() == INT ){
  92.         match( INT );
  93.         return num;
  94.     }
  95.     else if( peek() == OPERATOR ){
  96.         //puts( "OPERA" );
  97.         error2 = 1;
  98.         match( OPERATOR );
  99.         char o = op;
  100.         int a , b;
  101.         a = exp();
  102.         b = exp();
  103.         match( LINEENTER );
  104.         if( b == 2147483647 ){
  105.             if( o != '-' || o != '+' ) error = error3 = 1;
  106.             if( o == '-' ) return a * -1;
  107.             else return a;
  108.         }
  109.         //cout << a << o << b << endl;
  110.         switch( o ){
  111.             case '+':
  112.                 return a + b;
  113.                 break;
  114.             case '-':
  115.                 return a - b;
  116.                 break;
  117.             case '*':
  118.                 return a * b;
  119.                 break;
  120.             case '/':
  121.                 if( !b ){
  122.                     //printf( "aaa->%d error->%d error2->%d error3->%d only_int->%d zero -> %d\n" , aaa,error,error2,error3,only_int,zero );
  123.                     if( !( error || only_int > 1 || aaa > 1 ) ) zero = 1;
  124.                     return 0;
  125.                 }
  126.                 return a / b;
  127.                 break;
  128.         }
  129.     }
  130.     else {
  131.         //error = 1;
  132.         return 2147483647;
  133.         //puts( "exp error" );
  134.     }
  135.     return 0;
  136. }
  137.  
  138. int exps(){
  139.     if( peek() == OPERATOR || peek() == INT ){
  140.         error2 = 0;
  141.         ++aaa;
  142.         int ans = exp();
  143.         if( !error2 ) ++only_int;
  144.         exps();
  145.         return ans;
  146.     }
  147.     else{
  148.         // LAMDA
  149.     }
  150. }
  151.  
  152. int prog(){
  153.     if( peek() == OPERATOR || peek() == INT ){
  154.         return exps();
  155.     }
  156.     else{
  157.         error = 1;
  158.         //puts( "prog error" );
  159.     }
  160. }
  161.  
  162.  
  163. int EvalFormula(){
  164.     return prog();
  165. }
  166.  
  167. int main(){
  168.     puts( "Welcome use our calculator!" );
  169.  
  170.     cout << "> ";
  171.     while( getline( cin , s ) ){
  172.         error = error2 = error3 = only_int = zero = aaa = t = 0;
  173.  
  174.         while( s[t] == ' ' && ++t );
  175.         if( t == s.size() && cin.eof() ) break;
  176.  
  177.         if( s == "" ) {
  178.             cout << "> ";
  179.             continue;
  180.         }
  181.  
  182.         if( !ReadFormula() ) {
  183.             cout << "> ";
  184.             continue;
  185.         }
  186.  
  187.         int ans = EvalFormula();
  188.         //printf( "aaa->%d error->%d error2->%d error3->%d only_int->%d zero -> %d\n" , aaa,error,error2,error3,only_int,zero );
  189.         if( aaa > 1  ) {
  190.             if( aaa == 2 && only_int == 1 && err ) puts( "Error: Divide by ZERO!" );
  191.             else puts("Error: Illegal formula!");
  192.         }
  193.         else if( error || only_int > 1 && !zero ) puts("Error: Illegal formula!");
  194.         else if( zero ) puts( "Error: Divide by ZERO!" );
  195.         else{
  196.             cout << ans << endl;
  197.         }  
  198.         if( cin.eof() ) break;
  199.         cout << "> ";
  200.     }
  201.     puts( "ByeBye~" );
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement