#include <string.h>
#define MAX_NAME 20 //最大名字長度
#define MAX_NUM 100 //最大學生人數
void sort_bubble(char (*pc)[MAX_NAME],int n)//排序函數
{
int i,j;
char str[MAX_NAME];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(pc[i],pc[j])>0)
{
strcpy(str,pc[i]);
strcpy(pc[i],pc[j]);
strcpy(pc[j],str);
}
}
}
}
void display_name(char (*pc)[MAX_NAME],int n)//顯示這n個姓名
{
int i;
for(i=0;i<n;i++)
{
printf("%s\n",pc[i]);
}
}
int main()
{
int i=1,n;
char str[MAX_NUM][MAX_NAME];
printf("請輸入學生總數:");
scanf("%d",&n);
while(i<=n)
{
printf("請輸入第%d個學生姓名:",i);
scanf("%s",str[i++-1]);
}
printf("排序前的學生名單如下:\n");
display_name(str,n);
sort_bubble(str,n);
printf("排序後的學生名單如下:\n");
display_name(str,n);
return 0;
}
說明:
1:采用的是冒泡排序,用快速排序當然快些,但我忘了
2:已驗證通過,結果正確。
3:隨意輸入人數和姓名,只要數組不越界就沒有問題!
4:兩個函數:排序和顯示
5:給分+好評