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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值