當前位置:成語大全網 - 新華字典 - 編寫壹個書名排序程序,在主函數中輸入n(n<=10)個書名存入壹個二維數組

編寫壹個書名排序程序,在主函數中輸入n(n<=10)個書名存入壹個二維數組

//#include "stdafx.h"//vc++6.0加上這壹行.

#include "stdio.h"

#include "string.h"

void sort(char (*name)[20],int n){

int i,j,k;

char t[20];

for(i=0;i<n;i++){

for(k=i,j=k+1;j<n;j++)

if(strcmp(name[k],name[j])>0) k=j;

if(k!=i){

strcpy(t,name[k]);

strcpy(name[k],name[i]);

strcpy(name[i],t);

}

}

}

void print(char (*name)[20],int n){

int i;

for(i=0;i<n;printf("%s\n",name[i++]));

}

int main(void){

char name[10][20];

int n,i;

while(1){

printf("Input n(1~10)...\nn=");

scanf("%d",&n);

if(n>0 && n<11) break;

printf("Error,redo: ");

}

printf("Type the name of %d books...\n",n);

for(i=0;i<n;fflush(stdin),gets(name[i++]));

sort(name,n);

printf("\n");

print(name,n);

return 0;

}