C //练习 5-14 修改排序程序,使它能处理-r标记。该标记表明,以逆序(递减)方式排序。要保证-r和-n能够组合在一起使用。

C程序设计语言 (第二版) 练习 5-14

练习 5-14 修改排序程序,使它能处理-r标记。该标记表明,以逆序(递减)方式排序。要保证-r和-n能够组合在一起使用。

注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010

 

代码块:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLINES 5000
#define MAXLEN 1000
#define ALLOCSIZE 10000

static 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;
	}
}

char *lineptr[MAXLINES];

int getline(char *s, int lim){
   
	int c;
	char *t = s;

	while(--lim > 0 && (c = getchar()) != EOF && c != '\n'){
   
		*s++= c;
	}
	if(c == '\n'){
   
		*s++ = c;
	}
	*s = '\0';
	return s - t;
}

int readlines(char *lineptr[], int maxlines){
   
	int len, nlines;
	char *p, line[MAXLEN];

	nlines = 0;
	while((len = getline(line, MAXLEN)) > 0){
   
		if(nlines >= maxlines || (p = alloc(len)) == NULL){
   
			return -1;
		}
		else{
   
			line[len-1] = '\0';
			strcpy(p, line);
			lineptr[nlines++] = p;
		}
	}
	return nlines;
}

void writelines(char *lineptr[], int nlines){
   
	while(nlines-- > 0){
   
		printf("%s\n", *lineptr++);
	}
}

void swap(void *v[], int i, int j){
   
	void *temp;
	temp = v[i];
	v[i] = v[j];
	v[j] = temp;
}

int numcmp(const void *s1, const void *s2){
   
	double v1, v2;
	v1 = atof(*(const char **)s1);
	v2 = atof(*(const char **)s2);
	if(v1 < v2){
   
		return -1;
	}
	else if(v1 > v2){
   
		return 1;
	}
	else{
   
		return 0;
	}
}

void qsort(void *v[], int left, int right, int(*comp)(const void*, const void*), int sign){
   
	int i, last;

	if(left >= right){
   
		return;
	}
	swap(v, left, (left + right) / 2);
	last = left;
	for(i = left + 1; i <= right; i++){
   
		if(sign == 0){
   
			if((*comp)(v[i], v[left]) < 0){
   
				swap(v, ++last, i);
			}
		}
		if(sign == 1){
   
			if((*comp)(v[i], v[left]) > 0){
   
				swap(v, ++last, i);
			}
		}
	}
	swap(v, left, last);
	qsort(v, left, last - 1, comp, sign);
	qsort(v, last + 1, right, comp, sign);
}

int main(int argc, char *argv[]){
   
	int nlines;
	int numeric = 0;
	int sign = 0;

	if(argc > 1){
   
		if(strcmp(argv[1], "-n") == 0){
   
			numeric = 1;
			sign = 0;
		}
		if(strcmp(argv[1], "-n") == 0 && strcmp(argv[2], "-r") == 0){
   
			numeric = 1;
			sign = 1;
		}
	}

	if((nlines = readlines(lineptr, MAXLINES)) >= 0){
   
		qsort((void**)lineptr, 0, nlines - 1, (numeric ? numcmp : (int (*)(const void *,const void *))strcmp), sign);
		writelines(lineptr, nlines);
		system("pause");
		return 0;
	}
	else{
   
		printf("Error: input too big to sort!\n");
		system("pause");
		return 1;
	}

	system("pause");
	return 0;
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-13 06:04:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 06:04:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 06:04:05       87 阅读
  4. Python语言-面向对象

    2024-01-13 06:04:05       96 阅读

热门阅读

  1. VCG 网格平滑之Laplacian平滑

    2024-01-13 06:04:05       54 阅读
  2. 小米路由器有线中继模式设置固定IP

    2024-01-13 06:04:05       214 阅读
  3. 使用Docker部署PDF多功能工具Stirling-PDF

    2024-01-13 06:04:05       58 阅读
  4. c# ref和out参数修饰符

    2024-01-13 06:04:05       59 阅读
  5. 生成并压缩多个word文件,写入response

    2024-01-13 06:04:05       46 阅读
  6. jenkins设置Jenkinsfile的pipeline脚本 nohup运行

    2024-01-13 06:04:05       60 阅读
  7. 解决chromebook kabylake安装linux没有声音问题

    2024-01-13 06:04:05       60 阅读
  8. @KafkaListener指定kafka集群

    2024-01-13 06:04:05       51 阅读