本帖最后由 Gin_Q 于 2020-2-27 10:09 编辑
命令行用法:D:\GIN\c\test>csv分析.exe source.csv result.txt 1- //编译器 Dev-C++ 5.11
- //Rev 02
- //Author By Gin
- //增加打印格式
- //.cpp
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
-
-
- int main(int argv,char *argc[])
- {
- int delims(char *ser,char *res,int res_size);
- FILE *file_p(char *path,const char *mode);
- void form_printf(FILE *fp,char *item,double *sum,double *min,double *max_1,double *avg,int width,int T_max,int off);
- int check_width(FILE *fp);
- int check_high(FILE *fp);
-
- FILE *source_txt=file_p(argc[1],"r"); //源文件
- FILE *result_txt=file_p(argc[2],"w"); //结果
-
- int off=atoi(argc[3]); //输出结果格式
- int width=check_width(source_txt); //文件列数统计
- int high=check_high(source_txt); //文件行数统计
- int Max=10240,T_max=50;
- char temp[width][T_max]; //用于临时储存一行数据 (用于atof转换)
- char item[width][T_max]={"0"}; //储存数据项目名
- char line_size[Max]="0"; //储存一行数据空间
- double sum[width]={0}; //储存一行数据
- int nu_line=0; //用来储存文件一行数据个数
-
- rewind(source_txt);
- fgets(line_size,Max,source_txt);
- delims(line_size,item[0],T_max); //将第一行的标题单独存放
-
- int i=0,k=0,j=0,count=0;
- double min[width]={0},max_1[width]={0}; //最大值,最小值
- while (fgets(line_size,Max,source_txt)!=NULL)
- {
- count++;
- nu_line=delims(line_size,temp[0],T_max);
- for (i=1;*(temp[i])!='\0';i++) //(i=1)跳过第一列的数据
- {
- if (i<nu_line) sum[i-1]+=atof(temp[i]); //将每一项数据相加
- if (count==1)
- {
- min[i-1]=atof(temp[i]); //最少值(初始化)
- max_1[i-1]=atof(temp[i]); //最大值(初始化)
- }
- else
- {
- if (atof(temp[i]) > max_1[i-1]) max_1[i-1]=atof(temp[i]); //最大值
- if (atof(temp[i]) < min[i-1]) min[i-1]=atof(temp[i]); //最小值
- }
- }
- }
- double avg[width]={0}; //平均值
- for (i=0;i<width-1;i++)
- {
- if (i<nu_line-1) avg[i]=sum[i]/count; //最后一行如果小于width
- else
- {
- avg[i]=sum[i]/(count-1);
- }
- }
- form_printf(result_txt,item[0],sum,min,max_1,avg,width,T_max,off);
- fclose(source_txt);
- fclose(result_txt);
- return 0;
- }
- int check_width(FILE *fp)
- {
- rewind(fp);
- char t;
- int w=1;
- for (;(t=fgetc(fp))!='\n';)
- {
- if (t==',') w++;
- }
- return w;
- }
- int check_high(FILE *fp)
- {
- rewind(fp);
- int h=0,size=10240;
- char temp[size]="0";
- for (;fgets(temp,size,fp)!=NULL;h++);
- return h-1; //去掉第一行
- }
- //分割数据
- int delims(char *sou,char *res,int res_size)
- {
- int j=0,k=0,i=0,n=0;
- for (;sou[i]!='\0';i++)
- {
- if (sou[i]!=',' && sou[i]!=' ')
- {
- *(res+j*res_size+k)=sou[i];
- k++;
- }
- if (sou[i]==',')
- {
- *(res+j*res_size+k)='\0';
- j++,n++,k=0;
- }
- if (sou[i+1]=='\0') *(res+j*res_size+k-1)='\0'; //把最后一个符号替换
- }
- return n+1; //加上最后一行
- }
- FILE *file_p(char *path,const char *mode)
- {
- FILE *fp;
- if ((fp=fopen(path,mode))==NULL)
- {
- printf("%s open faile!",path);
- exit(0);
- }
- return fp;
- }
- void form_printf(FILE *fp,char *item,double *sum,double *min,double *max_1,double *avg,int width,int T_max,int off)
- {
- int i=0;
- if (off==1)
- {
- char form='+';
- char form_1='|';
- char form_2[22]="---------------------" ;
- char form_3[32]="-------------------------------" ;
- fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
- fprintf(fp,"%c %-30s%c %-20s%c %-20s%c %-20s%c %-20s%c\n",form_1,"Item",form_1,"Sum",form_1,"Min",form_1,"Max",form_1,"Avg",form_1);
- fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
- for (i=0;i<width-1;i++) fprintf(fp,"%c %-30s%c %-20f%c %-20f%c %-20f%c %-20f%c\n",form_1,item+i*T_max+T_max,form_1,sum[i],form_1,min[i],form_1,max_1[i],form_1,avg[i],form_1);
- fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
- }
- else if (off==2)
- {
- char form='+';
- char form_1='|';
- char form_2[22]="---------------------" ;
- char form_3[32]="-------------------------------" ;
- fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
- fprintf(fp,"%c %-30s%c %-20s%c %-20s%c %-20s%c %-20s%c\n",form_1,"Item",form_1,"Sum",form_1,"Min",form_1,"Max",form_1,"Avg",form_1);
- for (i=0;i<width-1;i++) fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n%c %-30s%c %-20f%c %-20f%c %-20f%c %-20f%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form,form_1,item+i*T_max+T_max,form_1,sum[i],form_1,min[i],form_1,max_1[i],form_1,avg[i],form_1);
- fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
- }
- else
- {
- fprintf(fp,"%-30s%-20s%-20s%-20s%-20s\n","Item","Sum","Min","Max","Avg");
- for (i=0;i<width-1;i++) fprintf(fp,"%-30s%-20f%-20f%-20f%-20f\n",item+i*T_max+T_max,sum[i],min[i],max_1[i],avg[i]);
- }
- }
复制代码
|