我希望它讀進所以字串才開始排序,應該要怎樣修改才好?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compare(const void *prt1,const void *prt2){
return (strcmp((char *)prt1,(char *)prt2));
}
char str1[(int)1e5][103];
int main() {
int number_of_lines,i;
number_of_lines=0;
while(fgets(str1[number_of_lines],103,stdin)!=NULL){
number_of_lines++;
qsort(str1, number_of_lines, sizeof(char)*103,compare);
for( i=0;i<number_of_lines;i++)
printf("%s",str1[i]);}
return 0;
}
只要弄清楚「那一段要執行多次
;那一段只要執行一次
」就好
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compare(const void *prt1,const void *prt2){
return (strcmp((char *)prt1,(char *)prt2));
}
char str1[(int)1e5][103];
int main() {
int number_of_lines,i;
number_of_lines=0;
//讀進字串,多次
while(fgets(str1[number_of_lines],103,stdin)!=NULL) {
number_of_lines++;
}
//qsort,一次
qsort(str1, number_of_lines, sizeof(char)*103,compare);
//顯示結果,多次
for( i=0;i<number_of_lines;i++)
printf("%s",str1[i]);
return 0;
}