当前位置: 首页 > news >正文

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 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;}
}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;
}
http://www.lryc.cn/news/279190.html

相关文章:

  • CAN总线报文格式———标准数据帧
  • DFT中的SCAN、BIST、ATPG基本概念
  • 掌握 Vue 响应式系统,让数据驱动视图(下)
  • apache、nginx、php 隐藏版本号
  • sqoop的安装与使用
  • 【docker】Docker Stack 详细使用及注意事项
  • Android开发基础(四)
  • HTML5+CSS3+JS小实例:音频可视化
  • 【写作】短篇《相遇与相守》
  • 2024年最新软件测试面试题
  • instanceof、对象类型转化、static关键字
  • 学习笔记-python文件基本操作
  • 【Scala】——流程控制
  • imgaug库指南(20):从入门到精通的【图像增强】之旅
  • 最新AI绘画Midjourney绘画提示词Prompt大全
  • 编写一个简单的服务和客户端(C++)
  • InseRF: 文字驱动的神经3D场景中的生成对象插入
  • 类厂,变长参数,序列化
  • LLK的2023年度总结
  • Redis-浅谈redis.conf配置文件
  • 【liunx】线程池+单例模式+STL,智能指针和线程安全+其他常见的各种锁+读者写者问题
  • Golang的API项目快速开始
  • 机器学习_实战框架
  • Java8常用新特性
  • Go语言中的Channel
  • Unity中URP下实现深度贴花
  • openssl3.2 - 官方demo学习 - cipher - aesccm.c
  • 点云从入门到精通技术详解100篇-基于多传感器融合的智能汽车 环境感知(下)
  • 蓝桥杯单片机组备赛——蜂鸣器和继电器的基本控制
  • 嵌入式linux 编译qt5(以v851s为例)