Advertisement
Um_nik

spoj_wrapper_1

Dec 4th, 2021
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1.  
  2. bool readToken(FILE* stream, string &s, int maxLen) {
  3.     int c = ' ';
  4.     s = "";
  5.     while(isspace(c)) c = getc(stream);
  6.     if (c == EOF) return false;
  7.     while(!isspace(c) && c != EOF) {
  8.         s.push_back((char)c);
  9.         if ((int)s.length() > maxLen) return false;
  10.         c = getc(stream);
  11.     }
  12.     return true;
  13. }
  14. bool readLong(FILE* stream, int64_t &x) {
  15.     string s;
  16.     if (!readToken(stream, s, 20)) return false;
  17.     ll sgn = 1;
  18.     if (s[0] == '-') {
  19.         sgn *= -1;
  20.         s = s.substr(1, (int)s.length() - 1);
  21.     }
  22.     for (char c : s) if (!isdigit(c)) return false;
  23.     if ((int)s.length() > 19) return false;
  24.     uint64_t y = 0;
  25.     for (char c : s) y = y * 10 + (uint64_t)(c - '0');
  26.     if (y > ((uint64_t)1 << 63)) return false;
  27.     if (y == ((uint64_t)1 << 63)) {
  28.         if (sgn == -1) {
  29.             x = 1;
  30.             x <<= 62;
  31.             x *= -1;
  32.             x += x;
  33.             return true;
  34.         } else {
  35.             return false;
  36.         }
  37.     }
  38.     x = sgn * (int64_t)y;
  39.     return true;
  40. }
  41. bool readLong(FILE* stream, int64_t &x, int64_t L, int64_t R) {
  42.     if (!readLong(stream, x)) return false;
  43.     if (x < L || x > R) return false;
  44.     return true;
  45. }
  46. bool readInt(FILE* stream, int &x, int L, int R) {
  47.     int64_t xx = 0;
  48.     if (!readLong(stream, &xx, L, R)) return false;
  49.     x = xx;
  50.     return true;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement