Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <cctype>
  5. #include <cstring>
  6. #include <inttypes.h>
  7.  
  8. using namespace std;
  9.  
  10. class Recurs_descent_calc {
  11.     public:
  12.         bool there_was_error;
  13.         int64_t count_up(char* str_expr) {
  14.             expr = str_expr;
  15.             return add();
  16.         }
  17.         Recurs_descent_calc(): there_was_error(false) {}
  18.     private:
  19.         char* expr;
  20.         void eat_up_next_spaces() { for(; expr[0] == ' '; expr++); }
  21.         int64_t add() {
  22.             eat_up_next_spaces();
  23.             int64_t counted = sub();
  24.             eat_up_next_spaces();
  25.             if(*expr == '+') { expr++; return counted + add(); }
  26.             return counted;
  27.         }
  28.         int64_t sub() {
  29.             eat_up_next_spaces();
  30.             int64_t counted = mul_div();
  31.             eat_up_next_spaces();
  32.             if(*expr == '-') { expr++; return counted - sub(); }
  33.             return counted;
  34.         }
  35.         int64_t mul_div() {
  36.             eat_up_next_spaces();
  37.             int64_t counted = minus();
  38.             eat_up_next_spaces();
  39.             if(*expr == '*') { expr++; return counted * mul_div(); }
  40.             if(*expr == '/') {
  41.                 expr++;
  42.                 int64_t divider = mul_div();
  43.                 if(divider != 0) return counted / divider;
  44.                 there_was_error = true;
  45.             }
  46.             return counted;
  47.         }
  48.         int64_t minus() {
  49.             eat_up_next_spaces();
  50.             if(*expr == '-') {
  51.                 expr++;
  52.                 return -1 * num(); //Может ли быть много ун. минусов подряд?
  53.             }
  54.             return num();
  55.         }
  56.         int64_t number_of_digits(int64_t num) {
  57.             int64_t q;
  58.             for(q = 0; num != 0; q++) num = num / 10;
  59.             return q;
  60.         }
  61.         int64_t num() {
  62.             eat_up_next_spaces();
  63.             int64_t number;
  64.             if(sscanf(expr, "%" SCNd64, &number)) {
  65.                 expr += number_of_digits(number);
  66.                 eat_up_next_spaces();
  67.                 return number;
  68.             }
  69.             there_was_error = true;
  70.         }
  71. };
  72.  
  73. int main(int argc, char* argv[]) {
  74.     int64_t result;
  75.     Recurs_descent_calc calc;                   //Create a calculator instance
  76.     if(argc != 2) calc.there_was_error = true;  //Сheck the correctness of input
  77.     else result = calc.count_up(argv[1]);       //Feed him the entered expr to calculate
  78.     if(calc.there_was_error) {                  //Сheck the correctness of calculation
  79.         cout << "error" << '\n';
  80.         return 1;
  81.     }
  82.     cout << result << '\n';
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement