对"black,green,yellow,pin,red"几个单词排序
01
#include <iostream.h> #include <string.h> void sort(char (*p)[20],int n) { char *q; char b[20]; q=b; int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(p[i],p[j])>0) { strcpy(q,*(p+i)); strcpy(*(p+i),*(p+j)); strcpy(*(p+j),q); } } } } void main() { char a[5][20]={"black","green","yellow","pin","red"}; sort(a,5); for(int i=0;i<5;i++) { cout<<a[i]<<endl; } }
02
#include <iostream.h> #include <string.h> void sort(char *p[],int n) { char *q; int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(*(p+i),*(p+j))>0) { q=*(p+i); *(p+i)=*(p+j); *(p+j)=q; } } } } void main() { char a[5][20]={"black","green","yellow","pin","red"}; char *q[5]; for(int i=0;i<5;i++) { q[i]=a[i]; } sort(q,5); for(i=0;i<5;i++) { cout<<q[i]<<endl; } }
03
#include <iostream.h> #include <string.h> void sort(char *p,int n) { char *q; char b[20]; q=b; int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(p+i*20,p+j*20)>0) { strcpy(q,p+i*20); strcpy(p+i*20,p+j*20); strcpy(p+j*20,q); } } } } void main() { char a[5][20]={"black","green","yellow","pin","red"}; char *q; q=&a[0][0]; sort(q,5); for(int i=0;i<5;i++) { cout<<a[i]<<endl; } }
04
#include <iostream.h> #include <string.h> void sort(char **p,int n) { char *q; int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(*(p+i),*(p+j))>0) { q=*(p+i); *(p+i)=*(p+j); *(p+j)=q; } } } } void main() { char a[5][20]={"black","green","yellow","pin","red"}; char *q[5]; for(int i=0;i<5;i++) { q[i]=a[i]; } sort(q,5); for(i=0;i<5;i++) { cout<<a[i]<<endl; } }
时间: 2024-10-17 19:31:16