首页 > 学院 > 开发设计 > 正文

<The C programming language> 5.11 将其他一些选项增加到排序程序中

2019-11-08 02:28:01
字体:
来源:转载
供稿:网友

Version 1: 增加-r标记,以逆序(递减)方式排序。要保证-r和-n能够组合在一起使用。

#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAXLINES 5000#define MAXLEN 1000char *lineptr[MAXLINES];int readlines(char *lineptr[], int nlines);void writelines(char *lineptr[], int nlines);void qsort1(void *lineptr[], int left, int right, int (*comp)(void *, void *));int numcmp(char *, char *);void reverse(void *lineptr[], int nlines);int main(int argc, char *argv[]){	int nlines, c;	int numeric = 0; // 1 = numerical sort	int decrease = 0; // 1 = nonincreasing sort	int error = 0;	while (--argc > 0){		if ((*++argv)[0] != '-'){			PRintf("ERROR: Parameters should start with '-'./n");			error = -1;		}		else{			while (c = *++argv[0])				switch (c){					case 'n':						numeric = 1;						break;					case 'r':						decrease = 1;						break;					default:						printf("ERROR: No such '%c' parameters./n", c);						error = -2;						break;				}		}	}		if (error){		printf("Usage:/nmytest/nmytest -n -r/nmytest -r -n/nmytest -rn/nmytest -nr/n");		return error;	}	else{		if ((nlines = readlines(lineptr, MAXLINES)) >= 0)			qsort1((void **) lineptr, 0, nlines - 1,							(numeric ? (int (*)(void *, void *))numcmp : (int (*)(void *, void *))strcmp));		else{			printf("input too big to sort/n");			return 1;		}		if (decrease)			reverse(lineptr, nlines); //reverse the sort procedure		writelines(lineptr, nlines);		printf("These lines are sorted %s with %s order./n",							numeric ? "numerically" : "alphabetically",								decrease ? "nonincreasing" : "nondecreasing");		return 0;	}}void qsort1(void *v[], int left, int right, int (*comp)(void *, void *)){	int i, last;	void swap(void *v[], int, int);	if (left >= right)		return;	swap(v, left, (left + right)/2);	last = left;	for(i = left + 1;i <= right;i++)		if ((*comp)(v[i], v[left]) < 0)			swap(v, ++last, i);	swap(v, left, last);	qsort1(v, left, last-1, comp);	qsort1(v, last+1, right, comp);}int numcmp(char *s1, char *s2){	double v1, v2;	v1 = atof(s1);	v2 = atof(s2);	if (v1 < v2)		return -1;	else if (v1 > v2)		return 1;	else		return 0;}void swap(void *v[], int i, int j){	void *temp;	temp = v[i];	v[i] = v[j];	v[j] = temp;}int getline1(char *, int);char *alloc(int);void afree(char *);int readlines(char *lineptr[], int maxlines){	int len, nlines;	char *p, line[MAXLEN];	nlines = 0;	while ((len = getline1(line, MAXLEN)) > 0)		if (nlines >= maxlines || (p = alloc(len)) == NULL)			return -1;		else{			line[len-1] = '/0';			strcpy(p, line);			lineptr[nlines++] = p;//			afree(p);		}	return nlines;}void writelines(char *lineptr[], int nlines){	while (nlines -- > 0)		printf("%s/n", *lineptr++);}int getline1(char *line, int maxlen){	int c, i;	i = 0;	while (--maxlen > 0 && (c = getchar()) != EOF && c != '/n')		line[i++] = c;	if (c == '/n')		line[i++] = c;	line[i] = '/0';	return i;}#define ALLOCSIZE 10000static char allocbuf[ALLOCSIZE];static char *allocp = allocbuf;char *alloc(int n){	if (allocbuf + ALLOCSIZE - allocp >= n){		allocp += n;		return allocp - n;	}else		return 0;}void afree(char *p){	if (p >= allocbuf && p < allocbuf + ALLOCSIZE)		allocp = p;}void reverse(void *lineptr[], int nlines){	void swap(void *v[], int start, int end);	int i = 0;	nlines--;	while (i < nlines)		swap((void**) lineptr, i++, nlines--);}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表