1. 程式人生 > >快速讀入(存模板)

快速讀入(存模板)

#ifndef FASTREAD_H
#define FASTREAD_H

#include <cstdio>

template <typename t>

t fastread_intergar(void)
{
	t res = 0;
	short f = 1;
	char ch = getchar();
	
	while ((ch < '0') || (ch > '9'))
	{
		ch = getchar();
		if (ch == '-')
		{
			f = -f;
		}
	}
	while ((ch >= '0') && (ch <= '9'))
	{
		res = res * 10 + ch - '0';
		ch = getchar();
	}
	
	return f * res;
}

template <typename t>

t fastread_unsigned_intergar(void)
{
	t res = 0;
	char ch = getchar();
	
	while ((ch < '0') || (ch > '9'))
	{
		ch = getchar();
	}
	while ((ch >= '0') && (ch <= '9'))
	{
		res = res * 10 + ch - '0';
		ch = getchar();
	}
	
	return res;
}

template <typename t>

t fastread_radix_intergar(int radix)
{
	t res = 0;
	int temp;
	short f = 1;
	char ch = getchar();
	
	while (((ch < '0') || (ch > '9')) && ((ch < 'A') || (ch > 'Z')) && ((ch < 'a') || (ch > 'z')))
	{
		ch = getchar();
		if (ch == '-')
		{
			f = -f;
		}
	}
	for ( ; ; )
	{
		if ((ch >= '0') && (ch <= '9'))
		{
			temp = ch - '0';
		}
		else if ((ch >= 'A') && (ch <= 'Z'))
		{
			temp = ch - 'A' + 10;
		}
		else if ((ch >= 'a') && (ch <= 'z'))
		{
			temp = ch - 'a' + 10;
		}
		else
		{
			break;
		}
		res = res * radix + temp;
		ch = getchar();
	}
	
	return f * res;
}

template <typename t>

t fastread_unsigned_radix_intergar(int radix)
{
	t res = 0;
	int temp;
	char ch = getchar();
	
	while (((ch < '0') || (ch > '9')) && ((ch < 'A') || (ch > 'Z')) && ((ch < 'a') || (ch > 'z')))
	{
		ch = getchar();
	}
	for ( ; ; )
	{
		if ((ch >= '0') && (ch <= '9'))
		{
			temp = ch - '0';
		}
		else if ((ch >= 'A') && (ch <= 'Z'))
		{
			temp = ch - 'A' + 10;
		}
		else if ((ch >= 'a') && (ch <= 'z'))
		{
			temp = ch - 'a' + 10;
		}
		else
		{
			break;
		}
		res = res * radix + temp;
		ch = getchar();
	}
	
	return res;
}

template <typename t>

void __fastwrite_intergar(t x)
{
	if (x)
	{
		__fastwrite_intergar(x / 10);
		putchar(x % 10 + '0');
	}
	
	return;
}

template <typename t>

void fastwrite_intergar(t x)
{
	if (x < 0)
	{
		putchar('-');
		__fastwrite_intergar(-x);
	}
	else if (x)
	{
		__fastwrite_intergar(x);
	}
	else
	{
		putchar('0');
	}
	
	return;
}

template <typename t>

void __fastwrite_radix_intergar(t x, int radix)
{
	t temp;
	
	if (x)
	{
		__fastwrite_radix_intergar(x / radix, radix);
		temp = x % radix;
		if (temp < 10)
		{
			putchar(temp + '0');
		}
		else
		{
			putchar(temp + 'A' - 10);
		}
	}
	
	return;
}

template <typename t>

void fastwrite_radix_intergar(t x, int radix)
{
	if (x < 0)
	{
		putchar('-');
		__fastwrite_radix_intergar(-x, radix);
	}
	else if (x)
	{
		__fastwrite_radix_intergar(x, radix);
	}
	else
	{
		putchar('0');
	}
	
	return;
}

#endif