題目88(無憂id 66 字符串排序題)
函數(shù)ReadDat()實(shí)現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串?dāng)?shù)組xx中,請(qǐng)編制函數(shù)SortCharA(),其函數(shù)的功能是:以行為單位對(duì)字符按從小到大的順序進(jìn)行排序,排序后的結(jié)果仍按行重新存入字符串?dāng)?shù)組xx中,最后調(diào)用函數(shù)writeDat()把結(jié)果xx輸出到文件OUT1.DAT中。
例:原文:dAe,BfC.
CCbbAA
結(jié)果:,.ABCdef
AACCbb
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個(gè)字符,含標(biāo)點(diǎn)符號(hào)和空格。
部分源程序存在文件prog1.c中。
請(qǐng)勿改動(dòng)主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void SortCharA(void)
{int i,j,k,strl;
char ch;
for(i=0;i {strl=strlen(xx[i]);
for(j=0;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()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件IN.DAT不能打開!\n\007");
return;
}
SortCharA();
WriteDat();
}
int ReadDat(void)
{
FILE *fp;
int i=0;
char *p;
if((fp=fopen("IN.DAT","r"))==NULL) return 1;
while(fgets(xx[i],80,fp)!=NULL){
p=strchr(xx[i],'\n');
if(p)*p=0;
i++;
}
maxline=i;
fclose(fp);
return 0;
}
void WriteDat(void)
{
FILE *fp;
int i;
clrscr();
fp=fopen("OUT1.DAT","w");
for(i=0;i printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
函數(shù)ReadDat()實(shí)現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串?dāng)?shù)組xx中,請(qǐng)編制函數(shù)SortCharA(),其函數(shù)的功能是:以行為單位對(duì)字符按從小到大的順序進(jìn)行排序,排序后的結(jié)果仍按行重新存入字符串?dāng)?shù)組xx中,最后調(diào)用函數(shù)writeDat()把結(jié)果xx輸出到文件OUT1.DAT中。
例:原文:dAe,BfC.
CCbbAA
結(jié)果:,.ABCdef
AACCbb
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個(gè)字符,含標(biāo)點(diǎn)符號(hào)和空格。
部分源程序存在文件prog1.c中。
請(qǐng)勿改動(dòng)主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void SortCharA(void)
{int i,j,k,strl;
char ch;
for(i=0;i
for(j=0;j
{ch=xx[i][j]; xx[i][j]=xx[i][k]; xx[i][k]=ch;}
}
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件IN.DAT不能打開!\n\007");
return;
}
SortCharA();
WriteDat();
}
int ReadDat(void)
{
FILE *fp;
int i=0;
char *p;
if((fp=fopen("IN.DAT","r"))==NULL) return 1;
while(fgets(xx[i],80,fp)!=NULL){
p=strchr(xx[i],'\n');
if(p)*p=0;
i++;
}
maxline=i;
fclose(fp);
return 0;
}
void WriteDat(void)
{
FILE *fp;
int i;
clrscr();
fp=fopen("OUT1.DAT","w");
for(i=0;i
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}