-
/* writelines函数: 写输出行*/
-
void writelines(char *lineptr[],int nlines)
-
{
-
int i;
-
-
for(i = 0; i < nlines;++i)
-
}
-
-
/* swap函数:交换v[i]和v[j]*/
-
void swap(void *v[],int i, int j)
-
{
-
void *temp;
-
-
temp = v[i];
-
v[i] = v[j];
-
v[j] = temp;
-
}
-
续nqsort
Thu, 29 May 2008 04:48:05 +0800
YEASH!!!
Thu, 29 May 2008 04:37:59 +0800
刚发现有这样适合 programmer的BLOG!
就把我今天刚作的C语言的小练习方上来看看先~~
初学编程,练习是 The C programming language 里的
练习5-14
请指教...
-
/*对输入文本进行排序,
-
*nqsort -n -r
-
*其中参数 -n 是按数值排序, -r是逆序
-
* *exercise.5-13.in 《The C Programming Language》,page102 */
-
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
-
#define MAXLINES 5000
-
char *lineptr[MAXLINES];
-
-
int readlines(char *lineptr[],int nlines);
-
void writelines(char *lineptr[], int nlines);
-
void swap(void *v[],int i, int j);
-
-
void nqsort(void *lineptr[],int left, int right,int argument,
-
int (*comp)(void *, void *));
-
int numcmp(char *, char *);
-
-
int main(int argc, char **argv)
-
{
-
int c;
-
int nlines = 0;
-
int numeric = 0,reverse = 0;
-
-
while(--argc > 0 && *(++argv)[0] == '-')
-
while((c = *++argv[0]))
-
switch(c){
-
case 'n':
-
numeric = 1;
-
break;
-
case 'r':
-
reverse = 1;
-
break;
-
default:
-
argc = 0;
-
break;
-
}
-
if(argc != 0)
-
else{
-
if((nlines = readlines(lineptr,MAXLINES)) >= 0) {
-
nqsort((void **)lineptr,0,nlines-1,reverse,
-
(int (*)(void *, void *))(numeric ? numcmp : strcmp));
-
writelines(lineptr, nlines);
-
return 0;
-
}else {
-
return 1;
-
}
-
}
-
return 0;
-
}
-