【发布时间】:2010-01-13 17:00:23
【问题描述】:
我正在开发一个程序,该程序根据传递给 main 的参数按字母/数字对输入行进行排序。这是后续练习:
添加字段处理功能,因此可以对行内的字段进行排序,每个字段都根据一组独立的选项进行排序。 (这本书的索引使用 -df 表示索引类别和 -n 表示页码。)
我有点不明白它们对字段的含义。
field_of 函数究竟是做什么的?它是否会增加原始指针字段的时间,字段是非空白字符的字符串?
另外,如果输入了 num_fields,那么 compare 函数将在到达第一个非零比较时返回,对吗?如果结果为零(相等的字符串),那么它不会返回任何东西,因为不需要替换字符串。否则,它返回一个数字。
#include <stdio.h>
#include <string.h>
#define MAX_FIELDS 10
#define FLAG_DIRECTORY (0x01 << 0)
#define FLAG_FOLD (0x01 << 1)
#define FLAG_NUMERIC (0x01 << 2)
#define FLAG_REVERSE (0x01 << 3)
int fieldarray[MAX_FIELDS];
unsigned char flagarray[MAX_FIELDS];
int num_fields = 0;
#define MAXLINES 5000 /* max #lines to be sorted */
char *lineptr[MAXLINES]; /* pointers to text lines */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
static char *field_of(char *s, int n);
static isdir(int c);
int compare(char *, char *);
int stringcmp(char *, char *);
int stringcmpi(char *, char *);
int dircmp(char *, char *);
int numcmp(char *, char *);
int main()
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort((void **) lineptr, 0, nlines-1, (int (*)(void *, void *)) compare);
writelines(lineptr, nlines);
return 0;
} else {
printf("input too big to sort\n");
return 1;
}
}
#define MAXLEN 1000 /* max length of any input line */
int getline(char *, int);
char *alloc(int);
/* qsort: sort v[left]...v[right] into increasing order */
void qsort(void *v[], int left, int right,
int (*comp)(void *, void *))
{
int i, last;
void swap(void *v[], int, int);
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
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);
qsort(v, left, last-1, comp);
qsort(v, last+1, right, comp);
}
void swap(void *v[], int i, int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
static char *field_of(char *s, int n)
{
while (isspace(*s))
s++;
while (--n > 0) {
while (!isspace(*s)) {
if (*s == '\0')
return NULL;
s++;
}
}
return s;
}
static isdir(int c)
{
return isalpha(c) || isdigit(c) || isspace(c);
}
int compare_field(char *s1, char *s2, unsigned int flags)
{
int d;
if (flags & FLAG_NUMERIC) {
d = numcmp(s1, s2);
} else if (flags & FLAG_DIRECTORY) {
do {
while (!isdir(*s1) && !isspace(*s1) && *s1 != '\0')
s1++;
while (!isdir(*s2) && !isspace(*s2) && *s2 != '\0')
s2++;
if (flags & FLAG_FOLD)
d = toupper(*s1) - toupper(*s2);
else
d = *s1 - *s2;
} while (d == 0 && !isspace(*s1) && !isspace(*s2)
&& *s1++ != '\0' && *s2++ != '\0');
} else {
do {
if (flags & FLAG_FOLD)
d = toupper(*s1) - toupper(*s2);
else
d = *s1 - *s2;
} while (d == 0 && !isspace(*s1) && !isspace(*s2)
&& *s1++ != '\0' && *s2++ != '\0');
}
if (flags & FLAG_REVERSE)
return -d;
else
return d;
}
/* compare: compare s1 and s2 according to the values of the
external variables numeric, reverse, fold, and directory. */
int compare(char *s1, char *s2)
{
int i, d;
char *f1, *f2;
for (i = 0; i < num_fields; i++) {
f1 = field_of(s1, fieldarray[i]);
f2 = field_of(s2, fieldarray[i]);
d = compare_field(f1, f2, flagarray[i]);
if (d != 0)
return d;
}
if (numeric)
d = numcmp(s1, s2);
else if (directory)
d = dircmp(s1, s2);
else
d = stringcmp(s1, s2);
if (reverse)
return -d;
else
return d;
}
/* stringcmp: compare s1 and s2 as strings */
int stringcmp(char *s1, char *s2)
{
if (fold)
return stringcmpi(s1, s2);
else
return strcmp(s1, s2);
}
/* stringcmpi: compare s1 and s2 case-insensitively */
int stringcmpi(char *s1, char *s2)
{
while (toupper(*s1) == toupper(*s2)) {
if (*s1 == '\0')
return 0;
s1++;
s2++;
}
return toupper(*s1) - toupper(*s2);
}
/* dircmp: compare s1 and s2 in "directory order" */
int dircmp(char *s1, char *s2)
{
int d;
do {
while (!isdir(*s1) && *s1 != '\0')
s1++;
while (!isdir(*s2) && *s2 != '\0')
s2++;
if (fold)
d = toupper(*s1) - toupper(*s2);
else
d = *s1 - *s2;
} while (d == 0 && *s1++ != '\0' && *s2++ != '\0');
return d;
}
/* numcmp: compare s1 and s2 numerically */
int numcmp(char *s1, char *s2)
{
extern double atof(const char *);
double v1, v2;
v1 = atof(s1);
v2 = atof(s2);
if (v1 < v2)
return -1;
else if (v1 > v2)
return 1;
else
return 0;
}
【问题讨论】:
-
你会考虑缩短你的代码示例吗?很难说你问的是哪一部分。
-
老兄,没有冒犯,但你问这个问题的方式并没有帮助自己。 1)这个问题很模糊,没有明确定义的问题 2)这里的代码太多了。将其拉到您遇到问题或您想解释的简短 sn-ps。超过 10 或 15 行代码的问题很少能得到好的答案
-
是的,对不起...我提到的所有功能都在代码的末尾。我对 field_of 函数和比较函数的一些解释很感兴趣。
-
如果此代码来自 K&R,可以在这里发布吗?是否侵权?
-
强指南:如果代码显示垂直滚动条,你必须在发布前缩短它。