void rank(){//排序
FILE *f1,*f2,*f3,*f4;
f2 = fopen("temp.txt","w");//代码自动生成的辅助文件
f1 = fopen("test.txt", "r");//原文件
char a[100][100];//可以自行调整大小
char buf[100];
int ii=0;
int index=0;
while((fgets(buf,sizeof(buf),f1))!=NULL){
fputs(buf,f2);
}
fclose(f1);
fclose(f2);
f3 = fopen("temp.txt","r");
f4 = fopen("test.txt","w");
while(f3 !=NULL){
if((fgets(buf,sizeof(buf),f3))!=NULL)//整行读出并保存到buf的字符数组
{
buf[strlen(buf)-1]='\0';
strcpy(a[ii],buf);
ii++;
index++;//计算有多少行
}
else{
break;
}
}
int c1,c2,c3,c4,d1,d2,d3,d4;
int sum1,sum2;
char s[100]={0};
for(int i=0;i<index;i++){
c4=(a[i][0]-48)*1000;
c3=(a[i][1]-48)*100;
c2=(a[i][2]-48)*10;
c1=(a[i][3]-48);
sum1=c1+c2+c3+c4;
for(int j=0;j<index;j++){
d4=(a[j][0]-48)*1000;
d3=(a[j][1]-48)*100;
d2=(a[j][2]-48)*10;
d1=(a[j][3]-48);
sum2=d1+d2+d3+d4;
if(sum1<sum2){//从小到大
strcpy(s,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],s);
}
}
}
for(int iii=0;iii<=index;iii++){
fputs(a[iii],f4);
fputc('\n',f4);
}
fclose(f3);
fclose(f4);
system("ERASE temp.txt");//删除辅助文档
printf("排序完成\n");
system("pause");
}