声æä¸ä¸ªå符串æéæ°ç»åæ¾æ¯ä¸ªå符串çé¦å°åï¼è°ç¨åºå½æ°qusortæé¢ç®è¦æ±å¯¹å符串æéæåºï¼ä¸ç§»å¨æºå符串ãå
³é®æ¯è¦è®¾è®¡ä¸ä¸ªå¥½çæ¯è¾å½æ°ï¼ç²¾å·§å°è§£å³âæé¿åº¦ãé¿åº¦ç¸çæ¶æ大å°âæåºçé®é¢ã举ä¾ä»£ç å¦ä¸ï¼
//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"#include "string.h"#include "stdlib.h"#define N 10 //å符串个æ°#define LN 21 //éå¶å符串é¿åº¦ä¸º20int mycmp(const void *a,const void *b){//æ¯è¾å½æ° char *pa=*(char **)a,*pb=*(char **)b; int x=int(strlen(pa)-strlen(pb));//ä¾é¿åº¦æ¯è¾ return x ? x : strcmp(pa,pb);//é¿åº¦ç¸çæ¶ä¾å¤§å°æ¯è¾}int main(void){ int i=0,j=0; char *f[N],w[LN*N];//声ææéæ°ç»fåå符串æ»ç©ºé´ printf("Input %d string(s)(length<=%d)...\n",N,LN); while(i<N){//è¾å
¥å¹¶å°å符串é¦åèµç»f[i] if(scanf(" %[1234567890]",f[i]=w+j)>0 && strlen(f[i])<LN) i++,j+=LN; else printf("Error, redo: Required length less than %d:",LN); } qsort(f,N,sizeof(char *),mycmp);//è°ç¨åºå½æ°å¯¹å符串æéæåº for(i=0;i<N;printf("%s\n",f[i++]));//è¾åº... return 0;}
温馨提示:内容为网友见解,仅供参考