本帖最后由 Gin_Q 于 2020-2-26 09:56 编辑
- //Dev-C++ 5.11
- //排序算法用的是选择排序
- //by Gin
-
- #include <time.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define LINE_SIZE 100 //行存储大小
-
-
- FILE *fp(char *,const char *);
- int check_width(FILE *fp);
- int check_high(FILE *fp);
-
- int main(int argc,char *argv[])
- {
- clock_t start_t,end_t; //计时
- double time_sum; //储存时间
- start_t=clock(); //开始计时
-
- FILE *a_txt=fp(argv[1],"r");
- FILE *b_txt=fp(argv[2],"w");
-
- int width=0,high=0;
- width=check_width(a_txt);
- high=check_high(a_txt);
-
- void anay(char *,char *,int linesize,int width); //把二维素组做一维数组传递
- void init(char *,int *,int linesize,int width); //把二维素组做一维数组传递
- void sort_min_max(int *p,int high,int width,int n); //小到大
- void sort_max_min(int *p,int high,int width,int n); //大到小
- void res_print(FILE *fp,int *p,int high,int width,char *p_kemu,char *kemu,char *way,char *form,char *form_1,char *form_2); //打印结果
-
- char mu[LINE_SIZE]="0"; //储存第一行数据
- char temp_1[high][width]={"0"}; //存储分割好的数据
- char temp[LINE_SIZE]="0"; //存储一行数据
- int arr[high][width]={0}; //存储最终数组
- int (*p_arr)[width]=arr;
- char kemu[5][7]={"0"};
- char *p_kemu=kemu[0];
-
- char original[40]="原数据";
- char min_m[7]="小到大";
- char max_m[7]="大到小";
- char form[8]="+-----+";
- char form_1[8]="-----+";
- char form_2[2]="|";
- char spk[2]="\0";
-
- rewind(a_txt);
- fgets(mu,LINE_SIZE,a_txt);
- anay(mu,(char*)kemu,7,width);
-
- int i;
- for (i=0;fgets(temp,LINE_SIZE,a_txt)!=NULL;i++)
- {
- anay(temp,temp_1[0],width,width); //将数据分割成二维字符串
- init(temp_1[0],arr[i],width,width); //将二维字符串赋值给二维数组
- }
-
- res_print(b_txt,(int *)arr,high,width,p_kemu,spk,original,form,form_1,form_2);
- fputc('\n',b_txt);
-
- int mark=0; //0代表升序,1代表降序
-
- for (i=0;i<width;i++)
- {
- if (mark==0)
- {
- sort_min_max((int *)arr,high,width,i);
- mark=1;
- res_print(b_txt,(int *)arr,high,width,p_kemu,kemu[i],min_m,form,form_1,form_2);
- }
- else if (mark==1)
- {
- sort_max_min((int *)arr,high,width,i);
- mark=0;
- res_print(b_txt,(int *)arr,high,width,p_kemu,kemu[i],max_m,form,form_1,form_2);
- }
- if (i<(high-1)) fputs("\n",b_txt);
- }
- end_t=clock();
- time_sum=(double)(end_t-start_t)/CLOCKS_PER_SEC;
- fprintf(b_txt,"程序耗时:%f",time_sum);
- fclose(a_txt);
- fclose(b_txt);
- return 0;
-
-
- }
- FILE *fp(char *p,const char *mode)
- {
- FILE *pp;
- if ((pp= fopen(p,mode))==NULL)
- {
- printf("open fail! %s",p);
- getchar();
- exit (0);
- }
- return pp;
- }
- //检查数据
- int check_width(FILE *fp)
- {
- rewind(fp);
- char delims='0';
- int w=1;
- for (;(delims=fgetc(fp))!='\n';)
- {
- if (delims==' ') w++;
- }
- return w;
- }
- int check_high(FILE *fp)
- {
- rewind(fp);
- int h=0;
- char arr[LINE_SIZE];
- for (;fgets(arr,LINE_SIZE,fp)!=NULL;h++);
- return h-1; //去掉第一行不算
- }
- void anay(char *p,char *t,int linesize, int width)
- {
- int i,j=0,k=0; //j第二维,k第一维
- for (i=0;*(p+i)!='\0';i++)
- {
- if (*(p+i)!=' ' && *(p+i)!='\n')
- {
- *(t+j*linesize+k)=*(p+i);
- k++;
- }
- else
- {
- *(t+j*linesize+k)='\0';
- j++,k=0;
- }
- }
- }
- void init(char *p,int *p1,int linesize,int width)
- {
- int i;
- for (i=0;i<width;i++)
- {
- *(p1+i)=atoi(p+i*linesize);
- }
- }
- void sort_min_max(int *p,int high,int width,int n)
- {
- int i,k,l,temp=0;
- for (i=0;i<high-1;i++)
- {
- for (k=i+1;k<high;k++)
- {
- if (*(p+k*width+n)<*(p+i*width+n))
- {
- for (l=0;l<width;l++)
- {
- temp=*(p+k*width+l);
- *(p+k*width+l)=*(p+i*width+l);
- *(p+i*width+l)=temp;
- }
- }
- }
- }
- }
- void sort_max_min(int *p,int high,int width,int n)
- {
- int i,k,l,temp=0;
- for (i=0;i<high-1;i++)
- {
- for (k=i+1;k<high;k++)
- {
- if (*(p+k*width+n)>*(p+i*width+n))
- {
- for (l=0;l<width;l++)
- {
- temp=*(p+k*width+l);
- *(p+k*width+l)=*(p+i*width+l);
- *(p+i*width+l)=temp;
- }
- }
- }
- }
- }
- void res_print(FILE *fp,int *p,int high,int width,char *p_kemu,char *kemu,char *way,char *form,char *form_1,char *form_2)
- {
- fprintf(fp,"%s%s%s%s%s\n",form,form_1,form_1,form_1,form_1);
- fprintf(fp,"%s%6s%-23s%s\n",form_2,way,kemu,form_2);
- fprintf(fp,"%s%s%s%s%s\n",form,form_1,form_1,form_1,form_1);
- for (int i=0;i<width;i++) fprintf(fp,"%s%-5s",form_2,(p_kemu+i*7));
- fprintf(fp,"%s\n%s%s%s%s%s\n",form_2,form,form_1,form_1,form_1,form_1);
- int i,j;
- for (i=0;i<high;i++)
- {
- for (j=0;j<width;j++)
- {
- fprintf(fp,"%s%-5d",form_2,*(p+i*width+j));
- }
- fprintf(fp,"%s\n",form_2);
- fprintf(fp,"%s%s%s%s%s",form,form_1,form_1,form_1,form_1);
- fputc('\n',fp);
- }
- }
复制代码 结果:复制代码 |