排序问题
一个数据文件如下:abd12ef
cf3205g
egi08pk
gop66hi
pfr52lf
ope48pk
adc68p6
vbg32lo
我想以每个字符串的4、5位为关键字进行升序排序,排序后的结果应该是:
egi08pk
abd12ef
cf3205g
vbg32lo
ope48pk
pfr52lf
gop66hi
adc68p6
可我却得不到正确结果,我的代码如下:
#include "stdafx.h"
#include<stdio.h>
#include "string.h"
#include "stdlib.h"
#define LENGTH 1000
//void paixu(int shuju[], int shangbiao, int xiabiao);
void paixu(int shuju[], int shangbiao, int xiabiao,char str[][]);
void main()
{
int i,n;
int shuju[LENGTH];
FILE *fi;
fi=fopen("test.txt","r");
char str[LENGTH][100];
char strr[100];
int linenum=0;
char b[LENGTH][100];
if(fi==NULL)
{
printf("can't open test.txt to read!\n");
exit(0);
}
for(i=0;i<8;i++)
{
fgets(strr,100,fi);
strcpy((char*)str[i],strr);
linenum++;
}
for(i=0;i<8;i++)
{
printf("%s\n",str[i]);
}
printf("共有%d行\n",linenum);
fclose(fi);
for(i=0;i<linenum;i++)
{
strncpy(b[i],str[i]+3,2);
b[i][5]=0;
printf("%s\n",b[i]);//12345
}
for(i=0;i<linenum;i++)
{
shuju[i]=atoi((char*)b[i]);
printf("%d\n",shuju[i]);
}
paixu(shuju,0,linenum-1,str[8][100]);
printf("排序后:\n");
for(i=0;i<linenum;i++) printf("%d\n ",str[i]);
putchar('\n');
getchar();
getchar();
}
//void paixu(int shuju[], int shangbiao, int xiabiao)
void paixu(int shuju[], int shangbiao, int xiabiao,char str[][]);
{
int i=shangbiao,j=xiabiao;
int shu=i;
int zhongjie;
char bb[100];
if(shangbiao<xiabiao)
{
while(i<j)
{
for(;j>shu;j--)
{
if(shuju[j]<shuju[shu])
{
zhongjie=shuju[j];
shuju[j]=shuju[shu];
shuju[shu]=zhongjie;
bb=str[j];
str[j]=str[shu];
str[shu]=bb;
shu=j;
break;
}
}
i++;
for(;i<shu;i++)
{
if(shuju[i]>shuju[shu])
{
zhongjie=shuju[i];
shuju[i]=shuju[shu];
shuju[shu]=zhongjie;
bb=str[i];
str[i]=str[shu];
str[shu]=bb;
shu=i;
break;
}
}
j--;
}
paixu(shuju,shangbiao,shu-1,str[][]);
paixu(shuju,shu+1,xiabiao,str[][]);
}
}
请问错在哪里,该如何改正?