C //练习 5-16 增加选项-d(代表目录顺序)。该选项表明,只对字母、数字和空格进行比较。要保证该选项可以和-f组合在一起使用。

本文主要是介绍C //练习 5-16 增加选项-d(代表目录顺序)。该选项表明,只对字母、数字和空格进行比较。要保证该选项可以和-f组合在一起使用。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

练习 5-16 增加选项-d(代表目录顺序)。该选项表明,只对字母、数字和空格进行比较。要保证该选项可以和-f组合在一起使用。

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

 

代码块:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>#define MAXLINES 5000char *lineptr[MAXLINES];int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines, int reverse);
void q_sort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
int numcmp(char *, char *);
int strfcmp(char *, char*);
int strdcmp(char *, char *);
int strdfcmp(char *, char *);int main(int argc, char *argv[]) {int nlines;int numeric = 0;int reverse = 0;int fold = 0;int dir = 0;while (--argc > 0 && (*++argv)[0] == '-') {int c;while (c = *++argv[0])switch (c) {case 'n':numeric = 1;break;case 'r':reverse = 1;break;case 'f':fold = 1;break;case 'd':dir = 1;break;default:printf("Illegal option: %c\n", c);break;}}if (argc != 0)printf("Usage: sort -n -r -f\n");printf(numeric ? "Numerical sort\n" : "Lexicographic sort\n");printf(reverse ? "Reverse sort\n" : "Normal sort\n");printf(fold ? "Case insensitive\n" : "Case sensitive\n");printf(dir ? "Directory order\n" : "");if ((nlines = readlines(lineptr, MAXLINES)) > 0) {if (numeric)q_sort((void **) lineptr, 0, nlines - 1,(int (*)(void *, void *)) numcmp);else if (dir)if (fold)  q_sort((void **) lineptr, 0, nlines - 1,(int (*)(void *, void *)) strdfcmp);elseq_sort((void **) lineptr, 0, nlines - 1,(int (*)(void *, void *)) strdcmp);elseif (fold)q_sort((void **) lineptr, 0, nlines - 1,(int (*)(void *, void *)) strfcmp);elseq_sort((void **) lineptr, 0, nlines - 1,(int (*)(void *, void *)) strcmp);writelines(lineptr, nlines, reverse);return 0;} else {printf("input too big to sort\n");return 1;}system("pause");return 0;
}void q_sort(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);q_sort(v, left, last - 1, comp);q_sort(v, last + 1, right, comp);
}int strdcmp(char *s1, char *s2) {char a, b;do {while (!isalnum(*s1) && *s1 != ' ' && *s1 != '\0')s1++;while (!isalnum(*s2) && *s2 != ' ' && *s2 != '\0')s2++;a = *s1;s1++;b = *s2;s2++;if (a == b && a == '\0')return 0;} while (a == b);return a - b;}int strdfcmp(char *s1, char *s2) {char a, b;do {while (!isalnum(*s1) && *s1 != ' ' && *s1 != '\0')s1++;while (!isalnum(*s2) && *s2 != ' ' && *s2 != '\0')s2++;a = tolower(*s1);s1++;b = tolower(*s2);s2++;if (a == b && a == '\0')return 0;} while (a == b);return a - b;
}int strfcmp(char *s1, char *s2) {for ( ; tolower(*s1) == tolower(*s2); s1++, s2++)if (*s1 == '\0')return 0;return tolower(*s1)-tolower(*s2);
}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;elsereturn 0;
}void swap(void *v[], int i, int j) {void *temp;temp = v[i];v[i] = v[j];v[j] = temp;
}#define MAXLEN 1000int get_line(char line[], int maxline);
char *alloc(int);int readlines(char *lineptr[], int maxlines) {int len, nlines;char *p, line[MAXLEN];nlines = 0;while ((len = get_line(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, int reverse) {int i;if (reverse)for (i = nlines-1; i >= 0; i--)printf("%s\n", lineptr[i]);elsefor (i = 0; i < nlines; i++)printf("%s\n", lineptr[i]);
}int get_line(char s[], int lim) {int c, i;for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)s[i] = c;if (c == '\n') s[i++] = c;s[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;} elsereturn 0;
}void afree(char *p) {if (p >= allocbuf && p < allocbuf + ALLOCSIZE)allocp = p;
}

这篇关于C //练习 5-16 增加选项-d(代表目录顺序)。该选项表明,只对字母、数字和空格进行比较。要保证该选项可以和-f组合在一起使用。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/605232

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删