Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool readToken(FILE* stream, string &s, int maxLen) {
- int c = ' ';
- s = "";
- while(isspace(c)) c = getc(stream);
- if (c == EOF) return false;
- while(!isspace(c) && c != EOF) {
- s.push_back((char)c);
- if ((int)s.length() > maxLen) return false;
- c = getc(stream);
- }
- return true;
- }
- bool readLong(FILE* stream, int64_t &x) {
- string s;
- if (!readToken(stream, s, 20)) return false;
- ll sgn = 1;
- if (s[0] == '-') {
- sgn *= -1;
- s = s.substr(1, (int)s.length() - 1);
- }
- for (char c : s) if (!isdigit(c)) return false;
- if ((int)s.length() > 19) return false;
- uint64_t y = 0;
- for (char c : s) y = y * 10 + (uint64_t)(c - '0');
- if (y > ((uint64_t)1 << 63)) return false;
- if (y == ((uint64_t)1 << 63)) {
- if (sgn == -1) {
- x = 1;
- x <<= 62;
- x *= -1;
- x += x;
- return true;
- } else {
- return false;
- }
- }
- x = sgn * (int64_t)y;
- return true;
- }
- bool readLong(FILE* stream, int64_t &x, int64_t L, int64_t R) {
- if (!readLong(stream, x)) return false;
- if (x < L || x > R) return false;
- return true;
- }
- bool readInt(FILE* stream, int &x, int L, int R) {
- int64_t xx = 0;
- if (!readLong(stream, &xx, L, R)) return false;
- x = xx;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement