WKC祕製讀(寫)掛

//傳統簡單樸素讀入掛,只可以讀取非負整數。實際效果不佳
template <class T> inline void scand(T &x) 
{ char c; x = 0; while ((c = getchar())<'0'); while (c >= '0'&&c <= '9')x = (x << 3) + (x << 1) + (c - 48), c = getchar(); }

//簡單輸入掛,使用方法是複製const int SIZE以下的6行作爲基本讀入的"頭文件"。其中SIZE爲緩衝區的分塊大小(設置小的話,會增加讀入的次數,這裏推薦使用1 << 20即1mb
const int SIZE = 1 << 20;
char S[SIZE], *SS = S, *ST = S, CH;
inline char getc()
{
	return SS == ST && (ST = (SS = S) + fread(S, 1, SIZE, stdin), SS == ST) ? 0 : *SS++;
}

//以下是各種讀入函數
template<class T>inline bool read(T &x)
{
	x = 0; int sgn = 1;
	while (CH = getc(), !isdigit(CH) && CH != '-')if (CH == 0)return 0;
	if (CH == '-')sgn = -1; else x = CH ^ 48;
	while (CH = getc(), isdigit(CH))x = (x << 3) + (x << 1) + (CH ^ 48);
	x *= sgn;
	return 1;
}
template<class T1, class T2>inline void read2(T1 &x, T2 &y)
{
	read(x); read(y);
}
template<class T1, class T2, class T3>inline void read3(T1 &x, T2 &y, T3 &z)
{
	read(x); read(y); read(z);
}

//讀入單一字符串,以非可見字符爲字符串的結束(ascii值<'!')
inline void reads(char s[])
{
	int n = 0;
	while (CH = getc(), CH<'!');
	s[n++] = CH;
	while (CH = getc(), CH >= '!')s[n++] = CH;
	s[n] = 0;
}
//讀入單行字符串,以換行字符爲結束(ascii值=='!')
inline void GETS(char s[])
{
	int n = 0;
	while (CH = getc(), CH == 0);
	while (CH != '\n')
	{
		s[n++] = CH;
		CH = getc();
	}s[n] = 0;
}

//輸出掛,整數用print,否則用putc,初始化爲這裏的"PS = PF",最後要輸出fwrite(PF, 1, SIZE, stdout); *PS = PF, *PT = PF + SIZE; 用來把緩衝區清掉
char PF[SIZE], *PS = PF, *PT = PF + SIZE;
void putc(char ch)
{
	if (PS == PT)fwrite(PF, 1, SIZE, stdout), PS = PF;
	*PS++ = ch;
}
template<class T>inline void print(T x)
{
	char s[20], *b; b = s;
	bool sgn = 0;
	if (x < 0) { sgn = 1; x = -x; }
	if (!x)*b++ = 48;
	while (x) { *b++ = x % 10 + 48; x /= 10; }
	if (sgn)putc('-');
	while (b-- != s) putc(*b);
}
void prints(char s[])
{
	for (int i = 0; s[i]; ++i)putc(s[i]);
}

//清緩衝區:這兩句話要加在輸出之後
fwrite(PF, 1, PS - PF, stdout);
*PS = PF, *PT = PF + SIZE;

//普通讀入加速外掛
inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x*f;
}

//C++的擴棧
#pragma comment(linker,"/STACK:102400000,102400000")

//G++的擴棧,放在int main(){}的開頭
int __size__ = 16 << 20; // 16MB
char *__p__ = (char*)malloc(__size__) + __size__;
__asm__("movl %0, %%esp\n" :: "r"(__p__));


//完整輸入掛:2015年8月5日_WKC祕製讀寫掛
#include<stdio.h>
#include<ctype.h>

const int SIZE = 1 << 20;
char S[SIZE], *SS = S, *ST = S, CH;//SS和ST分別表示字符串的起點和終點(+1)
inline char getc()//字符讀入,帶有判斷EOF的作用
{
	if (SS != ST)return *SS++;
	int len = fread(S, 1, SIZE, stdin);
	if (len == 0)return 0;
	SS = S; ST = S + len; return *SS++;
	//下面這個是簡化寫法
	//return SS==ST&&(ST=(SS=S)+fread(S,1,SIZE,stdin),SS==ST)?0:*SS++;
}
template<class T>inline void readu(T &x)//正整數,不判定字符串結束
{
	while (CH = getc(), !isdigit(CH));
	x = CH ^ 48;
	while (CH = getc(), isdigit(CH))x = (x << 3) + (x << 1) + (CH ^ 48);
}
template<class T>inline bool readU(T &x)//正整數,附帶判定字符串結束
{
	while (CH = getc(), !isdigit(CH))if (CH == 0)return 0;
	x = CH ^ 48;
	while (CH = getc(), isdigit(CH))x = (x << 3) + (x << 1) + (CH ^ 48);
	return 1;
}
template<class T>inline void readd(T &x)//正負整數,不判定字符串結束
{
	x = 0; int sgn = 1;
	while (CH = getc(), !isdigit(CH) && CH != '-');
	if (CH == '-')sgn = -1; else x = CH ^ 48;
	while (CH = getc(), isdigit(CH))x = (x << 3) + (x << 1) + (CH ^ 48);
	x *= sgn;
}
template<class T>inline bool readD(T &x)//正負整數,附帶判定字符串結束
{
	x = 0; int sgn = 1;
	while (CH = getc(), !isdigit(CH) && CH != '-')if (CH == 0)return 0;
	if (CH == '-')sgn = -1; else x = CH ^ 48;
	while (CH = getc(), isdigit(CH))x = (x << 3) + (x << 1) + (CH ^ 48);
	x *= sgn;
	return 1;
}
template<class T>inline void readf(T &x)//通用,包括浮點數,不判定字符串結束,不可以以.開頭
{
	x = 0; int sgn = 1;
	while (CH = getc(), !isdigit(CH) && CH != '-');
	if (CH == '-')sgn = -1; else x = CH ^ 48;
	while (CH = getc(), isdigit(CH))x = x * 10 + (CH ^ 48);
	if (CH == '.')
	{
		T bit = 0.1;
		while (CH = getc(), isdigit(CH))x += (CH ^ 48)*bit, bit /= 10;
	}
	x *= sgn;
}
template<class T>inline bool readF(T &x)//通用,包括浮點數,附帶判定字符串結束,不可以以.開頭
{
	x = 0; int sgn = 1;
	while (CH = getc(), !isdigit(CH) && CH != '-')if (CH == 0)return 0;
	if (CH == '-')sgn = -1; else x = CH ^ 48;
	while (CH = getc(), isdigit(CH))x = x * 10 + (CH ^ 48);
	if (CH == '.')
	{
		T bit = 0.1;
		while (CH = getc(), isdigit(CH))x += (CH ^ 48)*bit, bit /= 10;
	}
	x *= sgn;
	return 1;
}
inline void reads(char s[])//字符串,不判定字符串結束
{
	int n = 0;
	while (CH = getc(), CH<'!');
	s[n++] = CH;
	while (CH = getc(), CH >= '!')s[n++] = CH;
	s[n] = 0;
}
inline bool readS(char s[])//字符串,附帶判定字符串結束
{
	int n = 0;
	while (CH = getc(), CH<'!')if (CH == 0)return 0;
	s[n++] = CH;
	while (CH = getc(), CH >= '!')s[n++] = CH;
	s[n] = 0;
	return 1;
}
inline void readc(char &ch)//單字符,不判定字符串結束
{
	while (CH = getc(), CH<'!');
	ch = CH;
}
inline bool readC(char &ch)//單字符,附帶判定字符串結束
{
	while (CH = getc(), CH<'!')if (CH == 0)return 0;
	ch = CH;
	return 1;
}
void fre()//關聯文件讀寫
{
	freopen("c://test//input.in", "r", stdin);
	freopen("c://test//output.out", "w", stdout);
}
void Fscanf()
{
	//如何實現指定文件或者多文件讀寫呢?
	//FILE *FI=fopen("c://test//input.in","r");
	//fscanf(FI(文件名),"%d",&x);這種形式
}
void Fread()
{
	//fread(s,1,SIZE,stdin);
	//fread(字符串名,單位讀入長度,讀入次數,讀入來源);
	//fread會恰好返回讀入長度,比如對於"1 2\n"的返回值爲4
}
void test()
{
	unsigned long long x; readu(x); printf("%llu\n", x);
	long long y; readd(y); printf("%lld\n", y);
	double z; readf(z); printf("%f\n", z);
	char s[1010]; reads(s); printf("%s\n", s);
	char ch; readc(ch); printf("%c\n", ch);
}
void testU()
{
	unsigned long long x;
	while (readU(x))printf("%llu\n", x);
}
void testD()
{
	long long x;
	while (readD(x))printf("%lld\n", x);
}
void testS()
{
	char s[1010];
	while (readS(s))printf("%s\n", s);
}
void testC()
{
	char ch;
	while (readC(ch))printf("%c\n", ch);
}
int main()
{
	//fre();
	test();
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章