#pragma once
#include <iostream>
#include <cstdio>
#include <cctype>
#include <stdlib.h>
using namespace std;
typedef long long LL;
class QIO { //常规的快读和快输
public:
//利用fread加快读取速度
QIO();
~QIO();
LL read(); //stdin读入LL
void read(char *str); //stdin读入char*
void getinfile(FILE *file); //读入文件
void closeinfile(); //关闭文件
int getc(); //getchar
LL readlld(); //文件读入LL
LL readnum(int l); //文件读入指定长度number
//利用fwrite加快输出速度
void getoutfile(FILE *file) //读入输出文件
{
filp = file;
}
void closeoutfile() //关闭输出文件
{
fclose(filp);
}
void flush(); //fwrite输出
void write(LL x, char ch); //文件输出LL
void write(char *s, char ch); //文件输出char*
private:
char buf[1 << 21], *p1, *p2; //fread串
FILE *fp; //读入文件指针
char BUF[1 << 21], outnum[20]; //fwrite串
int Size = -1, p;
FILE *filp; //输出文件指针
};
#include "QIO.h"
QIO::QIO()
{
Size = -1;
p = 0;
p1 = p2 = buf;
fp = filp = NULL;
}
QIO::~QIO()
{
delete p1;
delete p2;
delete fp;
delete filp;
}
LL QIO::read()
{
LL ret = 0, f = 1;
char ch = getchar();
while (ch<'0' || ch>'9')
{
if (ch == '-')
f = -f;
ch = getchar();
}
while (ch >= '0'&&ch <= '9')
ret = ret * 10 + ch - '0',
ch = getchar();
return ret * f;
}
void QIO::read(char *str)
{
char ch; int g = 0;
ch = getchar();
while (ch == ' ' || ch == '\n')
ch = getchar();
while (ch != '\n')
{
str[g++] = ch;
ch = getchar();
}
str[g] = '\0';
}
void QIO::getinfile(FILE *file)
{
if (!file)
{
printf("文件打开失败,请重试.\n");
exit(1);
}
fp = file;
}
void QIO::closeinfile()
{
fclose(fp);
}
int QIO::getc()
{
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, fp), p1 == p2) ? EOF : *p1++;
}
LL QIO::readlld() {
LL ret = 0, f = 0;
char ch = getc();
while (!isdigit(ch)) {
if (ch == '-')
f = 1;
ch = getc();
}
while (isdigit(ch)) {
ret = ret * 10 + ch - 48;
ch = getc();
}
return f ? -ret : ret;
}
LL QIO::readnum(int l) {
char ch = getc();
while (!isdigit(ch))
ch = getc();
LL ret = ch - 48;
for (int i = 1; i < l; ++i)
{
ch = getc();
ret = ret * 10 + ch - 48;
}
return ret;
}
void QIO::flush()
{
fwrite(BUF, 1, Size + 1, filp);
Size = -1;
}
void QIO::write(LL x, char ch) {
if (Size > 1 << 20) flush();
if (x < 0) BUF[++Size] = 45, x = -x;
do {
outnum[++p] = x % 10 + 48;
} while (x /= 10);
do {
BUF[++Size] = outnum[p];
} while (--p);
BUF[++Size] = ch;
}
void QIO::write(char *s, char ch) {
if (Size > 1 << 20) flush();
int i = 0;
while (s[i] != '\0') {
BUF[++Size] = s[i];
i++;
}
BUF[++Size] = ch;
}
QIO
最新推荐文章于 2023-06-05 08:00:00 发布