下面是小編為大家整理的字符按從小到大順序(全文),供大家參考。
字符按從小到大的順序. txt 熬夜, 是因為沒有勇氣結束這一天; 賴床, 是因為沒有勇氣開始這一天。
朋友, 就是將你看透了 還能喜歡你的人。
函數 ReadDat( ) 的功能是實現從文件IN73. DAT 中讀取一篇英文文章存入到字符串數組 xx 中。
請編制函數 SortCharA( ) , 該函數的功能是:
以行為單位對字符按從小到大的順序進行排序, 排序后的結果仍按行重新存入字符串數組 xx 中。
最后調用函數 WriteDat( ) 把結果 xx 輸出到文件 OUT73. DAT 中。
例如, 原文:
dAe, BfC CCbbAA 結果:
ABCdef AACCbb 原始數據文件存放的格式是:
每行的寬度均小于 80 個字符, 含標點符號和空格。
注意:
部分源程序已給出。
請勿改動主函數 main( ) 、 讀函數 ReadDat( ) 和寫函數 WriteDat( ) 的內容。
試題程序:
#include<stdio. h> #include<string. h> #include<stdlib. h>
char xx[50][80]; int maxline=0;
int ReadDat(void) ; void WriteDat(void) ;
void SortCharA()
{
int i, j, k;
/*定義循環控制變量*/
int str;
/*存儲字符串的長度*/
char temp;
/*數據交換時的暫存變量*/
for (i=0; i<maxline; i++)
/*以行為單位獲取字符*/
{
str=strlen(xx[i]) ;
/*求得當前行的字符串長度*/
for(j=0; j<str-1; j++)
/*對字符按從小到大的順序進行排序*/
for(k=j+1;k<str; k++)
if (xx[i][j]>xx[i][k])
{
temp=xx[i][j];
xx[i][j]=xx[i][k];
xx[i][k]=temp;
}
}
}
void main()
{
system("CLS") ;
if (ReadDat() )
{
printf("數據文件 IN73. DAT 不能打開!\n\007") ;
return;
}
SortCharA() ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp;
int i=0;
char *p;
if((fp=fopen("IN73. 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()
{
FILE *fp;
int i;
system("CLS") ;
fp=fopen("OUT73. DAT", "w") ;
for(i=0; i<maxline; i++)
{
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}