成绩单排序(多条件)挑战!
原文本语文 数学 英语 化学 生物
79 77 70 77 67
64 70 74 75 72
79 64 72 62 76
62 75 65 71 63
79 76 62 62 77
66 63 77 75 79
74 75 72 61 61
71 63 79 60 70
74 64 74 78 69//Dev-C++ 5.11
//排序算法用的是选择排序
//by Gin
#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[])
{
FILE *a_txt=fp(argv,"r");
FILE *b_txt=fp(argv,"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 *mu,char *p_kemu,char *kemu,char *way,char *form,char *form_1,char *form_2); //打印结果
char mu="0"; //储存第一行数据
char temp_1={"0"}; //存储分割好的数据
char temp="0"; //存储一行数据
int arr={0}; //存储最终数组
int (*p_arr)=arr;
char kemu={"0"};
char *p_kemu=kemu;
char original="原数据 ";
char min_m="小到大";
char max_m="大到小";
char form="+-----+";
char form_1="-----+";
char form_2="|";
char spk="\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,width,width); //将数据分割成二维字符串
init(temp_1,arr,width,width); //将二维字符串赋值给二维数组
}
res_print(b_txt,(int *)arr,high,width,mu,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,mu,p_kemu,kemu,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,mu,p_kemu,kemu,max_m,form,form_1,form_2);
}
if (i<(high-1)) fputs("\n",b_txt);
}
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;
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 *mu,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%s%-25s%s\n",form_2,kemu,way,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);
}
}
结果:
+-----+-----+-----+-----+-----+
|原数据 |
+-----+-----+-----+-----+-----+
|语文 |数学 |英语 |化学 |生物 |
+-----+-----+-----+-----+-----+
|79 |77 |70 |77 |67 |
+-----+-----+-----+-----+-----+
|64 |70 |74 |75 |72 |
+-----+-----+-----+-----+-----+
|79 |64 |72 |62 |76 |
+-----+-----+-----+-----+-----+
|62 |75 |65 |71 |63 |
+-----+-----+-----+-----+-----+
|79 |76 |62 |62 |77 |
+-----+-----+-----+-----+-----+
|66 |63 |77 |75 |79 |
+-----+-----+-----+-----+-----+
|74 |75 |72 |61 |61 |
+-----+-----+-----+-----+-----+
|71 |63 |79 |60 |70 |
+-----+-----+-----+-----+-----+
|74 |64 |74 |78 |69 |
+-----+-----+-----+-----+-----+
+-----+-----+-----+-----+-----+
|语文小到大 |
+-----+-----+-----+-----+-----+
|语文 |数学 |英语 |化学 |生物 |
+-----+-----+-----+-----+-----+
|62 |75 |65 |71 |63 |
+-----+-----+-----+-----+-----+
|64 |70 |74 |75 |72 |
+-----+-----+-----+-----+-----+
|66 |63 |77 |75 |79 |
+-----+-----+-----+-----+-----+
|71 |63 |79 |60 |70 |
+-----+-----+-----+-----+-----+
|74 |75 |72 |61 |61 |
+-----+-----+-----+-----+-----+
|74 |64 |74 |78 |69 |
+-----+-----+-----+-----+-----+
|79 |77 |70 |77 |67 |
+-----+-----+-----+-----+-----+
|79 |76 |62 |62 |77 |
+-----+-----+-----+-----+-----+
|79 |64 |72 |62 |76 |
+-----+-----+-----+-----+-----+
+-----+-----+-----+-----+-----+
|数学大到小 |
+-----+-----+-----+-----+-----+
|语文 |数学 |英语 |化学 |生物 |
+-----+-----+-----+-----+-----+
|79 |77 |70 |77 |67 |
+-----+-----+-----+-----+-----+
|79 |76 |62 |62 |77 |
+-----+-----+-----+-----+-----+
|62 |75 |65 |71 |63 |
+-----+-----+-----+-----+-----+
|74 |75 |72 |61 |61 |
+-----+-----+-----+-----+-----+
|64 |70 |74 |75 |72 |
+-----+-----+-----+-----+-----+
|74 |64 |74 |78 |69 |
+-----+-----+-----+-----+-----+
|79 |64 |72 |62 |76 |
+-----+-----+-----+-----+-----+
|71 |63 |79 |60 |70 |
+-----+-----+-----+-----+-----+
|66 |63 |77 |75 |79 |
+-----+-----+-----+-----+-----+
+-----+-----+-----+-----+-----+
|英语小到大 |
+-----+-----+-----+-----+-----+
|语文 |数学 |英语 |化学 |生物 |
+-----+-----+-----+-----+-----+
|79 |76 |62 |62 |77 |
+-----+-----+-----+-----+-----+
|62 |75 |65 |71 |63 |
+-----+-----+-----+-----+-----+
|79 |77 |70 |77 |67 |
+-----+-----+-----+-----+-----+
|74 |75 |72 |61 |61 |
+-----+-----+-----+-----+-----+
|79 |64 |72 |62 |76 |
+-----+-----+-----+-----+-----+
|74 |64 |74 |78 |69 |
+-----+-----+-----+-----+-----+
|64 |70 |74 |75 |72 |
+-----+-----+-----+-----+-----+
|66 |63 |77 |75 |79 |
+-----+-----+-----+-----+-----+
|71 |63 |79 |60 |70 |
+-----+-----+-----+-----+-----+
+-----+-----+-----+-----+-----+
|化学大到小 |
+-----+-----+-----+-----+-----+
|语文 |数学 |英语 |化学 |生物 |
+-----+-----+-----+-----+-----+
|74 |64 |74 |78 |69 |
+-----+-----+-----+-----+-----+
|79 |77 |70 |77 |67 |
+-----+-----+-----+-----+-----+
|64 |70 |74 |75 |72 |
+-----+-----+-----+-----+-----+
|66 |63 |77 |75 |79 |
+-----+-----+-----+-----+-----+
|62 |75 |65 |71 |63 |
+-----+-----+-----+-----+-----+
|79 |64 |72 |62 |76 |
+-----+-----+-----+-----+-----+
|79 |76 |62 |62 |77 |
+-----+-----+-----+-----+-----+
|74 |75 |72 |61 |61 |
+-----+-----+-----+-----+-----+
|71 |63 |79 |60 |70 |
+-----+-----+-----+-----+-----+
+-----+-----+-----+-----+-----+
|生物小到大 |
+-----+-----+-----+-----+-----+
|语文 |数学 |英语 |化学 |生物 |
+-----+-----+-----+-----+-----+
|74 |75 |72 |61 |61 |
+-----+-----+-----+-----+-----+
|62 |75 |65 |71 |63 |
+-----+-----+-----+-----+-----+
|79 |77 |70 |77 |67 |
+-----+-----+-----+-----+-----+
|74 |64 |74 |78 |69 |
+-----+-----+-----+-----+-----+
|71 |63 |79 |60 |70 |
+-----+-----+-----+-----+-----+
|64 |70 |74 |75 |72 |
+-----+-----+-----+-----+-----+
|79 |64 |72 |62 |76 |
+-----+-----+-----+-----+-----+
|79 |76 |62 |62 |77 |
+-----+-----+-----+-----+-----+
|66 |63 |77 |75 |79 |
+-----+-----+-----+-----+-----+
记事本上面对齐了!复制过来就变样了!{:5_94:} 强,请问一下,你大几了啊 最后的魁拔 发表于 2020-2-24 20:31
强,请问一下,你大几了啊
{:5_109:}谢谢夸奖!!{:5_101:} Cool_Breeze 发表于 2020-2-24 20:38
谢谢夸奖!!
老哥,你还没有回答我的问题呢,想向你取经
最后的魁拔 发表于 2020-2-24 20:41
老哥,你还没有回答我的问题呢,想向你取经
c语言学不懂了。。。
谭浩强那本一定要看!
页:
[1]