Loading docs/misc/io.md +71 −59 Original line number Diff line number Diff line Loading @@ -214,16 +214,28 @@ c = read<__int128>(); ## 完整带调试版 关闭调试开关时使用 `fread()` , `fwrite()` ,退出时自动析构执行 `fwrite()` 。 开启调试开关时使用 `getchar()` , `putchar()` ,便于调试。 若要开启文件读写时,请在所有读写之前加入 `freopen()` 。 ```cpp #define DEBUG 1 //调试开关 namespace IO { #define isdigit(x) x >= '0' && x <= '9' const int MAXSIZE = 1 << 20; // #define DEBUG 1 //调试开关 struct IO { #define MAXSIZE (1 << 20) #define isdigit(x) (x >= '0' && x <= '9') char buf[MAXSIZE], *p1, *p2; char pbuf[MAXSIZE], *pp; #if DEBUG #else IO() : p1(buf), p2(buf), pp(pbuf) {} ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); } #endif inline char gc() { #if DEBUG //调试,可显示字符 return getchar(); #endif static char buf[MAXSIZE], *p1 = buf + MAXSIZE, *p2 = buf + MAXSIZE; if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin); return p1 == p2 ? -1 : *p1++; } Loading @@ -238,9 +250,10 @@ inline void read(T &x) { register char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') sign = 1; for (; isdigit(ch); ch = gc()) x = x * 10 + ch - '0'; for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0'); if (ch == '.') for (ch = gc(); isdigit(ch); ch = gc()) tmp /= 10.0, x += tmp * (ch - 48); for (ch = gc(); isdigit(ch); ch = gc()) tmp /= 10.0, x += tmp * (ch - '0'); if (sign) x = -x; } inline void read(char *s) { Loading @@ -255,29 +268,28 @@ inline void read(char &c) { ; } inline void push(const char &c) { char pbuf[MAXSIZE], *pp = pbuf; #if DEBUG //调试,可显示字符 putchar(c); #else if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf; *pp++ = c; #endif } template <class T> inline void write(T x) { if (x < 0) x = -x, push('-'); // 负数输出 static T sta[35]; T top = 0; do { sta[top++] = x % 10, x /= 10; } while (x); #if DEBUG //调试,可显示字符 while (top) putchar(sta[--top] + '0'); return; #endif while (top) push(sta[--top] + '0'); } template <class T> inline void write(T x, char lastChar) { write(x), putchar(lastChar); //打印末尾字符 ex.'\n' write(x), push(lastChar); } } // namespace IO using namespace IO; } io; ``` ## 参考 Loading Loading
docs/misc/io.md +71 −59 Original line number Diff line number Diff line Loading @@ -214,16 +214,28 @@ c = read<__int128>(); ## 完整带调试版 关闭调试开关时使用 `fread()` , `fwrite()` ,退出时自动析构执行 `fwrite()` 。 开启调试开关时使用 `getchar()` , `putchar()` ,便于调试。 若要开启文件读写时,请在所有读写之前加入 `freopen()` 。 ```cpp #define DEBUG 1 //调试开关 namespace IO { #define isdigit(x) x >= '0' && x <= '9' const int MAXSIZE = 1 << 20; // #define DEBUG 1 //调试开关 struct IO { #define MAXSIZE (1 << 20) #define isdigit(x) (x >= '0' && x <= '9') char buf[MAXSIZE], *p1, *p2; char pbuf[MAXSIZE], *pp; #if DEBUG #else IO() : p1(buf), p2(buf), pp(pbuf) {} ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); } #endif inline char gc() { #if DEBUG //调试,可显示字符 return getchar(); #endif static char buf[MAXSIZE], *p1 = buf + MAXSIZE, *p2 = buf + MAXSIZE; if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin); return p1 == p2 ? -1 : *p1++; } Loading @@ -238,9 +250,10 @@ inline void read(T &x) { register char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') sign = 1; for (; isdigit(ch); ch = gc()) x = x * 10 + ch - '0'; for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0'); if (ch == '.') for (ch = gc(); isdigit(ch); ch = gc()) tmp /= 10.0, x += tmp * (ch - 48); for (ch = gc(); isdigit(ch); ch = gc()) tmp /= 10.0, x += tmp * (ch - '0'); if (sign) x = -x; } inline void read(char *s) { Loading @@ -255,29 +268,28 @@ inline void read(char &c) { ; } inline void push(const char &c) { char pbuf[MAXSIZE], *pp = pbuf; #if DEBUG //调试,可显示字符 putchar(c); #else if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf; *pp++ = c; #endif } template <class T> inline void write(T x) { if (x < 0) x = -x, push('-'); // 负数输出 static T sta[35]; T top = 0; do { sta[top++] = x % 10, x /= 10; } while (x); #if DEBUG //调试,可显示字符 while (top) putchar(sta[--top] + '0'); return; #endif while (top) push(sta[--top] + '0'); } template <class T> inline void write(T x, char lastChar) { write(x), putchar(lastChar); //打印末尾字符 ex.'\n' write(x), push(lastChar); } } // namespace IO using namespace IO; } io; ``` ## 参考 Loading