★題目71
函數(shù)readdat()實(shí)現(xiàn)從文件in.dat中讀取20行數(shù)據(jù)存放到字符串?dāng)?shù)組xx中(每行字符串長(zhǎng)度均小于80)。請(qǐng)編制函數(shù)jssort(),其函數(shù)的功能是:以行為單位對(duì)字符串按給定的條件進(jìn)行排序,排序后的結(jié)果仍按行重新存入字符串?dāng)?shù)組xx中,最后調(diào)用函數(shù)writedat()把結(jié)果xx輸出到文件out.dat中。
條件:從字符串中間一分為二,左邊部分按字符的ascii值降序排序,右邊部分按字符的ascii值升序排序。如果原字符串長(zhǎng)度為奇數(shù),則最中間的字符不參加排序,字符仍放在原位置上。
例如:位置 0 1 2 3 4 5 6 7 8
源字符串 a b c d h g f e
1 2 3 4 9 8 7 6 5
則處理后字符串 d c b a e f g h
4 3 2 1 9 5 6 7 8
部分源程序存在文件prog1.c中。
請(qǐng)勿改動(dòng)主函數(shù)main()、讀數(shù)據(jù)函數(shù)readdat()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
#include
#include
#include
char xx[20][80];
void jssort()
{int i,j,k,strl,half;
char ch;
for(i=0;i<20;i++)
{ strl=strlen(xx[i]);
half=strl/2;
for(j=0;j for(k=j+1;k if(xx[i][j] if(strl%2) half++;
for(j=half;j for(k=j+1;k if(xx[i][j]>xx[i][k]) {ch=xx[i][j];xx[i][j]=xx[i][k];xx[i][k]=ch;}
}
}
void main()
{
readdat();
jssort();
writedat();
}
readdat()
{
file *in;
int i=0;
char *p;
in=fopen("in.dat","r");
while(i<20&&fgets(xx[i],80,in)!=null){
p=strchr(xx[i],'\n');
if(p)*p=0;
i++;
}
fclose(in);
}
writedat()
{
file *out;
int i;
clrscr();
out=fopen("out.dat","w");
for(i=0;i<20;i++){
printf("%s\n",xx[i]);
fprintf(out,"%s\n",xx[i]);
}
fclose(out);
}
函數(shù)readdat()實(shí)現(xiàn)從文件in.dat中讀取20行數(shù)據(jù)存放到字符串?dāng)?shù)組xx中(每行字符串長(zhǎng)度均小于80)。請(qǐng)編制函數(shù)jssort(),其函數(shù)的功能是:以行為單位對(duì)字符串按給定的條件進(jìn)行排序,排序后的結(jié)果仍按行重新存入字符串?dāng)?shù)組xx中,最后調(diào)用函數(shù)writedat()把結(jié)果xx輸出到文件out.dat中。
條件:從字符串中間一分為二,左邊部分按字符的ascii值降序排序,右邊部分按字符的ascii值升序排序。如果原字符串長(zhǎng)度為奇數(shù),則最中間的字符不參加排序,字符仍放在原位置上。
例如:位置 0 1 2 3 4 5 6 7 8
源字符串 a b c d h g f e
1 2 3 4 9 8 7 6 5
則處理后字符串 d c b a e f g h
4 3 2 1 9 5 6 7 8
部分源程序存在文件prog1.c中。
請(qǐng)勿改動(dòng)主函數(shù)main()、讀數(shù)據(jù)函數(shù)readdat()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
#include
#include
#include
char xx[20][80];
void jssort()
{int i,j,k,strl,half;
char ch;
for(i=0;i<20;i++)
{ strl=strlen(xx[i]);
half=strl/2;
for(j=0;j for(k=j+1;k if(xx[i][j] if(strl%2) half++;
for(j=half;j for(k=j+1;k if(xx[i][j]>xx[i][k]) {ch=xx[i][j];xx[i][j]=xx[i][k];xx[i][k]=ch;}
}
}
void main()
{
readdat();
jssort();
writedat();
}
readdat()
{
file *in;
int i=0;
char *p;
in=fopen("in.dat","r");
while(i<20&&fgets(xx[i],80,in)!=null){
p=strchr(xx[i],'\n');
if(p)*p=0;
i++;
}
fclose(in);
}
writedat()
{
file *out;
int i;
clrscr();
out=fopen("out.dat","w");
for(i=0;i<20;i++){
printf("%s\n",xx[i]);
fprintf(out,"%s\n",xx[i]);
}
fclose(out);
}