统计 【CSP2020-S 数据统计程序】

囧仙

2020-11-08 22:48:13

Personal

## 前言 闲得无聊,想要统计统计江苏省的选手程序。 然后我发现每个选手分别存储,还放在不同的文件夹里,看起来巨麻烦。于是我写了个代码用来将所有数据整合到一起。 ~~有点小问题,似乎程序会发生覆盖,导致一些程序末尾多出点东西。~~最新版已修复。 ## 源码 ```cpp #include<bits/stdc++.h> #define up(l,r,i) for(int i=l,END##i=r;i<=END##i;++i) #define dn(r,l,i) for(int i=r,END##i=l;i>=END##i;--i) using namespace std; const int NUM =736,SIZ=10240; //选手人数即最大源代码长度 const char PR[]="JS"; //省份前缀 int S[NUM+3][4]; //选手文件大小 void clc(int id,int tp,char *fil){ static char cpp[SIZ]; if(freopen(fil,"r",stdin)){ if(S[id][tp]>10000) printf("===== The file %35s is too big!The size of it is %5.3lf KB.\n",fil,1.0*S[id][tp]/1000); else printf("===== Successfully open the file %35s.The size of it is %5.3lf KB.\n",fil,1.0*S[id][tp]/1000); fread(cpp,1,min(10000,S[id][tp]),stdin); //输入文件。 fwrite(cpp,1,min(10000,S[id][tp]),stdout); //这里实际只输出了前 10KB. if(S[id][tp]>10000) printf("...\n"); puts("=========="); } else { printf("===== Opening %35s Failed.File not found.\n",fil); } } void cnt(int id,int tp,char *fil){ if(freopen(fil,"r",stdin)){ char c; while((c=getchar())!=EOF) ++S[id][tp]; printf("--> The size of [%35s] is %5.3lf KB.\n",fil,1.0*S[id][tp]/1000); } else { S[id][tp]=0; printf("--> Opening %35s Failed.\n",fil); } } int I[NUM+3],T[NUM+3]; bool cmp(int a,int b){ return T[a]<T[b]; } int main(){ char fil[256],pre[256]; freopen("siz.txt","w",stdout); up(1,NUM,i){ sprintf(pre,"answers\\%s-%5d",PR,i); for(int j=0;pre[j];++j) if(pre[j]==' ') pre[j]='0'; printf("===== Now calculating %s...\n",pre+9); sprintf(fil,"%s\\julian\\julian.cpp",pre),cnt(i,0,fil); sprintf(fil,"%s\\zoo\\zoo.cpp",pre),cnt(i,1,fil); sprintf(fil,"%s\\call\\call.cpp",pre),cnt(i,2,fil); sprintf(fil,"%s\\snakes\\snakes.cpp",pre),cnt(i,3,fil); T[i]=S[i][0]+S[i][1]+S[i][2]+S[i][3]; } up(1,NUM,i) I[i]=i; sort(I+1,I+1+NUM,cmp); printf("The rank of size is below.\n"); up(1,NUM,i){ printf("rank=%5d,ID=%5d,total size=%8.3lf KB.\n",i,I[i],1.0*T[I[i]]/1000); printf("The size of julian is %8.3lf KB.\n",1.0*S[I[i]][0]/1000); printf("The size of zoo is %8.3lf KB.\n",1.0*S[I[i]][1]/1000); printf("The size of call is %8.3lf KB.\n",1.0*S[I[i]][2]/1000); printf("The size of snakes is %8.3lf KB.\n",1.0*S[I[i]][3]/1000); puts(""); } freopen("log.txt","w",stdout); up(1,NUM,i){ sprintf(pre,"answers\\%s-%5d",PR,i); for(int j=0;pre[j];++j) if(pre[j]==' ') pre[j]='0'; printf("---------- Now Opening %s's file(s).\n",pre+9); sprintf(fil,"%s\\julian\\julian.cpp",pre),clc(i,0,fil); sprintf(fil,"%s\\zoo\\zoo.cpp",pre),clc(i,1,fil); sprintf(fil,"%s\\call\\call.cpp",pre),clc(i,2,fil); sprintf(fil,"%s\\snakes\\snakes.cpp",pre),clc(i,3,fil); puts(""),puts(""); } return 0; } ``` ## 说明 - 使用方法:将程序放在 $\text{answers}$ 同级的文件夹下(不是 $\text{answers}$ 里),将 $\verb!NUM!$ 改成该省总人数,就能运行了。 - 程序会生成两个文件 $\verb!siz.txt!$ 和 $\verb!log.txt!$ 。前者统计了选手程序的大小,并进行了排名,后者统计了选手的程序(不超过 $10\text{KB}$ 的部分,多余的会被删除)。 - 基本使用方法看看就懂了