From 64603588bb6a64afdf29bfa137c4e73e14266b1a Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 7 Nov 2022 14:03:47 +0800 Subject: [PATCH] chore(judger/formatter): sync from uoj.ac --- judger/uoj_judger/run/formatter.cpp | 71 ++++++++++------------------- 1 file changed, 24 insertions(+), 47 deletions(-) diff --git a/judger/uoj_judger/run/formatter.cpp b/judger/uoj_judger/run/formatter.cpp index 6dcbf14..a4fa91c 100644 --- a/judger/uoj_judger/run/formatter.cpp +++ b/judger/uoj_judger/run/formatter.cpp @@ -1,55 +1,32 @@ -#include -#include -#include -#include -#include -using namespace std; -typedef long long LL; -int main() -{ - //freopen("1.in","r",stdin); - char c,last; - int nSpace=0,nR=0,first; - while(1) - { - last=c,c=getchar(); - if(c==EOF) - { - if(last!='\n') +#include +#include +#include +#include +#include +#include + +int main() { + int c = '?', last; + std::string buf; + while (true) { + last = c; + c = getchar(); + if (c == EOF) { + if (last != '\n') { putchar('\n'); + } break; - } - else if(c!='\r'&&c!=' ') - { - if(c!='\n'&&first==0) - { - for(int j=1;j<=nSpace;++j) - putchar(' '); - for(int j=1;j<=nR;++j) - putchar('\r'); + } else if (c == ' ' || c == '\r') { + buf.push_back((char)c); + } else { + if (!buf.empty()) { + if (c != '\n') { + printf("%s", buf.c_str()); + } + buf.clear(); } - else if(c!='\n') - { - for(int j=1;j<=nR;++j) - putchar('\r'); - for(int j=1;j<=nSpace;++j) - putchar(' '); - } - nSpace=nR=0; putchar(c); } - else if(c==' ') - { - ++nSpace; - if(nR==0) - first=0; - } - else - { - ++nR; - if(nSpace==0) - first=1; - } } return 0; }