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

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

Nginx如何进行流量按比例转发

《Nginx如何进行流量按比例转发》Nginx可以借助split_clients指令或通过weight参数以及Lua脚本实现流量按比例转发,下面小编就为大家介绍一下两种方式具体的操作步骤吧... 目录方式一:借助split_clients指令1. 配置split_clients2. 配置后端服务器组3. 配

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML